2009-10-27 79 views
13

我必須加密/解密Xml文件中的一些敏感信息嗎? 是的,我可以通過編寫我自己的自定義算法來做到這一點。我想知道是否已經建立在.NET中的內置方式來做到這一點,以及我總是需要注意什麼點。如何在.NET中加密字符串?

回答

24

這裏有一對夫婦使用.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; 
} 

當然我不建議硬編碼像這樣:)

+2

密鑰和初始化向量「 ABC「&」123「對於Base-64字符數組無效。 – JeffO 2010-02-08 18:34:53

+1

它只是爲了說明,但公平點;) – Cocowalla 2010-02-24 03:54:27

+0

只是爲了增加其他訪客的價值 - 鑰匙和IV的長度可以是24個字符。例如:「keJhDo9YvJsp01j4JUdVuE ==」 – 2013-03-12 13:07:02