在调试C#应用程序的性能时,我注意到它比Java等效要慢得多。经过研究,问题似乎是由加密/解密方法引起的。
我被迫在模式设置为CFB8且没有填充的情况下使用AES加密。对于Java,这很简单,因为我可以使用Cipher.getInstance("AES/CFB8/NoPadding");
。在C#中,我发现我需要使用new RijndaelManaged()
。使用相同的键和相同的数据运行测试后,以下是结果:
Java:
C#:
C#密码:
public ICryptoTransform enc;
public ICryptoTransform dec;
public AesCrypto(byte[] key)
{
enc = Generate(key).CreateEncryptor();
dec = Generate(key).CreateDecryptor();
}
private SymmetricAlgorithm Generate(byte[] key) {
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;
cipher.KeySize = 128;
cipher.FeedbackSize = 8;
cipher.Key = key;
cipher.IV = key;
return cipher;
}
public byte[] Crypt(byte[] buffer, int offset, int count) {
return enc.TransformFinalBlock(buffer, offset, count);
}
C#测试代码:
static void Test() {
// Init
var AesCrypto = new AesCrypto(Encoding.UTF8.GetBytes("aaabbbccaaabbbcc"));
var testData = Encoding.UTF8.GetBytes(createDataSize(9000000)); // 9mb test.
// Timer
var stopWatch = new Stopwatch();
stopWatch.Start();
AesCrypto.Crypt(testData, 0, testData.Length);
stopWatch.Stop();
Console.WriteLine("AesCrypto.Crypt took: " + stopWatch.ElapsedMilliseconds);
}
static string createDataSize(int msgSize)
{
StringBuilder sb = new StringBuilder(msgSize);
for (int i = 0; i < msgSize; i++)
{
sb.Append('a');
}
return sb.ToString();
}
结果:“ AesCrypto.Crypt占用了:3626”
JAVA密码:
public Cryptor(boolean reader) throws CryptingException {
keySpec = new SecretKeySpec(secretKey.getBytes(CHARSET), "AES");
ivSpec = new IvParameterSpec(iv.getBytes(CHARSET));
try {
cipher = Cipher.getInstance("AES/CFB8/NoPadding");
if (reader) cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
else cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
} catch (NoSuchAlgorithmException e) {
throw new SecurityException(e);
} catch (NoSuchPaddingException e) {
throw new SecurityException(e);
}catch (InvalidKeyException e) {
throw new SecurityException(e);
} catch (InvalidAlgorithmParameterException e) {
throw new SecurityException(e);
}
}
public byte[] decrypt(byte[] input) throws CryptingException {
return cipher.update(input);
}
public byte[] encrypt(String input) throws CryptingException {
return cipher.update(input.getBytes());
}
Java测试代码:
private static void Test() {
// Init
String data = createDataSize(9392963);
Cryptor writer = new Cryptor(false);
// Timer
Instant starts = Instant.now();
byte[] encrypted = writer.encrypt(data);
Instant ends = Instant.now();
System.out.println("Java Encryption took: " + Duration.between(starts, ends));
}
private static String createDataSize(int msgSize) {
StringBuilder sb = new StringBuilder(msgSize);
for (int i=0; i<msgSize; i++) {
sb.append('a');
}
return sb.toString();
}
结果:“ Java加密使用:PT0.469S”
经过大量研究之后,看来AesCryptoServiceProvider()
的性能与Java的等效性能大致相同,并且结果大部分相同。但是,与此相关的问题是它需要填充,而Java的等效性不需要填充。例如,这意味着如果我要加密“ abcdab”,它将仅加密“ abcd”并返回该结果,并将其余结果(“ ab”)保留在内部。如果我使用填充,我可以让它返回完整的加密“ abcdab”,但是它会附加其他数据,并且对称算法将不同步,因为在Java中,我可以加密“ abcdab”而无需任何填充。
所以最后我的问题是,我将如何使C#加密/解密过程与Java一样快?我在AesCryptoServiceProvider
上做错了什么,也许它可能不需要填充?