如何从加密字符串中字节[]?
How to byte[] from an encrypted string?
我正在尝试加密和解密一个字符串,我可以成功完成。之后,我将加密的byte[]
转换为字符串保存在数据库中。
但是当我再次尝试将字符串转换回 byte[]
时,我的代码会抛出错误。
为了测试代码,我创建了一个字符串变量来保存加密的字符串。
the print error
如何将字符串再次成功转换回 byte[]
?
static void Main(string[] args)
{
Console.WriteLine("Enter text that needs to be encrypted..");
string data = Console.ReadLine();
EncryptAesManaged(data);
Console.ReadLine();
}
static void EncryptAesManaged(string raw)
{
string EncryptionKey = "sruohfaymonerishfiahbihbgrG546";
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(raw);
try
{
using (AesManaged aes = new AesManaged())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(EncryptionKey);
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
// Encrypt string
byte[] encrypted = Encrypt(raw, aes.Key, aes.IV);
// Print encrypted string
string passe = System.Text.Encoding.UTF8.GetString(encrypted);
Console.WriteLine($"Encrypt:{passe}");
Console.WriteLine(System.Text.Encoding.UTF8.GetBytes(passe));
// Decrypt the bytes to a string.
string decrypted = Decrypt(System.Text.Encoding.UTF8.GetBytes(passe), aes.Key, aes.IV);
// Print decrypted string. It should be same as raw data
Console.WriteLine($"Decrypted data: {decrypted}");
}
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
Console.ReadKey();
}
static byte[] Encrypt(String plainText, byte[] Key, byte[] IV)
{
byte[] encrypted;
using (AesManaged aes = new AesManaged())
{
// Create encryptor
ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
// Create MemoryStream
using (MemoryStream ms = new MemoryStream())
{
// Create crypto stream using the CryptoStream class. This class is the key to encryption
// and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream
// to encrypt
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
// Create StreamWriter and write data to a stream
using (StreamWriter sw = new StreamWriter(cs))
sw.Write(plainText);
encrypted = ms.ToArray();
}
}
}
return encrypted;
}
static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
string plaintext = null;
// Create AesManaged
using (AesManaged aes = new AesManaged())
{
// Create a decryptor
ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
// Create the streams used for decryption.
using (MemoryStream ms = new MemoryStream(cipherText))
{
// Create crypto stream
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
// Read crypto stream
using (StreamReader reader = new StreamReader(cs))
plaintext = reader.ReadToEnd();
}
}
}
return plaintext;
}
问题就在这里:
string passe = System.Text.Encoding.UTF8.GetString(encrypted);
^---------------------------------^
然后在这里:
string decrypted = Decrypt(System.Text.Encoding.UTF8.GetBytes(passe), aes.Key, aes.IV);
^--------------------------------^
我假设这是您将其存储到数据库然后取回并将其存储为字符串的模拟。
但是,您所做的几乎肯定会以这种方式损坏字符串。
Encoding.UTF8.GetString(bytes)
不会 将包含任意字节的字节数组转换为字符串。相反,它将字节数组 应该 包含组成 UTF8 编码字符串的字节返回到该字符串。
如果字节数组包含任意字节,例如加密文本的结果,此步骤及其伴随步骤Encoding.UTF8.GetBytes
几乎肯定会破坏数据and/or丢失字节。
相反,您应该使用不同的方法将字节数组转换为字符串并返回。
一种方法是使用 Base64 编码,您可以将上面的两行替换为:
string passe = Convert.ToBase64String(encrypted);
...
string decrypted = Decrypt(Convert.FromBase64String(passe), aes.Key, aes.IV);
这将导致您的程序加密然后解密字符串就好了。
此外,您可能想考虑将字节直接存储到数据库中。根据您的数据库引擎,可能会很好地支持直接存储字节数组,而无需进行任何类型的字符串转换。
我正在尝试加密和解密一个字符串,我可以成功完成。之后,我将加密的byte[]
转换为字符串保存在数据库中。
但是当我再次尝试将字符串转换回 byte[]
时,我的代码会抛出错误。
为了测试代码,我创建了一个字符串变量来保存加密的字符串。
the print error
如何将字符串再次成功转换回 byte[]
?
static void Main(string[] args)
{
Console.WriteLine("Enter text that needs to be encrypted..");
string data = Console.ReadLine();
EncryptAesManaged(data);
Console.ReadLine();
}
static void EncryptAesManaged(string raw)
{
string EncryptionKey = "sruohfaymonerishfiahbihbgrG546";
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(raw);
try
{
using (AesManaged aes = new AesManaged())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(EncryptionKey);
aes.Key = pdb.GetBytes(32);
aes.IV = pdb.GetBytes(16);
// Encrypt string
byte[] encrypted = Encrypt(raw, aes.Key, aes.IV);
// Print encrypted string
string passe = System.Text.Encoding.UTF8.GetString(encrypted);
Console.WriteLine($"Encrypt:{passe}");
Console.WriteLine(System.Text.Encoding.UTF8.GetBytes(passe));
// Decrypt the bytes to a string.
string decrypted = Decrypt(System.Text.Encoding.UTF8.GetBytes(passe), aes.Key, aes.IV);
// Print decrypted string. It should be same as raw data
Console.WriteLine($"Decrypted data: {decrypted}");
}
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}
Console.ReadKey();
}
static byte[] Encrypt(String plainText, byte[] Key, byte[] IV)
{
byte[] encrypted;
using (AesManaged aes = new AesManaged())
{
// Create encryptor
ICryptoTransform encryptor = aes.CreateEncryptor(Key, IV);
// Create MemoryStream
using (MemoryStream ms = new MemoryStream())
{
// Create crypto stream using the CryptoStream class. This class is the key to encryption
// and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream
// to encrypt
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
// Create StreamWriter and write data to a stream
using (StreamWriter sw = new StreamWriter(cs))
sw.Write(plainText);
encrypted = ms.ToArray();
}
}
}
return encrypted;
}
static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
string plaintext = null;
// Create AesManaged
using (AesManaged aes = new AesManaged())
{
// Create a decryptor
ICryptoTransform decryptor = aes.CreateDecryptor(Key, IV);
// Create the streams used for decryption.
using (MemoryStream ms = new MemoryStream(cipherText))
{
// Create crypto stream
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
// Read crypto stream
using (StreamReader reader = new StreamReader(cs))
plaintext = reader.ReadToEnd();
}
}
}
return plaintext;
}
问题就在这里:
string passe = System.Text.Encoding.UTF8.GetString(encrypted);
^---------------------------------^
然后在这里:
string decrypted = Decrypt(System.Text.Encoding.UTF8.GetBytes(passe), aes.Key, aes.IV);
^--------------------------------^
我假设这是您将其存储到数据库然后取回并将其存储为字符串的模拟。
但是,您所做的几乎肯定会以这种方式损坏字符串。
Encoding.UTF8.GetString(bytes)
不会 将包含任意字节的字节数组转换为字符串。相反,它将字节数组 应该 包含组成 UTF8 编码字符串的字节返回到该字符串。
如果字节数组包含任意字节,例如加密文本的结果,此步骤及其伴随步骤Encoding.UTF8.GetBytes
几乎肯定会破坏数据and/or丢失字节。
相反,您应该使用不同的方法将字节数组转换为字符串并返回。
一种方法是使用 Base64 编码,您可以将上面的两行替换为:
string passe = Convert.ToBase64String(encrypted);
...
string decrypted = Decrypt(Convert.FromBase64String(passe), aes.Key, aes.IV);
这将导致您的程序加密然后解密字符串就好了。
此外,您可能想考虑将字节直接存储到数据库中。根据您的数据库引擎,可能会很好地支持直接存储字节数组,而无需进行任何类型的字符串转换。