Home >Backend Development >C++ >How to Encrypt and Decrypt Strings in C# Using RijndaelManaged?

How to Encrypt and Decrypt Strings in C# Using RijndaelManaged?

Barbara Streisand
Barbara StreisandOriginal
2025-02-02 17:01:09311browse

In C#, use the Rijndaelmanaged algorithm encryption and unclean string detailed explanation

How to Encrypt and Decrypt Strings in C# Using RijndaelManaged?

This article will introduce in detail how to use the Rijndaelmanaged algorithm in C#to encrypt and decrypt the string.

The encryption process

First of all, you need to introduce

Naming space. The following code fragment demonstrates how to use the Rijndaelmanaged algorithm plus a complicated string:

System.Security.Cryptography

The decryption process
<code class="language-csharp">using System.Security.Cryptography;

public class Encryption
{
    // 使用RijndaelManaged算法加密字符串
    public string EncryptString(string plainText, string sharedSecret)
    {
        // 从共享密钥和salt生成密钥
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

        // 创建RijndaelManaged对象
        RijndaelManaged aesAlg = new RijndaelManaged();
        aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

        // 创建加密器
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // 创建内存流来保存加密数据
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            // 将IV写入流
            msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
            msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);

            // 加密数据
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    swEncrypt.Write(plainText);
                }
            }

            // 将加密数据转换为base64字符串
            return Convert.ToBase64String(msEncrypt.ToArray());
        }
    }
}</code>

Decrypting and encryption string needs to use the same shared key as when encrypted. The following code fragment demonstrates how to use the same Rijndaelmanaged algorithm string:

Please note that It needs to be defined as a Byte array in the code for enhanced security. The complete code also needs to contain the implementation of the

method. This method read the byte array from
<code class="language-csharp">public string DecryptString(string cipherText, string sharedSecret)
{
    // 从共享密钥和salt生成密钥
    Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);

    // 创建RijndaelManaged对象
    RijndaelManaged aesAlg = new RijndaelManaged();
    aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);

    // 将密文从base64字符串转换
    byte[] bytes = Convert.FromBase64String(cipherText);

    // 创建内存流来保存解密数据
    using (MemoryStream msDecrypt = new MemoryStream(bytes))
    {
        // 从流中获取IV
        aesAlg.IV = ReadByteArray(msDecrypt);

        // 创建解密器
        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

        // 解密数据
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
        {
            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
            {
                // 从流中读取解密数据
                return srDecrypt.ReadToEnd();
            }
        }
    }
}</code>
. Remember, choosing a strong sharing key is essential for security.

The above is the detailed content of How to Encrypt and Decrypt Strings in C# Using RijndaelManaged?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn