Last active
August 25, 2017 10:22
-
-
Save andiisfh/62d6869d0cc74e6f7b1418515e0248c4 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
public static void main(String[] args) { | |
String data = "andiisfh@gmail.com|Andi Insanudin|" + String.valueOf(System.currentTimeMillis() / 1000); | |
String key = "social_login"; | |
URLEncoder.encode(encryption(data, key), "UTF-8") | |
} | |
public String encryption(String word, String key) throws Exception { | |
byte[] ivBytes; | |
String password = key + "clientAPIjakpost034535"; | |
byte[] saltBytes = md5(key).substring(3, 19).getBytes("UTF-8"); | |
// Derive the key | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); | |
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); | |
SecretKey secretKey = factory.generateSecret(spec); | |
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); | |
//encrypting the word | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, secret); | |
AlgorithmParameters params = cipher.getParameters(); | |
ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); | |
byte[] encryptedTextBytes = cipher.doFinal(word.getBytes("UTF-8")); | |
//prepend salt and vi | |
byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length]; | |
System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); | |
System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length); | |
System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length); | |
return new String(Base64.encode(buffer, Base64.DEFAULT)); | |
} | |
private static String md5(String text) { | |
MessageDigest md = null; | |
try { | |
md = MessageDigest.getInstance("MD5"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
md.update(text.getBytes()); | |
byte byteData[] = md.digest(); | |
//convert the byte to hex format method 1 | |
StringBuffer sb = new StringBuffer(); | |
for (int i = 0; i < byteData.length; i++) { | |
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); | |
} | |
Log.d("md5", sb.toString()); | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment