我制作的加密类有问题:
public static class Encryption {
public static string EncryptToString(string TextToEncrypt, byte[] Key, byte[] IV = null)
{
return ByteArrToString(EncryptStringToBytes(StrToByteArray(TextToEncrypt), Key, IV));
}
public static string EncryptToString(byte[] BytesToEncrypt, byte[] Key, byte[] IV = null)
{
return ByteArrToString(EncryptStringToBytes(BytesToEncrypt, Key, IV));
}
public static byte[] EncryptToBytes(string TextToEncrypt, byte[] Key, byte[] IV = null)
{
return EncryptStringToBytes(StrToByteArray(TextToEncrypt), Key, IV);
}
public static byte[] EncryptToBytes(byte[] BytesToEncrypt, byte[] Key, byte[] IV = null)
{
return EncryptStringToBytes(BytesToEncrypt, Key, IV);
}
public static string DecryptToString(string EncryptedText, byte[] Key,byte[] IV=null)
{
return ByteArrToString(DecryptStringFromBytes(StrToByteArray(EncryptedText), Key, IV));
}
public static string DecryptToString(byte[] EncryptedBytes, byte[] Key,byte[] IV=null)
{
return ByteArrToString(DecryptStringFromBytes(EncryptedBytes, Key, IV));
}
public static byte[] DecryptToBytes(string EncryptedText, byte[] Key,byte[] IV=null)
{
return DecryptStringFromBytes(StrToByteArray(EncryptedText), Key, IV);
}
public static byte[] DecryptToBytes(byte[] EncryptedBytes, byte[] Key,byte[] IV=null)
{
return DecryptStringFromBytes(EncryptedBytes, Key, IV);
}
private static byte[] EncryptStringToBytes(byte[] TextToEncrypt, byte[] Key, byte[] IV=null)
{
Debug.WriteLine("Password: " + ByteArrToString(Key));
Debug.WriteLine("IV: " + ByteArrToString(IV));
byte[] encrypted;
// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.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))
{
//Write all data to the stream.
swEncrypt.Write(TextToEncrypt);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
private static byte[] DecryptStringFromBytes(byte[] EncryptedText, byte[] Key, byte[] IV)
{
Debug.WriteLine("Password: " + ByteArrToString(Key));
Debug.WriteLine("IV: " + ByteArrToString(IV));
byte[] fromEncrypt = new byte[EncryptedText.Length];
// Create a Rijndael object with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(EncryptedText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
//read stream into byte array
csDecrypt.Read(fromEncrypt,0,fromEncrypt.Length);
}
}
}
return fromEncrypt;
}
public static byte[] StrToByteArray(string str)
{
if (str.Length == 0)
throw new Exception("Invalid string value in StrToByteArray");
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static string ByteArrToString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
public static byte[] GetIV()
{
byte[] randomArray = new byte[16];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomArray);
return randomArray;
}
}
我用以下方法测试它:
byte[] iv = Encryption.GetIV();
byte[] password = Encryption.StrToByteArray("password");
string encrypted = Encryption.EncryptToString("Hello", password, iv);
Debug.WriteLine("Result: " + Encryption.DecryptToString(encrypted, password, iv));
这是我在调试窗口中得到的结果:
密码:密码
四:䴞ㆫ튾辅
密码:密码
四:䴞ㆫ튾辅
结果:祓瑳浥䈮瑹孥]
我没有收到任何错误;只是一个乱七八糟的结果。
我不知道这是初始化向量、流还是我缺少的其他东西的问题。