我最近转换之前的加密程序从Java到C#。解密函数工作正常(如使用Java加密字符串测试),但加密产生的结果是,无论是Java程序或C#程序可以进行解码。码和单元测试包含下面,与输出一起。信息getKey()函数中产生自己的盐可正确产生用于给定的输入(在Java和C#测试)相同的键键。
加密和解密功能是准确的对偶。我不明白为什么一个工程和其他没有。
我已经减少所有文本为base64避免Java和C#的迹象陷阱。插入解密函数了Java加密字符串(通过调试器),它工作得很好。加密的字符串失败无论是在解密和它的Java版本相同的错误解码
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
namespace Krypto
{
class Program
{
static void Main(string[] args)
{
string text;
string key;
string encrypText;
string decryptText;
Console.Write("Plese enter text to encrypt:");
text = Console.ReadLine();
Console.WriteLine("");
Console.Write("Plese enter Key:");
key = Console.ReadLine();
Console.WriteLine("");
encrypText = EncryptString(text, key);
Console.WriteLine("The encrypted string is: "+ encrypText);
decryptText = Decrypt(encrypText, key);
Console.WriteLine("The decrypted string is: " + decryptText);
if (text.Equals(decryptText)) Console.WriteLine("The test was sucessful");
else Console.WriteLine("The test failed!");
Console.ReadKey();
}
static string EncryptString(string text, string key)
{
byte[] key16 = getKey16(key);
string text64 = System.Convert.ToBase64String(System.Text.ASCIIEncoding.UTF8.GetBytes(text));
byte[] encrypted;
byte[] ivArr = { 1, 3, 3, 4, 5, 6, 6, 7, 4, 3, 2, 1, 7, 5, 5, 7 };
byte[] IVBytes16Value = new byte[16];
Array.Copy(ivArr, IVBytes16Value, 16);
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged aes = new RijndaelManaged())
{
aes.Key = key16;
aes.IV = IVBytes16Value;
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = aes.CreateEncryptor();
try
{
byte[] textBytes = Convert.FromBase64CharArray(text64.ToCharArray(), 0, text64.Length);
encrypted = encryptor.TransformFinalBlock(textBytes, 0, textBytes.Length);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
return "";
};
}
// Return the encrypted bytes from the memory stream.
return Convert.ToBase64String(encrypted);
}
private static string Decrypt(string CipherText, string key)
{
byte[] key16 = getKey16(key);
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
byte[] ivArr = { 1, 3, 3, 4, 5, 6, 6, 7, 4, 3, 2, 1, 7, 5, 5, 7 };
byte[] IVBytes16Value = new byte[16];
Array.Copy(ivArr, IVBytes16Value, 16);
aes.Key = key16;
aes.IV = IVBytes16Value;
ICryptoTransform decrypto = aes.CreateDecryptor();
byte[] decryptedData = null;
try
{
byte[] encryptedBytes = Convert.FromBase64CharArray(CipherText.ToCharArray(), 0, CipherText.Length);
decryptedData = decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
return "";
};
return System.Text.ASCIIEncoding.UTF8.GetString(decryptedData);
}
}
private static byte[] getKey16(string key)
{
byte[] key16 = new byte[16];
//make key a 64bit string
string base64key = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(key));
byte[] keyArr = Convert.FromBase64String(base64key);
int keyarraysize = keyArr.Count();
if (keyarraysize < 16)
{
int counter = 0;
while (keyarraysize * (counter + 1) < 16)
{
for (int i = 0; i < keyarraysize; ++i) key16[i + keyarraysize * counter] = keyArr[i];
++counter;
}
for (int i = keyarraysize * counter; i < 16; ++i)
{
key16[i] = 0;
}
}
else for (int i = 1; i < 16; ++i) key16[i] = keyArr[i];
return key16;
}
}
在加密和C#解密是准确的对偶。它应该只是产生一个正确的测试,而不是我得到下面的输出
请输入文本加密:这是一个测试
普莱舍ENTER键:密押
加密的字符串是:vLCR4QCJcVHvN4ss7H4Q2g ==错误:填充是无效的,不能删除。解密的字符串:测试失败!