我正在学习算法,我编写了一个带有Rijndae加密的控制台项目,具有给定的IV和Key。
由于它不会对给定的字符串进行任何更改,因此它实际上不起作用,无论您输入什么,它都会输出相同的字符串。
有人可以告诉我或引导我走出低谷吗?我真的不知道我在哪里做错
提前谢谢
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace RijndaelManage
{
class Rijndael
{
public static void Main()
{
try
{
string original = "input data here";
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
byte[] encrypted = EncryptStringToBytes(original);
string aftdecryp = DecryptStringFromBytes(encrypted);
Console.WriteLine("Original: {0}", original);
Console.WriteLine("After Decryption: {0}", aftdecryp);
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
public static byte[] EncryptStringToBytes(string plainText)
{
byte[] encrypted;
if (plainText == null)
{
goto TR_0000;
}
else if (plainText.Length > 0)
{
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
string s = "NigeroTomoyo!@#$";
string str2 = "8321947192739743";
rijAlg.KeySize = 0x80;
rijAlg.Mode = CipherMode.CBC;
rijAlg.Key = Encoding.UTF8.GetBytes(s);
rijAlg.IV = Encoding.UTF8.GetBytes(str2);
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(csEncrypt))
{
writer.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
}
else
{
goto TR_0000;
}
return encrypted;
TR_0000:
throw new ArgumentNullException("plainText");
}
static string DecryptStringFromBytes(byte[] cipherText)
{
string plaintext;
if (cipherText == null)
{
goto TR_0000;
}
else if (cipherText.Length > 0)
{
plaintext = null;
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
string s = "NigeroTomoyo!@#$";
string str3 = "8321947192739743";
rijAlg.KeySize = 0x80;
rijAlg.Mode = CipherMode.CBC;
rijAlg.Key = Encoding.UTF8.GetBytes(s);
rijAlg.IV = Encoding.UTF8.GetBytes(str3);
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.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();
}
}
}
}
}
else
{
goto TR_0000;
}
return plaintext;
TR_0000:
throw new ArgumentNullException("cipherText");
}
}
}