Android Java Generate Aes Key 256

Android Java Generate Aes Key 256 Average ratng: 3,9/5 3207 votes

This is a utility to encrypt/decrypt using AES/CBC/PKCS5Padding algorithm

  • Most common error_: 'Invalid Key Size' error is most likely caused by not updating JCE strength policy, see above
  • https://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parametersNOTE: This example is built using JDK8, ultimate strength JCE (JDK8) and Maven 3.x

/online-license-key-generator-from-registration-id.html. git clone https://github.com/quantum-fusion/aes-256-encryption-utility

AES-256 Encryption with Java and JCEKS. The AES key is nothing more than a. The beginning of this post has shown how easy it is to create new AES-256 keys that reference an alias inside of a. Sep 16, 2019  AES Encryption/Decryption on Android's Java. GitHub Gist: instantly share code, notes, and snippets.

cd aes-256-encryption-utility

mvn clean install

- Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8 Download

- JDK must have the unlimited strength policy for the JDK version

Extract the jar files from the zip and save them in ${java.home}/jre/lib/security/.

mvn clean test

keytool -genseckey -alias jceksaes -keyalg AES -keysize 256 -storetype JCEKS -keypass mykeypass -storetype jceks -keystore aes-

keystore.jck -storepass mystorepass

keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias

mvn clean package // generate executable JAR filejava -Dkeystore=main-aes-keystore.jck -Dstorepass=mystorepass -Dalias=jceksaes -Dkeypass=mykeypass -jar target/example-encryption-util.jar <COMMAND like 'showKey'>

// or optionally with Maven (using the above defaults)mvn exec:java

https://keenview.weebly.com/blog/layering-auto-tune-vocal-phase. // Generate executable JAR with: mvn package

// Ideally the IV passed in (0000000000000000) would be randomly generatedjava -Dkeystore=main-aes-keystore.jck -Dstorepass=mystorepass -Dalias=jceksaes -Dkeypass=mykeypass -jar target/example-encryption-util.jar encrypt blahblahblah 0000000000000000

java -Dkeystore=main-aes-keystore.jck -Dstorepass=mystorepass -Dalias=jceksaes -Dkeypass=mykeypass -jar target/example-encryption-util.jar decrypt baN3CIAcVgq+AQr7lvKmLw 0000000000000000

java -Dkeystore=main-aes-keystore.jck -Dstorepass=mystorepass -Dalias=jceksaes -Dkeypass=mykeypass -jar target/example-encryption-util.jar encrypt blahblahblah 0000000000000001

java -Dkeystore=main-aes-keystore.jck -Dstorepass=mystorepass -Dalias=jceksaes -Dkeypass=mykeypass -jar target/example-encryption-util.jar decrypt Wcaov8LNN4GJvp1bvOTJ0g 0000000000000001

Android integration frameworks (See https://www.whatsapp.com/security/WhatsApp-Security-Whitepaper.pdf )

Generate Aes 256 Key Java

AESCrypt Android (https://github.com/quantum-fusion/AESCrypt-Android)

Whisper Systems Android encrypt (https://github.com/quantum-fusion/libsignal-service-java)

quantum-fusion Copyright 2018 - Use of this code and it's concepts are considered a Proof-of-concept and should not be used directly in any environment

AESEnDecryption.java

Java Aes 256 Example

importjavax.crypto.KeyGenerator;
importjavax.crypto.SecretKey;
importjavax.crypto.Cipher;
importjavax.crypto.spec.SecretKeySpec;
importjavax.crypto.spec.IvParameterSpec;
importjava.security.MessageDigest;
importjava.security.spec.AlgorithmParameterSpec;
importandroid.util.Base64;
publicclassAESEnDecryption {
/*
//Sample Code
public static void main(String[] args) throws Exception{
String msg = '123456';
String keyStr = 'abcdef';
String ivStr = 'ABCDEF';
byte[] msg_byte = msg.getBytes('UTF-8');
System.out.println('Before Encrypt: ' + msg);
byte[] ans = AESEnDecryption.encrypt(ivStr, keyStr, msg.getBytes());
System.out.println('After Encrypt: ' + new String(ans, 'UTF-8'));
String ansBase64 = AESEnDecryption.encryptStrAndToBase64(ivStr, keyStr, msg);
System.out.println('After Encrypt & To Base64: ' + ansBase64);
byte[] deans = AESEnDecryption.decrypt(ivStr, keyStr, ans);
System.out.println('After Decrypt: ' + new String(deans, 'UTF-8'));
String deansBase64 = AESEnDecryption.decryptStrAndFromBase64(ivStr, keyStr, ansBase64);
System.out.println('After Decrypt & From Base64: ' + deansBase64);}
*/
publicstaticbyte[] encrypt(StringivStr, StringkeyStr, byte[] bytes) throwsException{
MessageDigest md =MessageDigest.getInstance('MD5');
md.update(ivStr.getBytes());
byte[] ivBytes = md.digest();
MessageDigest sha =MessageDigest.getInstance('SHA-256');
sha.update(keyStr.getBytes());
byte[] keyBytes = sha.digest();
return encrypt(ivBytes, keyBytes, bytes);
}
staticbyte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] bytes) throwsException{
AlgorithmParameterSpec ivSpec =newIvParameterSpec(ivBytes);
SecretKeySpec newKey =newSecretKeySpec(keyBytes, 'AES');
Cipher cipher =Cipher.getInstance('AES/CBC/PKCS5Padding');
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(bytes);
}
publicstaticbyte[] decrypt(StringivStr, StringkeyStr, byte[] bytes) throwsException{
MessageDigest md =MessageDigest.getInstance('MD5');
md.update(ivStr.getBytes());
byte[] ivBytes = md.digest();
MessageDigest sha =MessageDigest.getInstance('SHA-256');
sha.update(keyStr.getBytes());
byte[] keyBytes = sha.digest();
return decrypt(ivBytes, keyBytes, bytes);
}
staticbyte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] bytes) throwsException{
AlgorithmParameterSpec ivSpec =newIvParameterSpec(ivBytes);
SecretKeySpec newKey =newSecretKeySpec(keyBytes, 'AES');
Cipher cipher =Cipher.getInstance('AES/CBC/PKCS5Padding');
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(bytes);
}
publicstaticStringencryptStrAndToBase64(StringivStr, StringkeyStr, StringenStr) throwsException{
byte[] bytes = encrypt(keyStr, keyStr, enStr.getBytes('UTF-8'));
returnnewString(Base64.encode(bytes ,Base64.DEFAULT), 'UTF-8');
}
publicstaticStringdecryptStrAndFromBase64(StringivStr, StringkeyStr, StringdeStr) throwsException{
byte[] bytes = decrypt(keyStr, keyStr, Base64.decode(deStr.getBytes('UTF-8'),Base64.DEFAULT));
returnnewString(bytes, 'UTF-8');
}
}
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment