asp.net – AES and RSA encryption compatible with PHP libraries

Question:

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 take this test? Which protocol should I use first?

Answer:

Start by reading the documentation for the RSACryptoProviderService and AES classes.

Here are 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;
}

Decriptografia RSA :

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;

}

Decriptografia AES:

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);
}
Scroll to Top