AES encrypt/decrypt with Bouncy Castle provider
43,936
You would either use
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES", "BC");
or else
Cipher cipher = Cipher.getInstance("AES", new BouncyCastleProvider());
That said, Cipher.getInstance("AES")
uses Electronic Codebook, which is insecure. You either want Cipher Block Chaining (Cipher.getInstance("AES/CBC/PKCS5Padding")
) or Counter (Cipher.getInstance("AES/CTR/NoPadding")
) modes; they are both secure, the primary difference being that CBC requires padding while CTR does not.
Comments
-
fikouRaf about 4 years
Here is my implementation of a AES 256 encrypt and decrypt, developed with the native library of JDK 5:
public static String encrypt(String key, String toEncrypt) throws Exception { Key skeySpec = generateKeySpec(key); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(toEncrypt.getBytes()); byte[] encryptedValue = Base64.encodeBase64(encrypted); return new String(encryptedValue); } public static String decrypt(String key, String encrypted) throws Exception { Key skeySpec = generateKeySpec(key); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decodedBytes = Base64.decodeBase64(encrypted.getBytes()); byte[] original = cipher.doFinal(decodedBytes); return new String(original); }
I want to implement the same methods with the Boucy Castle API (Java): I've searched a lot, tested a lot, without results ... can someone help me?
Thanks
-
Perception about 11 yearsYou do realize that you will be using the same API, but just a different provider right? Anyways, read here.
-
Robert about 11 yearsNote that AES256 is by default disabled in all Java versions from Oracle. You have to install the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for Java 5. BTW: Java 5 is outdated and insecure. Don't use it anymore.
-
fikouRaf about 11 yearsI know that Robert, thanks. Perception, i want to use Boucny Castle because it's more portable thant the native lib of Java (only from JDK 6+)
-
LateralFractal over 10 yearsYes, this is a duplicate but the original has dreadfully sparse answers.
-
ArifMustafa about 6 yearsSure, it is duplicate but the scenario is different.
-