AES and RSA encryption compatible with PHP libraries

Asked

Viewed 445 times

3

I am starting a communication test work with a webservice, and this interaction includes the AES and RSA encryption protocols. However, the website that has the webservice (followzup) only presents examples in PHP and Java. How to do this test? Which protocol should I use first?

1 answer

1

Start by reading the class documentation Rsacryptoproviderservice and AES.

Here examples, taken from MSDN itself, of how to use them:

RSA encryption:

static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{        
    byte[] encryptedData;           
    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
    {

        RSA.ImportParameters(RSAKeyInfo);
        encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
    }
    return encryptedData;
}

RSA decryption:

static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
    byte[] decryptedData;
    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
    {

        RSA.ImportParameters(RSAKeyInfo);
        decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
    }

    return decryptedData;
}

And to use:

using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
    byte[] dataToEncrypt = ByteConverter.GetBytes("MEU DADO PARA CRIPTOGRAFAR");    
    byte[] encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false); // Criptografa
    byte[] decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false); // Descriptografa
}

AES encryption:

static byte[] AesEncrypt(string plainText, byte[] key, byte[] IV)
{
    byte[] encrypted;

    using (Aes aesAlg = Aes.Create())
    {
        aesAlg.Key = key;
        aesAlg.IV = IV;
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    swEncrypt.Write(plainText);
                }
                encrypted = msEncrypt.ToArray();
            }
        }
    }

    return encrypted;

}

AES decryption:

static string AesDecrypt(byte[] cipherText, byte[] key, byte[] IV)
{
    string plaintext = null;
    using (Aes aesAlg = Aes.Create())
    {
        aesAlg.Key = key;
        aesAlg.IV = IV;

        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
        {
            plaintext = srDecrypt.ReadToEnd();
        }
    }

    return plaintext;
}

And to use:

using (Aes myAes = Aes.Create())
{
    byte[] encrypted = AesEncrypt("MEU DADO PARA CRIPTOGRAFAR", myAes.Key, myAes.IV);
    string decrypted = AesDecrypt(encrypted, myAes.Key, myAes.IV);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.