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. .NETコアで文字列を暗号化する
- 3. アドバンストインストーラ(13.3)でカスタム文字列を暗号化する方法カスタムアクション
- 4. asp.netの文字列を暗号化する方法C#
- 5. Click-Once - 接続文字列を暗号化する方法
- 6. Cでの文字列の暗号化
- 7. NodeJSのBlowfishで暗号化文字列
- 8. Linuxでの文字列暗号化
- 9. 非ベース64文字の文字列を暗号化する方法
- 10. .NETで短い文字列を暗号化する最も良い方法は何ですか?
- 11. JavaScript文字列の暗号化と復号化ですか?
- 12. WinForms 1.1のapp.configで接続文字列を暗号化する方法は?
- 13. Java文字列を暗号化
- 14. 暗号化/復号化方法に暗号化された文字列に '/'が含まれていない
- 15. パスワードを128文字に暗号化する方法は?
- 16. .Net暗号化
- 17. ジェネリックハンドラでJavascriptと復号化のクエリ文字列暗号化
- 18. C#でSHA256暗号化文字列を生成するには?
- 19. vb.netでクエリ文字列を暗号化するには?
- 20. Androidで自分の鍵で文字列を暗号化/復号化する方法は?
- 21. 秘密鍵で文字列を暗号化し、公開鍵で復号化する方法は?
- 22. エンティティフレームワークの暗号化接続文字列
- 23. Elasticserch 2.3.5文字列値の暗号化
- 24. 接続文字列の暗号化
- 25. Android SDKの文字列の暗号化
- 26. 暗号化SQL接続文字列
- 27. C#とObjectiveの文字列暗号化
- 28. 文字列暗号化の問題
- 29. クロスプラットフォームの文字列の暗号化
- 30. Grailsドメインクラスの列を暗号化/復号化する方法は?
」 ABC "&" 123 "はBase-64 char配列の長さが無効です。 – JeffO
イラストだけであったが、フェアポイント; – Cocowalla
ここに他の訪問者の価値を加えるだけで、キーとIVの長さは24文字にすることができます。例: "keJhDo9YvJsp01j4JUdVuE ==" –