【问题标题】:How to encrypt a string in .NET? [duplicate]如何在 .NET 中加密字符串? [复制]
【发布时间】:2009-10-27 10:14:55
【问题描述】:

我必须加密/解密 Xml 文件中的一些敏感信息? 是的,我可以通过编写自己的自定义算法来做到这一点。我想知道 .NET 中是否已经有内置方法可以做到这一点,以及我总是需要注意哪些方面..

【问题讨论】:

    标签: c# .net encryption cryptography


    【解决方案1】:

    这里有几个使用 .NET 框架加密和解密字符串的函数:

    public string EncryptString(string plainText)
    {
        // Instantiate a new RijndaelManaged object to perform string symmetric encryption
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
    
        // Set key and IV
        rijndaelCipher.Key = Convert.FromBase64String("ABC");
        rijndaelCipher.IV = Convert.FromBase64String("123");
    
        // Instantiate a new MemoryStream object to contain the encrypted bytes
        MemoryStream memoryStream = new MemoryStream();
    
        // Instantiate a new encryptor from our RijndaelManaged object
        ICryptoTransform rijndaelEncryptor = rijndaelCipher.CreateEncryptor();
    
        // Instantiate a new CryptoStream object to process the data and write it to the 
        // memory stream
        CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelEncryptor, CryptoStreamMode.Write);
    
        // Convert the plainText string into a byte array
        byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
    
        // Encrypt the input plaintext string
        cryptoStream.Write(plainBytes, 0, plainBytes.Length);
    
        // Complete the encryption process
        cryptoStream.FlushFinalBlock();
    
        // Convert the encrypted data from a MemoryStream to a byte array
        byte[] cipherBytes = memoryStream.ToArray();
    
        // Close both the MemoryStream and the CryptoStream
        memoryStream.Close();
        cryptoStream.Close();
    
        // Convert the encrypted byte array to a base64 encoded string
        string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
    
        // Return the encrypted data as a string
        return cipherText;
    }
    
    
    public string DecryptString(string cipherText)
    {
        // Instantiate a new RijndaelManaged object to perform string symmetric encryption
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
    
        // Set key and IV
        rijndaelCipher.Key = Convert.FromBase64String("ABC");
        rijndaelCipher.IV = Convert.FromBase64String("123");
    
        // Instantiate a new MemoryStream object to contain the encrypted bytes
        MemoryStream memoryStream = new MemoryStream();
    
        // Instantiate a new encryptor from our RijndaelManaged object
        ICryptoTransform rijndaelDecryptor = rijndaelCipher.CreateDecryptor();
    
        // Instantiate a new CryptoStream object to process the data and write it to the 
        // memory stream
        CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelDecryptor, CryptoStreamMode.Write);
    
        // Will contain decrypted plaintext
        string plainText = String.Empty;
    
        try
        {
            // Convert the ciphertext string into a byte array
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
    
            // Decrypt the input ciphertext string
            cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
    
            // Complete the decryption process
            cryptoStream.FlushFinalBlock();
    
            // Convert the decrypted data from a MemoryStream to a byte array
            byte[] plainBytes = memoryStream.ToArray();
    
            // Convert the encrypted byte array to a base64 encoded string
            plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);
        }
        finally
        {
            // Close both the MemoryStream and the CryptoStream
            memoryStream.Close();
            cryptoStream.Close();
        }
    
        // Return the encrypted data as a string
        return plainText;
    }
    

    当然,我不建议像这样对密钥和初始化向量进行硬编码 :)

    【讨论】:

    • "ABC" & "123" 是 Base-64 字符数组的无效长度。
    • 这只是一个插图,但公平点;)
    • 只是一个旁注:使用 Encoding.UTF8 而不是 Encoding.ASCII,因为世界不是只有英文的地方。
    • IV 对于每个加密必须不同。
    • @DavidThibault 固定的 IV 仅作为说明,但是是的,每次加密时它都应该不同。您通常应该使用 rijndaelCipher.GenerateIV() 来生成唯一的 IV
    【解决方案2】:

    您可能想要深入了解System.Security.Cryptography 命名空间。我想 MSDN 上的文章 Cryptography OverviewEncrypting DataDecrypting Data 可能是不错的入门。

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2014-12-02
      • 2019-06-28
      • 2010-12-11
      • 1970-01-01
      相关资源
      最近更新 更多