我希望将数字字符串加密,然后解密并以ASCII格式返回。 但是在下面的静态方法中,在解密块中它给出了一个错误,因为长度无效。我的字符串应该像这样以xx-xx-xx的代码生成,每2位数字后会插入一个hypen,但是无法解密,请问要解密的数据长度是否无效?
如何解决错误,请帮忙!
以下是我的代码:- encryptionText = 9007315596089,key ='This Is MySecure',iv ='Encrption'到以下方法
private static string Decrypt(string encryptedText, string key, string iv)
{
//encryptedText = encryptedText.Insert(2, "-");
int length = encryptedText.Length;
byte[] keyBytes = getKeyBytes(key);
byte[] ivBytes = getIVBytes(iv);
string encrytedTextNew = "";
//for (int i = 2; i < encrytedTextNew.Length - 1; i += 3)
//{
// if (encrytedTextNew.Count(c => c == '-') >= 3) i++;
// encrytedTextNew = encrytedTextNew.Insert(i, "-");
//}
char[] encrytArray = encryptedText.ToCharArray(0, encryptedText.Length);
for (int i = 0; i < encryptedText.Length; i++)
{
if (i != 0)
{
int j = i + 1;
//if (j == 14)
//{
// encrytedTextNew = encrytedTextNew +"-";
// break;
//}
if (j % 2 == 0)
{
encrytedTextNew = encrytedTextNew + encrytArray[i] + "-";
}
else
{
encrytedTextNew = encrytedTextNew + encrytArray[i];
}
}
else if (i == 0)
{
encrytedTextNew = encrytedTextNew + encrytArray[i];
}
}
encrytedTextNew = encrytedTextNew.Remove(encrytedTextNew.Length-1);
//encrytedTextNew = encrytedTextNew + "7";
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.CBC;
aes.Key = keyBytes;
aes.IV = ivBytes;
ICryptoTransform encrypto = aes.CreateDecryptor();
byte[] plainTextByte = Array.ConvertAll<string, byte>(encrytedTextNew.Split('-'), s => Convert.ToByte(s, 16));
int len = plainTextByte.Length;
**byte[] CipherText = encrypto.TransformFinalBlock(plainTextByte,0, plainTextByte.Length - 1);**
return ASCIIEncoding.UTF8.GetString(CipherText);
}