'Convert function to javascript, calling from c++ to node.js server - encryption
I'm trying to encrypt a string using the juce framework generatekeypair, this create a RSA keypair and splits it into 2 parts, (and puts it in hex format I think).
This is the working code I'm using in c++:
const juce::String encryptString (const juce::String& str)
{
juce::RSAKey private_key2 ("thisistheprivatekeypart1,thisistheprivatekeypart2");
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);
private_key2.applyToValue(encodedInteger);
juce::MemoryBlock encodedMemoryBlock = encodedInteger.toMemoryBlock();
return encodedMemoryBlock.toBase64Encoding();
}
return {};
}
Now i'm doing my first steps in node.js, i've successfully setup a server that takes and sends parameters, does anyone know how to write this function in node.js?
On the juce website they have this unconfirmed java snippet but it's not helping me a lot:
public class RSAKey
{
static BigInteger applyToValue (BigInteger value, String key_part1, String key_part2)
{
BigInteger result = BigInteger.ZERO;
BigInteger part1 = new BigInteger (key_part1, 16);
BigInteger part2 = new BigInteger (key_part2, 16);
if (part1.equals (BigInteger.ZERO) || part2.equals (BigInteger.ZERO)
|| value.compareTo (BigInteger.ZERO) <= 0)
return result;
while (! value.equals (BigInteger.ZERO))
{
result = result.multiply (part2);
BigInteger[] div = value.divideAndRemainder (part2);
value = div[0];
result = result.add (div[1].modPow (part1, part2));
}
return result;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
