Created
January 10, 2020 03:50
-
-
Save nicemak/fc609b39cc838ec7a9015343d3326789 to your computer and use it in GitHub Desktop.
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 javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
public class CryptoUtil { | |
private static final String key = "SKs9uZKvZU2VOFjQISEg4tJQyAeqX3Mm"; | |
private static final String initVector = "53og3sqCcKdQl5Ud"; | |
public static String encrypt(String value) | |
{ | |
try | |
{ | |
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); | |
SecretKeySpec sKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv); | |
byte[] encrypted = cipher.doFinal(value.getBytes()); | |
return Base64.encodeToString(encrypted, Base64.NO_WRAP); | |
} | |
catch (Exception ex) | |
{ | |
System.out.println("encrypt ex: " + ex); | |
} | |
return null; | |
} | |
public static String decrypt(String encrypted) { | |
try { | |
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); | |
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); | |
byte[] original = cipher.doFinal(Base64.decode(encrypted.getBytes("UTF-8"), Base64.NO_WRAP)); | |
return new String(original); | |
} catch (Exception ex) { | |
System.out.println("decrypt ex: " + ex); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment