Score:0

Encrypting RSA with a 2 part key

ru flag

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 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 I 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?

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;
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.