我必须加密/解密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
您可能会想要潜入System.Security.Cryptography
命名空间。我猜想MSDN上的文章Cryptography Overview,Encrypting Data和Decrypting Data可能是不错的首发。
相关问题
- 1. 如何在c#中加密字符串?
- 2. 如何在AES加密中加密/解密多个字符串?
- 3. 使用.NET核心加密字符串
- 4. 如何解密在Delphi中加密的C#中的字符串
- 5. 如何在android中使用特殊字符加密字符串?
- 6. 如何在iOS中解密DES加密字符串?
- 7. 如何在C#中对字符串进行加密和解密#
- 8. 如何在Java中解密sha1加密的字符串
- 9. 如何完成在android中加密和解密字符串值?
- 10. 加密/解密字符串
- 11. 解密字符串加密
- 12. 加密字符串
- 13. 字符串加密
- 14. 在.NET中加密一个字符串并在PHP中解密它?
- 15. 在.NET和Javascript中的简单字符串加密
- 16. AES加密 - 如何以后解密加密的字符串?
- 17. 如何在使用JS加密技术加密的Java字符串中解密
- 18. 如何加密Sql连接字符串?
- 19. 如何使用GnuPG加密字符串?
- 20. 如何使用InstallScript加密字符串
- 21. 如何使用MD5加密字符串?
- 22. 如何使用node.js加密字符串?
- 23. 加密web.config中的密码字符串
- 24. 如何在.NET中翻译字符串
- 25. 在Android SDK中加密字符串
- 26. 在web.config中加密连接字符串
- 27. 在app.config中加密连接字符串
- 28. 如何解密java中的加密字符串
- 29. 我应该如何管理.net中的密码字符串?
- 30. 加密字符串到数字在PHP
密钥和初始化向量“ ABC“&”123“对于Base-64字符数组无效。 – JeffO 2010-02-08 18:34:53
它只是为了说明,但公平点;) – Cocowalla 2010-02-24 03:54:27
只是为了增加其他访客的价值 - 钥匙和IV的长度可以是24个字符。例如:“keJhDo9YvJsp01j4JUdVuE ==” – 2013-03-12 13:07:02