2009-10-27 25 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 char配列の長さが無効です。 – JeffO

+1

イラストだけであったが、フェアポイント; – Cocowalla

+0

ここに他の訪問者の価値を加えるだけで、キーとIVの長さは24文字にすることができます。例: "keJhDo9YvJsp01j4JUdVuE ==" –

関連する問題