Last active
June 20, 2018 17:18
-
-
Save abdullahicyc/6be5c85500d4c9952d9c79d8908439af to your computer and use it in GitHub Desktop.
Android Util class to perform encryption/decryption over strings
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 com.example.encryptiondemo; | |
import android.util.Log; | |
import java.io.IOException; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.NoSuchProviderException; | |
import java.security.SecureRandom; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
/** | |
* Util class to perform encryption/decryption over strings. <br/> | |
*/ | |
public final class UtilsEncryption { | |
private static final String TAG = UtilsEncryption.class.getName(); | |
/** | |
* Algorithm type | |
*/ | |
private static final String ALGORITHM = "AES"; | |
/** | |
* Avoid instantiation. <br/> | |
*/ | |
private UtilsEncryption() { | |
} | |
/** | |
* The HEX characters | |
*/ | |
private final static String HEX = "0123456789ABCDEF"; | |
/** | |
* Encrypt a given string. <br/> | |
* | |
* @param cleartext the string to encrypt | |
* @return the encrypted string in HEX | |
*/ | |
public static String encrypt(String secretKey, String cleartext) { | |
try { | |
byte[] result = process(secretKey, Cipher.ENCRYPT_MODE, cleartext.getBytes()); | |
return toHex(result); | |
} catch (Exception e) { | |
Log.e(TAG, "Encrypt: " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
/** | |
* Decrypt a HEX encrypted string. <br/> | |
* | |
* @param encrypted the HEX string to decrypt | |
* @return the decrypted string | |
*/ | |
public static String decrypt(String secretKey, String encrypted) { | |
try { | |
byte[] enc = fromHex(encrypted); | |
byte[] result = process(secretKey, Cipher.DECRYPT_MODE, enc); | |
return new String(result); | |
} catch (Exception e) { | |
Log.e(TAG, "Decrypt: " + e.getMessage()); | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
/** | |
* Get the raw encryption key. <br/> | |
* | |
* @param secretKey the encryption key | |
* @return the SecretKeySpec key | |
* @throws NoSuchAlgorithmException | |
*/ | |
private static SecretKeySpec generateKey(String secretKey) throws NoSuchAlgorithmException { | |
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | |
byte[] bytes = secretKey.getBytes(); | |
digest.update(bytes, 0, bytes.length); | |
byte[] key = digest.digest(); | |
return new SecretKeySpec(key, ALGORITHM); | |
} | |
/** | |
* Process the given input with the provided mode. <br/> | |
* | |
* @param mode the cipher mode | |
* @param value the value to process | |
* @return the processed value as byte[] | |
* @throws InvalidKeyException | |
* @throws IllegalBlockSizeException | |
* @throws BadPaddingException | |
* @throws NoSuchAlgorithmException | |
* @throws NoSuchPaddingException | |
* @throws NoSuchProviderException | |
*/ | |
private static byte[] process(String secretKey, int mode, byte[] value) | |
throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, | |
NoSuchPaddingException { | |
SecretKeySpec skeySpec = generateKey(secretKey); | |
Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(mode, keySpec); | |
return cipher.doFinal(value); | |
} | |
/** | |
* Decode an HEX encoded string into a byte[]. <br/> | |
* | |
* @param value the HEX string value | |
* @return the decoded byte[] | |
*/ | |
private static byte[] fromHex(String value) { | |
int len = value.length() / 2; | |
byte[] result = new byte[len]; | |
for (int i = 0; i < len; i++) { | |
result[i] = Integer.valueOf(value.substring(2 * i, 2 * i + 2), 16).byteValue(); | |
} | |
return result; | |
} | |
/** | |
* Encode a byte[] into an HEX string. <br/> | |
* | |
* @param value the byte[] value | |
* @return the HEX encoded string | |
*/ | |
private static String toHex(byte[] value) { | |
if (value == null) { | |
return ""; | |
} | |
StringBuilder result = new StringBuilder(2 * value.length); | |
for (byte b : value) { | |
result.append(HEX.charAt((b >> 4) & 0x0f)); | |
result.append(HEX.charAt(b & 0x0f)); | |
} | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment