Created
March 1, 2012 13:39
-
-
Save dealforest/1949873 to your computer and use it in GitHub Desktop.
AES256 encryption on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
"Unsupported key size: 8 bytes". how to solve this @dealforest
@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.
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
http://blog.dealforest.net/2012/03/ios-android-per-aes-crypt-connection/