Skip to content

Instantly share code, notes, and snippets.

@dmitriy-chernysh
Last active February 18, 2019 21:37
Show Gist options
  • Save dmitriy-chernysh/6a0165efb58a443524d4e44d6a1da99c to your computer and use it in GitHub Desktop.
Save dmitriy-chernysh/6a0165efb58a443524d4e44d6a1da99c to your computer and use it in GitHub Desktop.
/**
* This class helps to encrypt passwords before saving and decrypt then
* <p>
* Created by Dmitriy V. Chernysh
* <p>
* https://instagr.am/mobiledevpro
* <p>
* #MobileDevPro
*/
public class EncryptDataHelper {
//store this key in KeyStore
private static final String KEY = "Q5~VAwarOiOwexZa53xy0v1|E~OHcWYcYSd*8ZUFD%8m39D2=UZIvC2ueh3";
private static final String CHARSET_NAME = "UTF-8";
private static EncryptDataHelper sHelper;
private EncryptDataHelper() {
}
public static EncryptDataHelper getInstance() {
if (sHelper == null) sHelper = new EncryptDataHelper();
return sHelper;
}
public String encryptString(String str) {
try {
//convert key to SHA-256
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] keyBytes = digest.digest(KEY.getBytes(CHARSET_NAME));
byte[] ivBytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//encrypt string
byte[] result = encrypt(ivBytes, keyBytes, str.getBytes(CHARSET_NAME));
//convert result to Base64
return Base64.encodeToString(result, Base64.DEFAULT).replace("\n", "");
} catch (Exception e) {
Log.e(Constants.LOG_TAG_ERROR, "EncryptDataHelper.encryptString: " + e.getLocalizedMessage(), e);
}
return str;
}
public String decryptString(String str) {
try {
byte[] encodedStr = Base64.decode(str, Base64.DEFAULT);
//convert key to SHA-256
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] keyBytes = digest.digest(KEY.getBytes(CHARSET_NAME));
byte[] ivBytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte[] result = decrypt(ivBytes, keyBytes, encodedStr);
return new String(result, CHARSET_NAME);
} catch (Exception e) {
Log.e(Constants.LOG_TAG_ERROR, "EncryptDataHelper.decryptString: " + e.getLocalizedMessage(), e);
}
return str;
}
private byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes)
throws 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);
}
private byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes)
throws 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.DECRYPT_MODE, newKey, ivSpec);
return cipher.doFinal(textBytes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment