Xmlファイル内の機密情報を暗号化/復号化する必要がありますか? はい私自身のカスタムアルゴリズムを書くことでこれを行うことができます。私はそれに.NETで既に組み込まれている方法があるのか、またいつも気をつけなければならない点があるのか疑問に思っています。.NETで文字列を暗号化する方法は?
13
A
答えて
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;
}
私はこのようなキーと初期ベクトルをハードコーディング助言するものではありません:)
10
名前空間に潜在的に潜入しています。私は、MSDNのCryptography Overview、Encrypting Data、Decrypting Dataの記事が良いスタートになると思います。
関連する問題
- 1. apache.commons.codecを使用してJavaクライアントで文字列を暗号化(.NETで暗号化)する方法Base64?
- 2. Click-Once - 接続文字列を暗号化する方法
- 3. Linuxでの文字列暗号化
- 4. JavaScript文字列の暗号化と復号化ですか?
- 5. Java文字列を暗号化
- 6. WinForms 1.1のapp.configで接続文字列を暗号化する方法は?
- 7. 暗号化/復号化方法に暗号化された文字列に '/'が含まれていない
- 8. .NETで短い文字列を暗号化する最も良い方法は何ですか?
- 9. パスワードを128文字に暗号化する方法は?
- 10. ジェネリックハンドラでJavascriptと復号化のクエリ文字列暗号化
- 11. C#とObjectiveの文字列暗号化
- 12. エンティティフレームワークの暗号化接続文字列
- 13. 文字列暗号化の問題
- 14. Android SDKの文字列の暗号化
- 15. クロスプラットフォームの文字列の暗号化
- 16. vb.netでクエリ文字列を暗号化するには?
- 17. 秘密鍵で文字列を暗号化し、公開鍵で復号化する方法は?
- 18. Grailsドメインクラスの列を暗号化/復号化する方法は?
- 19. jsonの文字列を暗号化/復号化する最も良い方法は何ですか
- 20. app.configなしで接続文字列を暗号化する方法
- 21. .Net暗号化
- 22. AndroidとJavaで異なる文字列を暗号化/復号化する
- 23. JavaとPHPの間の文字列の暗号化/復号化
- 24. クエリ文字列パラメータの暗号化/復号化?シルバーライトのUsabe?
- 25. sha1で暗号化された文字列をJavaで復号化する方法
- 26. 配列を文字列として暗号化する
- 27. WCF aspnet_regiisで接続文字列を暗号化する
- 28. phpでの文字列の暗号化と復号
- 29. 暗号化されたデータを文字列に変換する方法
- 30. 接続文字列の暗号化、アイデアは何ですか?
」 ABC "&" 123 "はBase-64 char配列の長さが無効です。 – JeffO
イラストだけであったが、フェアポイント; – Cocowalla
ここに他の訪問者の価値を加えるだけで、キーとIVの長さは24文字にすることができます。例: "keJhDo9YvJsp01j4JUdVuE ==" –