//============================================================================== static String encryptXML (const XmlElement& xml, RSAKey privateKey) { MemoryOutputStream text; text << xml.createDocument (StringRef(), true); BigInteger val; val.loadFromMemoryBlock (text.getMemoryBlock()); privateKey.applyToValue (val); return val.toString (16); }
String EncryptedString::decrypt (const String& encryptedString, const String& privateKey, bool inputIsHex) { RSAKey rsaKey (privateKey); MemoryBlock encryptedMemoryBlock; if (inputIsHex) { encryptedMemoryBlock.loadFromHexString (encryptedString); } else { encryptedMemoryBlock.fromBase64Encoding (encryptedString); } BigInteger stringAsData; stringAsData.loadFromMemoryBlock (encryptedMemoryBlock); rsaKey.applyToValue (stringAsData); return stringAsData.toMemoryBlock().toString(); }
//============================================================================== static XmlElement decryptXML (String hexData, RSAKey rsaPublicKey) { BigInteger val; val.parseString (hexData, 16); RSAKey key (rsaPublicKey); jassert (key.isValid()); ScopedPointer<XmlElement> xml; if (! val.isZero()) { key.applyToValue (val); const MemoryBlock mb (val.toMemoryBlock()); if (CharPointer_UTF8::isValidString (static_cast<const char*> (mb.getData()), (int) mb.getSize())) xml = XmlDocument::parse (mb.toString()); } return xml != nullptr ? *xml : XmlElement("key"); }
String EncryptedString::encrypt (const String& stringToEncrypt, const String& publicKey, bool resultAsHex) { RSAKey rsaKey (publicKey); CharPointer_UTF8 stringPointer (stringToEncrypt.toUTF8()); MemoryBlock stringMemoryBlock (stringPointer.getAddress(), stringPointer.sizeInBytes()); BigInteger stringAsData; stringAsData.loadFromMemoryBlock (stringMemoryBlock); rsaKey.applyToValue (stringAsData); if (resultAsHex) { MemoryBlock encryptedMemoryBlock (stringAsData.toMemoryBlock()); return String::toHexString ((char*) encryptedMemoryBlock.getData(), (int) encryptedMemoryBlock.getSize(), 0); } else { return stringAsData.toMemoryBlock().toBase64Encoding(); } }