Encriptação Java BouncyCastle Cast6Engine (CAST-256)
Estou tentando implementar uma função que recebe uma string e retorna os valores codificados da String no CAST-256. O código a seguir é o que eu implemento seguindo o exemplo na página oficial do BoncyCastle (http://www.bouncycastle.org/specifications.html ponto 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);
}
}
Mas, quando eu corro meu código eu recebo
QUrYzMVlbx3OK6IKXWq1ng==
e se você codificarhola
no CAST-256 com a mesma chave (tente aqui se você quiserhttp://www.tools4noobs.com/online_tools/encrypt/eu deveria pegar
w5nZSYEyA8HuPL5V0J29Yg==
.
O que está acontecendo? Por que estou recebendo uma string criptografada wront?
Estou cansado de encontrar isso na internet e não encontrei uma resposta.