我將如何處理讀取和寫入加密的XML文件?我需要一種方法來保存用戶最後輸入的表單值,但我不希望它們以純文本或XML的形式可讀。我將如何讀寫加密的XML文件?
0
A
回答
6
的例子還有就是可在您一次性在System.Security.Cryptography.Xml Namespace
確保XML數據類的全面收集。
花一些時間來閱讀下面的文章MSDN文章:
- How to: Encrypt XML Elements with Symmetric Keys
- How to: Decrypt XML Elements with Symmetric Keys
- How to: Sign XML Documents with Digital Signatures
如果您訪問在導航菜單中的根節點上的任何鏈接的文章可以查看更多關於XML安全性的文章。
+0
謝謝,這非常有用! – Jack
0
我想,你可以保存用戶保存的字符串,然後用加密的字符串只寫一個常規的XML文件?
下面是here
public class Crypto
{
private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");
/// <summary>
/// Encrypt the given string using AES. The string can be decrypted using
/// DecryptStringAES(). The sharedSecret parameters must match.
/// </summary>
/// <param name="plainText">The text to encrypt.</param>
/// <param name="sharedSecret">A password used to generate a key for encryption.</param>
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
string outStr = null; // Encrypted string to return
RijndaelManaged aesAlg = null; // RijndaelManaged object used to encrypt the data.
try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
// Create a RijndaelManaged object
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize/8);
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
// prepend the IV
msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
// Return the encrypted bytes from the memory stream.
return outStr;
}
/// <summary>
/// Decrypt the given string. Assumes the string was encrypted using
/// EncryptStringAES(), using an identical sharedSecret.
/// </summary>
/// <param name="cipherText">The text to decrypt.</param>
/// <param name="sharedSecret">A password used to generate a key for decryption.</param>
public static string DecryptStringAES(string cipherText, string sharedSecret)
{
if (string.IsNullOrEmpty(cipherText))
throw new ArgumentNullException("cipherText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
try
{
// generate the key from the shared secret and the salt
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
// Create the streams used for decryption.
byte[] bytes = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize/8);
// Get the initialization vector from the encrypted stream
aesAlg.IV = ReadByteArray(msDecrypt);
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
}
private static byte[] ReadByteArray(Stream s)
{
byte[] rawLength = new byte[sizeof(int)];
if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
{
throw new SystemException("Stream did not contain properly formatted byte array");
}
byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];
if (s.Read(buffer, 0, buffer.Length) != buffer.Length)
{
throw new SystemException("Did not read byte array properly");
}
return buffer;
}
}
相關問題
- 1. 如何將XML寫入加密文件?
- 2. 讀/寫加密的XML
- 3. 如何使用LINQ to XML讀取/寫入加密的XML文件?
- 4. Java:如何在文件中寫入/讀取加密密碼
- 5. 如何加密/解密XML文件?
- 6. 讀寫XML文件
- 7. 如何讀取/寫入xml文件?
- 8. 如何讀取和寫入XML文件?
- 9. 加密xml文件
- 10. XML文件加密
- 11. 如何將文本寫入XML文件?
- 12. 如何在php中加密xml文件
- 13. C++:如何加密XML配置文件
- 14. 以讀寫權限存儲加密文件的密碼
- 15. 如何將xml文件讀入richtextbox?
- 16. 加密和解密的XML文件
- 17. 如何將變量寫入XML文件?
- 18. 如何將數據寫入xml文件
- 19. 如何將數據寫入XML文件?
- 20. 如何將XML文件寫入到AWS
- 21. 如何將位圖寫入XML文件
- 22. 如何讀取使用AES算法加密並加密的加密文件?
- 23. 我如何從Java加密文件加密的文件中解密objective-c
- 24. 用ruby讀/寫密碼保護和加密文件
- 25. 如何將我的xml輸出寫入文件
- 26. 如何寫.js文件從XML文件中讀取
- 27. 如何用PHP XML讀寫器更新這個xml文件?
- 28. 最近我如何讀取加密文件(encfs)
- 29. 如何使用pycrypto將加密數據寫入文件?
- 30. 如何將XML文件到我的netbeans
與加密任何其他字節流相同。 –
請勿爲此使用xml。 – AntoineLev
我會用什麼來做到這一點? – Jack