2

I'm writing an audio program in c++ with the juce framework and I'm able to successfully encrypt and decrypt. Juce has a function CreateKeypair: https://docs.juce.com/master/classRSAKey.html In the docs there is suggested code on how to encrypt in PHP or java, but it is untested. The createpair spits out a private and public key but the keys are split into 2 hex parts to be used with BigIntegers.

Now I'm trying to write the encryption in node.js so it's more secure. I'm not sure the math is correct, the code runs but the encryption key generated in the node code differs from the one on my local machine in c++. Can anyone have a look?

This is the sourceIntegertoString16:

736968746b63617263726576656e6c6c6977736c6f6f66756f7937342d35392d62332d31392d61312d3237

The encrypted string I end up with in c++ is:

64.xF51YfBPQS2WN9pC7bpbXaHq1nFQUTUBjDY+U05KRT+ZHsIF0JT7laW2LHaZccuazxI.msCk17VJL9Za0WivIA

Working code in c++:

const juce::String encryptString (const juce::String& str)
    {
        juce::RSAKey private_key2 ("559e161f5a7bd1924f5841b7bc4947d15baa16b61b530a2e30fbbf9ca98c48029ee9be8ace0c172e20fcce9a768cbedf4a4cac72073dc21d2feb9955c52aded1,b5efef02a0471d56e89b8ba6701bb89ce2c97042fa1075a22816f72ce84a1907415e574844420892762568831bf413f6d665a957e4e23c5bef3a9f15210c660d");
        auto utf8 = str.toUTF8();
           auto* utf8Address = utf8.getAddress();
           juce::MemoryBlock plainMemoryBlock(utf8Address, utf8.sizeInBytes());

           juce::BigInteger sourceInteger;
           sourceInteger.loadFromMemoryBlock(plainMemoryBlock);

           if (!sourceInteger.isZero())
           {
               juce::BigInteger encodedInteger(sourceInteger);
               std::cout << "dit heb je nodig" << sourceInteger.toString(16);
               private_key2.applyToValue(encodedInteger);

               juce::MemoryBlock encodedMemoryBlock = encodedInteger.toMemoryBlock();

               return encodedMemoryBlock.toBase64Encoding();
           }

           return {};
    }

Code in node.js:


const httpss = require('https')
var bigInt = require("big-integer");

const  key_part1 = '0x559e161f5a7bd1924f5841b7bc4947d15baa16b61b530a2e30fbbf9ca98c48029ee9be8ace0c172e20fcce9a768cbedf4a4cac72073dc21d2feb9955c52aded1';
const  key_part2 =  '0xb5efef02a0471d56e89b8ba6701bb89ce2c97042fa1075a22816f72ce84a1907415e574844420892762568831bf413f6d665a957e4e23c5bef3a9f15210c660d';

       var result = 0n;
       const part1 =  BigInt(key_part1);
        const part2 = BigInt(key_part2);
        var value = BigInt('0x736968746b63617263726576656e6c6c6977736c6f6f66756f7937342d35392d62332d31392d61312d3237');
var modded = 0n;
    while (value !== 0n)
        {
           result = result * part2;
            var remainder = BigInt(value % part2);
            value = BigInt(value / part2);
            console.log(remainder);
            modded = bigInt(remainder).modPow(part1, part2);
            console.log(modded);
            result = bigInt(result) + bigInt(modded);
 }
let data = result.toString();
let buff = new Buffer(data, 'base64');
let text = buff.toString('ascii');
            console.log(text);
console.log(result);

And this is the function for creating the keypair:

void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
                            const int numBits, const int* randomSeeds, const int numRandomSeeds)
{
    jassert (numBits > 16); // not much point using less than this..
    jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!

    BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
    BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? nullptr : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));

    const BigInteger n (p * q);
    const BigInteger m (--p * --q);
    const BigInteger e (findBestCommonDivisor (p, q));

    BigInteger d (e);
    d.inverseModulo (m);

    publicKey.part1 = e;
    publicKey.part2 = n;

    privateKey.part1 = d;
    privateKey.part2 = n;
}

This is the c++ code for encrypting:

bool RSAKey::applyToValue (BigInteger& value) const
{
    if (part1.isZero() || part2.isZero() || value <= 0)
    {
        jassertfalse;   // using an uninitialised key
        value.clear();
        return false;
    }

    BigInteger result;

    while (! value.isZero())
    {
        result *= part2;

        BigInteger remainder;
        value.divideBy (part2, remainder);

        remainder.exponentModulo (part1, part2);

        result += remainder;
    }

    value.swapWith (result);
    return true;
}
pppery
  • 3,731
  • 22
  • 33
  • 46
  • 1
    The key differs **how**? Please provide examples. Note that there are multiple ways of encoding RSA keys as they are not just a binary value, they consist of multiple numbers. – Maarten Bodewes May 11 '22 at 14:20
  • @MaartenBodewes I think I'm a bit out of my league here, i'm going through the Juce framwork code on how they do the calculations but I'm already a c++ beginner and porting this to node.js is a bit too much, I think I'm actually prob very close.. I want to encrypt a string using these methods and the private key in the code above. They are totally not identical but I'm probably also missing the way the source integer is loaded into the memory block and converted to hex – user1359023 May 11 '22 at 14:48
  • Encoding decoding to/from hex shouldn't be a big issue, right? Just copy / paste the hex into the question by hitting [edit]. – Maarten Bodewes May 11 '22 at 14:50
  • Ok, I'm closing in, up until the modpow everything runs the same, Juce has it's own mod calcs and the BigInt module on node js too, hmmm ```var modded = bigInt(remainder).modPow(part1, part2);``` ```remainder.exponentModulo (part1, part2);```` – user1359023 May 12 '22 at 06:33
  • I'm honestly quite at a loss with this question, not sure what to focus on; I guess you have to run the code to understand, but I don't have the time... – Maarten Bodewes May 12 '22 at 08:40
  • I think the problem is in the node bigInt library that I need to work with hex values in the modpow.. This remainder: 736968746b63617263726576656e6c6c6977736c6f6f66756f7936342d35392d62332d31392d61312d3237 in the modpow function should return 93091832a461e970a8fb517674ad15150acb50505dde8eb80ae30fa716e2bae2cd2e20f745a437dfdbfd89ac0162deb9b350107594129e3aaccd96974795253 but it returns a different value – user1359023 May 12 '22 at 09:19
  • I found the answer! indeed prob of hex, works now https://forum.juce.com/t/juce-rsa-implementation/10341/16 – user1359023 May 12 '22 at 14:25

0 Answers0