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 BigInteger
s.
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;
}