Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在.NET中加密字符串?_C#_.net_Encryption_Cryptography - Fatal编程技术网

C# 如何在.NET中加密字符串?

C# 如何在.NET中加密字符串?,c#,.net,encryption,cryptography,C#,.net,Encryption,Cryptography,我必须加密/解密Xml文件中的一些敏感信息? 是的,我可以通过编写自己的自定义算法来做到这一点。我想知道在.NET中是否已经有一种内置的方法可以做到这一点,以及我需要注意的地方。您可能会想深入研究名称空间。我想这些文章和MSDN上的文章可能是很好的入门资料。以下是一些使用.NET framework对字符串进行加密和解密的函数: public string EncryptString(string plainText) { // Instantiate a new RijndaelMan

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

您可能会想深入研究名称空间。我想这些文章和MSDN上的文章可能是很好的入门资料。

以下是一些使用.NET framework对字符串进行加密和解密的函数:

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都必须不同。@davidhibault固定的IV只是一个例子,但是的,每次加密时它都应该不同。通常应使用
rijndaelCipher.GenerateIV()
生成唯一的IV