Exemplo n.º 1
0
    //==============================================================================
    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::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();
    }
}
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();
}
Exemplo n.º 4
0
void jojo_bang (t_jojo *x)
{
    const ScopedLock myLock (x->lock_); 
    
    post ("Public / %s", x->public_.toString().toRawUTF8());
    post ("Private / %s", x->private_.toString().toRawUTF8());
    
    String myText (CharPointer_UTF8 ("P\xc3\xa9p\xc3\xa9 p\xc3\xa8te en ao\xc3\xbbt!"));
    
    post ("%s", myText.toRawUTF8());
    
    const juce::MemoryBlock blockBegin (myText.toRawUTF8(), myText.getNumBytesAsUTF8() + 1);

    BigInteger bitArray;
    bitArray.loadFromMemoryBlock (blockBegin);
    x->public_.applyToValue (bitArray);             /* Encrypt with the public key. */

    post ("%s", bitArray.toString (16).toRawUTF8());
    
    x->private_.applyToValue (bitArray);            /* Then decrypt with the private key. */
    
    const juce::MemoryBlock blockEnd (bitArray.toMemoryBlock());
    post ("%s", blockEnd.toString().toRawUTF8());
}