I need to convert a JCE based code to a Bouncy Castle based code. I'm completely new to Bouncy Castle and couldn't find a easy-to-understand introduction to this topic in general or my issue specifically. This is the JCE based class:
I need to convert a JCE based code to a Bouncy Castle based code. I'm completely new to Bouncy Castle and couldn't find a easy-to-understand introduction to this topic in general or my issue specifically. This is the JCE based class: [code]import java.security.NoSuchAlgorithmException; import java.security.Security;
[/code] ... And this is the Bouncy Castle based class: [code]import org.bouncycastle.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.engines.BlowfishEngine; import org.bouncycastle.crypto.paddings.PKCS7Padding; import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; import org.bouncycastle.crypto.params.KeyParameter;
public class BouncyCastleBlowfishEncrypterDecrypter {
public static String crypt(String msg, String key) throws Exception { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new BlowfishEngine(), new PKCS7Padding()); cipher.init(true, new KeyParameter(key.getBytes())); byte[] inputAsBytes = msg.getBytes(); byte[] encryptedAsBytes = new byte[cipher.getOutputSize(inputAsBytes.length)]; int numberOfBytesCopiedOnEncryptedAsBytes = cipher.processBytes(inputAsBytes, 0, inputAsBytes.length, encryptedAsBytes, 0); cipher.doFinal(encryptedAsBytes, numberOfBytesCopiedOnEncryptedAsBytes); return Hex.byte2hex(encryptedAsBytes); }
public static String decrypt(String msg, String key) throws Exception { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new BlowfishEngine(), new PKCS7Padding()); cipher.init(false, new KeyParameter(key.getBytes())); byte[] outputConvertedAsBytes = Hex.hex2byte(msg); byte[] decryptedAsBytes = new byte[cipher.getOutputSize(outputConvertedAsBytes.length)]; int numberOfBytesCopiedOnDecryptedAsBytes = cipher.processBytes(outputConvertedAsBytes, 0, outputConvertedAsBytes.length, decryptedAsBytes, 0); cipher.doFinal(decryptedAsBytes, numberOfBytesCopiedOnDecryptedAsBytes); return new String(decryptedAsBytes); } }
[/code] ... And this is the main method: [code] public static void main(String[] args) throws Exception { String encrypted = JCEBlowfishEncrypterDecrypter.crypt("Test", "mmTSQOFzSL9xAwXGLMEe1Q=="); String decrypted = JCEBlowfishEncrypterDecrypter.decrypt(encrypted, "mmTSQOFzSL9xAwXGLMEe1Q==");