在我的项目中,我有两种方法来加密和解密XML文件
以及另一种在XML文件中保存和加载数据的方法
我不知道为什么在加载数据Load()
输入不是有效的Base-64字符串,因为它包含非基数64 字符,两个以上的填充字符或非空白字符 填充字符中的字符。
这是我的方法
public static string Encrypt(string plainText)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes("teto1620@#$%asdf");
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = Encoding.Unicode.GetBytes("_+)&qwer9512popo");
var symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
public static string Decrypt(string cipherText)
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes("teto1620@#$%asdf");
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
byte[] keyBytes = Encoding.Unicode.GetBytes("_+)&qwer9512popo");
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
return plainText;
}
另外两种保存和加载数据的方法
public bool Save()
{
bool isSaved = false;
try
{
if (SharedData.DeviceList != null)
{
var fileInfo = new FileInfo(SharedData.CONFIGURATION_FULL_PATH);
if (!fileInfo.Exists)
{
File.Create(SharedData.CONFIGURATION_FULL_PATH).Close();
}
var streamWriter = new StreamWriter(SharedData.CONFIGURATION_FULL_PATH);
streamWriter.Write(Encrypt("<?xml version=\"1.0\" encoding=\"utf-8\"?><settings>"));
if (SharedData.DeviceList.Count > 0)
{
foreach (var device in SharedData.DeviceList)
{
streamWriter.Write(Encrypt("<username>" +device.Username + "</username>"));
streamWriter.Write(Encrypt("<AgentName>" +device.AgentName + "</AgentName>"));
streamWriter.Write(Encrypt("<password>" + device.Password + "</password>"));
streamWriter.Write(Encrypt("<domain>" + device.Domain + "</domain>"));
streamWriter.Write(Encrypt("<peerUri>" + device.PeerURI + "</peerUri>"));
streamWriter.Write(Encrypt("<sipUri>" + device.SipURI + "</sipUri>"));
streamWriter.Write(Encrypt("<fqdn>" + device.FQDN+ "</fqdn>"));
streamWriter.Write(Encrypt("<type>" + ((byte)device.Type).ToString() + "</type>"));
streamWriter.Write(Encrypt("<transportType>" +((byte)device.TransportType).ToString() + "</transportType>"));
}
}
streamWriter.Write(Encrypt("</settings>"));
streamWriter.Close();
isSaved = true;
}
else isSaved = false;
}
catch { isSaved = false; }
return isSaved;
}
private bool Load()
{
bool isLoaded = false;
try
{
if (SharedData.DeviceList != null)
{
var fileInfo = new FileInfo(SharedData.CONFIGURATION_FULL_PATH);
if (fileInfo.Exists)
{
var textTodecrypt = File.ReadAllText(SharedData.CONFIGURATION_FULL_PATH);
var xmlReader = new XmlTextReader(Decrypt(textTodecrypt));
var nodeElement = string.Empty;
var thisDevice = new Device();
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element) nodeElement = xmlReader.Name;
if (xmlReader.NodeType == XmlNodeType.Text)
{
switch (nodeElement)
{
case @"username":
if (xmlReader.Value.Length > 0) thisDevice = new Device
{
Username = xmlReader.Value
};
break;
case @"AgentName":
if (xmlReader.Value.Length > 0)
thisDevice.AgentName = xmlReader.Value;
break;
case @"password":
if (xmlReader.Value.Length > 0)
thisDevice.Password = xmlReader.Value;
break;
case @"peerUri":
if (xmlReader.Value.Length > 0)
thisDevice.PeerURI = xmlReader.Value;
break;
case @"sipUri":
if (xmlReader.Value.Length > 0)
thisDevice.SipURI = xmlReader.Value;
break;
case @"domain":
if (xmlReader.Value.Length > 0)
thisDevice.Domain = xmlReader.Value;
break;
case @"fqdn":
if (xmlReader.Value.Length > 0)
thisDevice.FQDN = xmlReader.Value;
break;
case @"type":
if (xmlReader.Value.Length > 0)
thisDevice.Type = (Enums.DeviceType)byte.Parse(xmlReader.Value);
break;
case @"transportType":
if (xmlReader.Value.Length > 0)
{
thisDevice.TransportType = (Enums.ServerTransportType)byte.Parse(xmlReader.Value);
if (!IsExist(thisDevice, false)) SharedData.DeviceList.Add(thisDevice);
}
break;
}
}
}
xmlReader.Close();
SharedData.TempDeviceList = SharedData.DeviceList;
isLoaded = true;
}
else isLoaded = false;
}
}
catch (Exception)
{
isLoaded = false;
}
return isLoaded;
}
答案 0 :(得分:5)
好的,您的错误是您逐行加密数据并尝试将其解密为整个块。
Base64将每3个字节转换为4个字符。如果输入数据不是3个字节的倍数,则编码会添加1或2个零字节作为填充。然后在流的末尾用一个或两个'='字符表示。
现在,如果您尝试解码多个连接的代码块,您很可能在流中包含'='字符,这是非法的。
解决方案:重写Save-Method,首先将所有xml实体存储在一个大字符串/内存块中(使用StringBuilder或写入MemoryStream),并立即对整个数据块使用加密方法。 / p>