Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
AES256 encryption on Android
package net.dealforest.sample.crypt;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;
public class AES256Cipher {
public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes)
throws java.io.UnsupportedEncodingException,
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException {
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = null;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(textBytes);
}
public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes)
throws java.io.UnsupportedEncodingException,
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
InvalidAlgorithmParameterException,
IllegalBlockSizeException,
BadPaddingException {
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(textBytes);
}
}
@vamshi245

This comment has been minimized.

Show comment Hide comment
@vamshi245

vamshi245 Apr 5, 2012

can u please provide the length of the input parameters byte[] ivBytes, byte[] keyBytes, byte[] textBytes

can u please provide the length of the input parameters byte[] ivBytes, byte[] keyBytes, byte[] textBytes

@dealforest

This comment has been minimized.

Show comment Hide comment
@dealforest

dealforest Apr 9, 2012

I'm sorry for late reply
I have sample code at the bottom of the mime blog.
I think I can understand if you look at it.

http://blog.dealforest.net/2012/03/ios-android-per-aes-crypt-connection/

Owner

dealforest commented Apr 9, 2012

I'm sorry for late reply
I have sample code at the bottom of the mime blog.
I think I can understand if you look at it.

http://blog.dealforest.net/2012/03/ios-android-per-aes-crypt-connection/

@bradley-curran

This comment has been minimized.

Show comment Hide comment
@bradley-curran

bradley-curran Aug 14, 2014

What's the block size on that cipher? Is it 16 by any chance?

What's the block size on that cipher? Is it 16 by any chance?

@chrisdew

This comment has been minimized.

Show comment Hide comment
@chrisdew

chrisdew May 22, 2015

The function name says AES256, but the cipher string is just "AES/CBC/PKCS5Padding" - how does that work?

The function name says AES256, but the cipher string is just "AES/CBC/PKCS5Padding" - how does that work?

@jemshit

This comment has been minimized.

Show comment Hide comment
@jemshit

jemshit Jan 31, 2018

"Unsupported key size: 8 bytes". how to solve this @dealforest

jemshit commented Jan 31, 2018

"Unsupported key size: 8 bytes". how to solve this @dealforest

@TabraizAhmad

This comment has been minimized.

Show comment Hide comment
@TabraizAhmad

TabraizAhmad Feb 20, 2018

@dealforest the block size is of 16 byte, is there a way to make it 32byte? I prefer if u use AED128 instead of AES256 for this.

@dealforest the block size is of 16 byte, is there a way to make it 32byte? I prefer if u use AED128 instead of AES256 for this.

@Arsh5620

This comment has been minimized.

Show comment Hide comment
@Arsh5620

Arsh5620 Mar 12, 2018

You have been such a help, thanks!

You have been such a help, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment