encryption - Java BouncyCastle Cast6Engine (CAST-256) encrypting -
i'm trying implement function receives string , returns encoded values of string in cast-256. following code implement following example on boncycastle official web page (http://www.bouncycastle.org/specifications.html , point 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); } }
but , when run code
quryzmvlbx3ok6ikxwq1ng==
and if encode hola
in cast-256 same key ( try here if want http://www.tools4noobs.com/online_tools/encrypt/) should
w5nzsyeya8hupl5v0j29yg==
.
what happening? why im getting wront encrypted string?
i'm tired of find on internet , didnt find answer.
bouncy castle uses pkcs #7 padding default, while php's mcrypt (and web site linked) uses 0 padding default. causes different ciphertexts.
please note ecb mode used here not secure use. additionally, hope secret key posted not real key, because it's not secret anymore, encryption useless.
Comments
Post a Comment