Skip to content

Instantly share code, notes, and snippets.

@hitesh-dhamshaniya
Created June 22, 2017 06:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitesh-dhamshaniya/38a52196427fe3f36f1c03274019399f to your computer and use it in GitHub Desktop.
Save hitesh-dhamshaniya/38a52196427fe3f36f1c03274019399f to your computer and use it in GitHub Desktop.
AESHelper for Encrypt and decrypt
package com.utils;
import android.util.Base64;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.SecretKeySpec;
/**
* CryptographyWrapper class
*/
public class AESHelper {
private final String encryptionKey;
private byte[] keyBytes;
private Cipher cipher;
private SecretKeySpec key;
public AESHelper(String encryptionKey) {
this.encryptionKey = encryptionKey;
keyBytes = encryptionKey.getBytes();
key = new SecretKeySpec(keyBytes, "AES");
try {
cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
/**
* Encrypt Method
*
* @param plainText text to be encrypted
* @return encryptedText
*/
public String encrypt(String plainText) {
String encryptedText = plainText;
if (!plainText.trim().equalsIgnoreCase("") || encryptedText.length() != 0) {
byte[] cipherText = encryptData(plainText.getBytes());
encryptedText = Base64.encodeToString(cipherText, Base64.DEFAULT);
}
return encryptedText;
}
/**
* Decrypt method
*
* @param encryptedText encrypted text
* @return plain text
*/
public String decrypt(String encryptedText) {
String decryptedText = encryptedText;
try {
if (!encryptedText.equals("") || !encryptedText.equals("0")) {
byte[] encryptedTextByte = Base64.decode(encryptedText, Base64.DEFAULT);
decryptedText = decryptData(encryptedTextByte);
Logger.debug(AESHelper.class.getSimpleName(), decryptedText);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return decryptedText;
}
/**
* Method used to encryptiong
* return value is without Base64 encoding.
*
* @param plainText
* @return encrypted Value
*/
private byte[] encryptData(byte[] plainText) {
byte[] encryptedValue = null;
try {
// encryptData pass
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] input = plainText;
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
encryptedValue = cipherText;
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (ShortBufferException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return encryptedValue;
}
/**
* Private method to decrypt data
*
* @param cipherText base64 decoded value
* @return decryptedValue
*/
private String decryptData(byte[] cipherText) {
String decryptedValue = "";
try {
cipher.init(Cipher.DECRYPT_MODE, key);
int ctLength = cipherText.length;
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
decryptedValue = new String(plainText, 0, ptLength, "UTF-8");
return decryptedValue;
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (ShortBufferException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return decryptedValue;
}
/*public static String symmetricDecrypt(String text, String secretKey) {
Cipher cipher;
String encryptedString;
byte[] encryptText = null;
byte[] raw;
SecretKeySpec skeySpec;
try {
raw = com.devdigital.seenote.utils.binary.Base64.decodeBase64(_KEY);
skeySpec = new SecretKeySpec(raw, "AES");
encryptText = com.devdigital.seenote.utils.binary.Base64.decodeBase64(text);
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
encryptedString = new String(cipher.doFinal(encryptText));
} catch (Exception e) {
e.printStackTrace();
return "Error";
}
return encryptedString;
}*/
}
@hitesh-dhamshaniya
Copy link
Author

It's work for me, I had used it in Wine App

@Gamis214
Copy link

Gracias por la aportacion :)

@hitesh-dhamshaniya
Copy link
Author

@Gamis214, Glad to know that it helps you. Thank you

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