简体   繁体   中英

How can I encrypt data in Delphi and in C# so that they are compatible?

I'd like to encrypt some data on the client in my Delphi application in a way that is compatible with the encryption on my server. On my server, I encrypt the data using this C# code:

public class AesCryptUtils
{

    private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");

    /// <summary> 
    /// Encrypt the given string using AES.  The string can be decrypted using  
    /// DecryptStringAES().  The sharedSecret parameters must match. 
    /// </summary> 
    /// <param name="plainText">The text to encrypt.</param> 
    /// <param name="sharedSecret">A password used to generate a key for encryption.</param> 
    public static string EncryptStringAES(string plainText, string sharedSecret)
    {
        if (string.IsNullOrEmpty(plainText))
            throw new ArgumentNullException("plainText");
        if (string.IsNullOrEmpty(sharedSecret))
            throw new ArgumentNullException("sharedSecret");

        string outStr = null;                       // Encrypted string to return 
        AesManaged aesAlg = null;              // AesManaged object used to encrypt the data. 

        try
        {
            // generate the key from the shared secret and the salt 
            Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

            // Create a AesManaged object 
            // with the specified key and IV. 
            aesAlg = new AesManaged();
            aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
            aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);

            // Create a decrytor to perform the stream transform. 
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream. 
                        swEncrypt.Write(plainText);
                    }
                }
                outStr = Convert.ToBase64String(msEncrypt.ToArray());
            }
        }
        finally
        {
            // Clear the AesManaged object. 
            if (aesAlg != null)
                aesAlg.Clear();
        }

        // Return the encrypted bytes from the memory stream. 
        return outStr;
    }
}

How could this encryption algorithm be implemented in Delphi? The resulting encrypted data must be the same given the same inputs.

The related questions list for your question contains this link which mentions some AES implementation for Delphi. I'm sure you can find some more and you can always use something like OpenSSL or CryptoAPI but you may need to write Delphi bindings for them yourself.

Note that as you don't pass the key directly you will need to implement the key derivation too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM