Encriptación Java BouncyCastle Cast6Engine (CAST-256)
Estoy intentando implementar una función que recibe una cadena y devuelve los valores codificados de la cadena en CAST-256. El siguiente código es lo que implemento siguiendo el ejemplo en la página web oficial de BoncyCastle (http://www.bouncycastle.org/specifications.html , punto 4.1).
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.engines.CAST6Engine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
public class Test {
static{
Security.addProvider(new BouncyCastleProvider());
}
public static final String UTF8 = "utf-8";
public static final String KEY = "CLp4j13gADa9AmRsqsXGJ";
public static byte[] encrypt(String inputString) throws UnsupportedEncodingException {
final BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CAST6Engine());
byte[] key = KEY.getBytes(UTF8);
byte[] input = inputString.getBytes(UTF8);
cipher.init(true, new KeyParameter(key));
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int outputLen = cipher.processBytes(input, 0, input.length, cipherText, 0);
try {
cipher.doFinal(cipherText, outputLen);
} catch (CryptoException ce) {
System.err.println(ce);
System.exit(1);
}
return cipherText;
}
public static void main(String[] args) throws UnsupportedEncodingException {
final String toEncrypt = "hola";
final String encrypted = new String(Base64.encode(test(toEncrypt)),UTF8);
System.out.println(encrypted);
}
}
Pero, cuando ejecuto mi código me sale
QUrYzMVlbx3OK6IKXWq1ng==
y si codificashola
en CAST-256 con la misma clave (intente aquí si quierehttp://www.tools4noobs.com/online_tools/encrypt/) debería obtener
w5nZSYEyA8HuPL5V0J29Yg==
.
¿Que esta pasando? ¿Por qué estoy consiguiendo una cadena cifrada sin errores?
Estoy cansado de encontrar eso en internet y no encontré una respuesta.