Tôi phải mã hóa/giải mã một số thông tin nhạy cảm trong một tệp Xml? Có, tôi có thể làm điều đó bằng cách viết các thuật toán tùy chỉnh của riêng tôi. Tôi tự hỏi nếu đã có một cách xây dựng trong NET để làm điều đó và cũng có những điểm tôi luôn luôn cần phải chăm sóc ..Làm thế nào để mã hóa một chuỗi trong .NET?
13
A
Trả lời
24
Dưới đây là một vài chức năng mà sử dụng .NET framework để mã hóa và giải mã một chuỗi:
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;
}
Tất nhiên tôi không khuyên hardcoding chìa khóa và khởi động vector như thế này :)
10
Bạn có thể sẽ muốn đi sâu vào không gian tên System.Security.Cryptography
. Tôi đoán các bài viết Cryptography Overview, Encrypting Data và Decrypting Data tại MSDN có thể là phần khởi đầu tốt.
Các vấn đề liên quan
- 1. Làm thế nào để mã hóa chuỗi cho Cụm từ thông dụng trong .NET?
- 2. Làm thế nào để đoán mã hóa của một tệp không có BOM trong .NET?
- 3. decode trăm mã hóa chuỗi C# .net
- 4. Làm cách nào để mã hóa chuỗi Dart trong base64?
- 5. Làm thế nào để giải mã một chuỗi được mã hóa bằng HMACSHA1?
- 6. làm thế nào để mã hóa/giải mã một chuỗi đơn giản
- 7. Làm cách nào để giải mã chuỗi mã hóa base64?
- 8. Làm thế nào để mã hóa uri một chuỗi trong jsp?
- 9. Làm thế nào để giải mã chuỗi được mã hóa bằng sha1 trong Java
- 10. Làm cách nào để mã hóa chuỗi này trong Ruby?
- 11. Làm thế nào để chuyển đổi một phản ứng Net :: HTTP thành một mã hóa nhất định trong Ruby 1.9.1?
- 12. Mã hóa chuỗi đơn giản trong .NET và Javascript
- 13. Làm thế nào để mã hóa một tệp .zip?
- 14. Làm thế nào để mã hóa mã nguồn Perl?
- 15. làm thế nào để mã hóa một thanh tiến trình
- 16. Làm thế nào để thêm/loại bỏ PKCS7 đệm từ một chuỗi mã hóa AES?
- 17. Làm thế nào để chuyển đổi một chuỗi được mã hóa hex thành chuỗi byte trong Perl?
- 18. Làm thế nào để giải mã chuỗi mã hóa "nhị phân" thành đệm nhị phân thô?
- 19. Làm thế nào để mã hóa dữ liệu trong máy chủ sql và giải mã nó trong ứng dụng .net
- 20. Làm thế nào để giải mã chuỗi mã hóa url trên Windows Mobile?
- 21. Làm thế nào tôi có thể đoán mã hóa của một chuỗi trong Perl?
- 22. Có cách nào tiêu chuẩn để mã hóa chuỗi .NET thành chuỗi JavaScript để sử dụng trong MS Ajax không?
- 23. Tôi làm cách nào để mã hóa chuỗi cho HTML?
- 24. Làm thế nào để mã hóa chuỗi bằng Base64 trong Android?
- 25. Làm thế nào để mã hóa các cột trong postgresql?
- 26. Làm thế nào để mã hóa trong VBScript bằng AES?
- 27. Làm thế nào để mã hóa chuỗi truy vấn trong asp.net?
- 28. Trong Android làm thế nào để nối chuỗi mã hóa base64?
- 29. làm thế nào để mã hóa url trong android?
- 30. Làm thế nào để mã hóa (.) Dot trong url Rails
" ABC "&" 123 "là độ dài không hợp lệ đối với mảng char Base-64. – JeffO
Nó chỉ có nghĩa là một minh họa, nhưng điểm công bằng;) – Cocowalla
Chỉ cần thêm giá trị cho các khách truy cập khác ở đây - chiều dài của cả khóa và IV có thể là 24 ký tự. Ví dụ: "keJhDo9YvJsp01j4JUdVuE ==" –