我正在学习C#,正在练习解密和加密。
我使用了Aes Class Example from MSDN,加密效果很好。
但是,当我尝试解密文本文件时,没有任何反应。
我认为我在解密功能中做错了,所以我用字符串测试了。
解密与字符串完美配合所以我想知道我做错了什么。
这是我的加密和解密的样子:
//The encryption
string path = @"C:\encrypt.txt";
//The path where my .txt file is.
string textfile = File.ReadAllText(path);
//Reading the text in the file and storing it in a string.
byte[] ByteToEncrypt = EncryptStringToBytes(textfile, myAes.Key, myAes.IV);
//From The Aes class example
string encrypted = "";
//Creating a string to store the bytes in.
for (int i = 0; i < ByteToEncrypt.Length; i++)
{
encrypted = encrypted + ByteToEncrypt[i];
//Adding all the bytes from an array to string.
}
File.WriteAllText(path, encrypted, LATIN1);
//Writing the encrypted string to the .txtfile.
//The decryption
string decrypted = DecryptStringFromBytes(ByteToEncrypt, myAes.Key, myAes.IV);
//From the Aes class example.
File.WriteAllText(path, Decrypted, LATIN1);
//Writing the decrypted string to textfile.
我在下面发布Aes课程以获取更多信息。
static byte[] EncryptStringToBytes(string plaintext, byte[] Key, byte[] IV)
{
//Checking the arguments
if (string.IsNullOrEmpty(plaintext))
{
throw new ArgumentNullException("plaintext");
}
else if (Key == null || Key.Length <= 0)
{
throw new ArgumentNullException("key");
}
else if (IV == null || IV.Length <= 0)
{
throw new ArgumentNullException("IV");
}
byte[] encrypted;
//Create an Aes object
//With the specified key and IV
using (Aes Aesalg = Aes.Create())
{
Aesalg.Key = Key;
Aesalg.IV = IV;
//Create a encryptor to perform the stream transform
ICryptoTransform encryptor = Aesalg.CreateEncryptor(Aesalg.Key, Aesalg.IV);
//Create the streams used for encryption
using (MemoryStream msencrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msencrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plaintext);
}
encrypted = msencrypt.ToArray();
}
}
}
return encrypted;
}
static string DecryptStringFromBytes(byte[] ciphertext, byte[] Key, byte[] IV)
{
if (ciphertext == null || ciphertext.Length <= 0)
{
throw new ArgumentNullException("plaintext");
}
else if (Key == null || Key.Length <= 0)
{
throw new ArgumentNullException("Key");
}
else if (IV == null || IV.Length <= 0)
{
throw new ArgumentNullException("IV");
}
string plaintext = null;
using (Aes Aesalg = Aes.Create())
{
Aesalg.Key = Key;
Aesalg.IV = IV;
ICryptoTransform decryptor = Aesalg.CreateDecryptor(Aesalg.Key, Aesalg.IV);
using (MemoryStream msDecrypt = new MemoryStream(ciphertext))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
答案 0 :(得分:2)
您不能只为字符串添加字节。字节可以包含00
和FF
之间的任何值(使用十六进制表示)。您似乎只是将这些字节作为整数添加到字符串中,如
encrypted = encrypted + ByteToEncrypt[i];
只为字节值"0255"
和00
创建一个字符串值,例如FF
。当然,当你尝试解密这样一个字符串时,你会遇到一个问题,只是因为字节不再分开了。
相反,您应该在加密后创建密文的base 64编码,并在解密之前将其转换回字节。或者,正如ntoskrnl建议的那样,你当然可以将密文一直视为二进制文件,如果这是一个选项。