Created
February 9, 2019 08:56
-
-
Save DrBrad/f5c52fedf5b253b5ca13397b24b3a95f to your computer and use it in GitHub Desktop.
AES Encryption
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
import android.util.Base64; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.spec.InvalidKeySpecException; | |
import java.security.spec.KeySpec; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.PBEKeySpec; | |
import javax.crypto.spec.SecretKeySpec; | |
public class AES { | |
private static final int pswdIterations = 10; | |
private static final int keySize = 128; | |
private static final String cypherInstance = "AES/CBC/PKCS5Padding"; | |
private static final String secretKeyInstance = "PBKDF2WithHmacSHA1"; | |
public static String encrypt(String textToEncrypt, String initializationVector, String plainText, String AESSalt) throws Exception { | |
SecretKeySpec skeySpec = new SecretKeySpec(getRaw(plainText, AESSalt), "AES"); | |
Cipher cipher = Cipher.getInstance(cypherInstance); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(initializationVector.getBytes())); | |
byte[] encrypted = cipher.doFinal(textToEncrypt.getBytes()); | |
return Base64.encodeToString(encrypted, Base64.DEFAULT); | |
} | |
public static String decrypt(String textToDecrypt, String initializationVector, String plainText, String AESSalt) throws Exception { | |
byte[] encryted_bytes = Base64.decode(textToDecrypt, Base64.DEFAULT); | |
SecretKeySpec skeySpec = new SecretKeySpec(getRaw(plainText, AESSalt), "AES"); | |
Cipher cipher = Cipher.getInstance(cypherInstance); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(initializationVector.getBytes())); | |
byte[] decrypted = cipher.doFinal(encryted_bytes); | |
return new String(decrypted, "UTF-8"); | |
} | |
private static byte[] getRaw(String plainText, String salt){ | |
try{ | |
SecretKeyFactory factory = SecretKeyFactory.getInstance(secretKeyInstance); | |
KeySpec spec = new PBEKeySpec(plainText.toCharArray(), salt.getBytes(), pswdIterations, keySize); | |
return factory.generateSecret(spec).getEncoded(); | |
}catch(InvalidKeySpecException e){ | |
e.printStackTrace(); | |
}catch(NoSuchAlgorithmException e){ | |
e.printStackTrace(); | |
} | |
return new byte[0]; | |
} | |
} | |
//TO USE | |
//ENCRYPT | |
encrypt("ACTUAL STRING YOU WANT TO DECRYPT", "INIT VALIDATION KEY - WHAT EVER YOU WANT", "PLAIN TEXT - WHAT EVERY YOU WANT", "SALT - WHAT EVER YOU WANT"); | |
//DECRYPT | |
decrypt("HASH VALUE - AKA WHATEVER THE STRING FROM DECRYPT WAS", "INIT VALIDATION KEY - WHAT EVER YOU WANT", "PLAIN TEXT - WHAT EVERY YOU WANT", "SALT - WHAT EVER YOU WANT"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment