I modified the implementation from this website to include a 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);
}
}
Is this a good implementation, or are there some mistakes, logical errors or security risks?