我修改了这个网站的实现,以包含一个salt:
package de.xxx.yyy.main;
import static org.junit.Assert.*;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.digests.SHA3Digest;
import org.bouncycastle.crypto.engines.RijndaelEngine;
import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Base64;
import org.junit.Test;
public class test {
private char[] password = "0123456789abcdef0123456789abcdef".toCharArray();
private byte[] salt = "0123456789".getBytes();
private int iterationCount = 5;
@Test
public void testEncryptRijndael() throws DataLengthException, IllegalStateException, InvalidCipherTextException {
PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA3Digest(256));
char[] passwordChars = password;
final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
pGen.init(pkcs12PasswordBytes, salt , iterationCount );
BlockCipher engine = new RijndaelEngine(256);
CBCBlockCipher cbc = new CBCBlockCipher(engine);
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbc, new PKCS7Padding());
ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 256);
cipher.init(true, aesCBCParams);
byte[] input = "Hallo ich bin ein Test".getBytes();
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int cipherLength = cipher.processBytes(input, 0, input.length, cipherText, 0);
cipher.doFinal(cipherText, cipherLength);
String result = new String(Base64.encode(cipherText));
System.out.println("testEncryptRijndael result : " + result);
assertEquals("cMoMSNMNsikAkLjaheE6iD48Xkfvo7Y6gS8/zroGfHc=",result);
}
@Test
public void testDecryptRijndael() throws DataLengthException, IllegalStateException, InvalidCipherTextException {
PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA3Digest(256));
char[] passwordChars = password;
final byte[] pkcs12PasswordBytes = PBEParametersGenerator.PKCS12PasswordToBytes(passwordChars);
pGen.init(pkcs12PasswordBytes, salt , iterationCount );
BlockCipher engine = new RijndaelEngine(256);
CBCBlockCipher cbc = new CBCBlockCipher(engine);
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbc, new PKCS7Padding());
ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 256);
cipher.init(false, aesCBCParams);
byte[] output = Base64.decode("cMoMSNMNsikAkLjaheE6iD48Xkfvo7Y6gS8/zroGfHc=".getBytes());
byte[] cipherText = new byte[cipher.getOutputSize(output.length)];
int cipherLength = cipher.processBytes(output, 0, output.length, cipherText, 0);
int outputLength = cipher.doFinal(cipherText, cipherLength);
outputLength += cipherLength;
byte[] resultBytes = cipherText;
if (outputLength != output.length) {
resultBytes = new byte[outputLength];
System.arraycopy(
cipherText, 0,
resultBytes, 0,
outputLength
);
}
String result = new String(resultBytes);
System.out.println("testDecryptRijndael result : " + result);
assertEquals("Hallo ich bin ein Test", result);
}
}这是一个很好的实现,还是存在一些错误、逻辑错误或安全风险?
发布于 2014-08-28 07:01:59
您使用了更有趣的测试字符串,而不是"value",这很好,而且在testEncryptRijndael中,您用更严格的assertEquals代替了assertNotNull。
由于您的测试与原始测试不同,因此最好将它们分别重命名为testEncryptRijndaelWithSalt和testDecryptRijndaelWithSalt。把原件也保留下来。
在原始测试和您的测试中,我都看不到日志记录和打印语句的意义。我想你可以放下这个。它们没有坏处,但它们可能会导致阅读测试输出的坏习惯,而不是专注于添加正确的断言。
在加密和解密的情况下有很多重复。也许您可以将该逻辑移到公共的私有助手方法中。这可以减少可能的键入错误。
我不认为这个if块有什么意义:
if (outputLength != output.length) { // .}
问题是,在单元测试中,一切都应该是可预测的:此时您应该已经知道该条件的结果。如果outputLength应该等于output.length,那么删除if,但保留代码块。否则就把整件事删掉。您可以为条件添加一个断言,作为正常检查。
https://codereview.stackexchange.com/questions/61294
复制相似问题