[RESOLVED] Implement Encryption and Decryption AES methods of java in Android« Back to Previous Page

I want to implement the code of encryption and decryption method AES of java in android I take the code from this link: https://gist.github.com/bricef/2436364/. I implement this code in Android but it does not work, I did not know what is the problem. Can you help me?! Thank you for your cooperation.
Posted by Nora (Questions:4, Answers:5)
Asked on July 18, 2013 10:22 am
Category: Android
0

OK,thanks

Posted by Nora (Questions:4, Answers:5)
Answered On July 19, 2013 9:41 pm #
0

Hello Nora. This code was tested under Android and it works just fine :)

Posted by itcuties (Questions:2, Answers:18)
Answered On July 19, 2013 3:51 pm #
0

Thank you for that ,but I want to ask you if you can implement the same method (AES)
in android because I dealing with data coming from web service ecrypted with this method

Posted by Nora (Questions:4, Answers:5)
Answered On July 19, 2013 3:20 pm #
2

Hello, here is the code that you can use to encrypt/decrypt data (based on our Encrypt GET parameters tutorial :))


// Algorithm used
private final static String ALGORITHM = "AES";

/**
* Encrypt data
* @param secretKey - a secret key used for encryption
* @param data - data to encrypt
* @return Encrypted data
* @throws Exception
*/
public static String cipher(String secretKey, String data) throws Exception {

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);

Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);

return toHex(cipher.doFinal(data.getBytes()));
}

/**
* Decrypt data
* @param secretKey - a secret key used for decryption
* @param data - data to decrypt
* @return Decrypted data
* @throws Exception
*/
public static String decipher(String secretKey, String data) throws Exception {

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), secretKey.getBytes(), 128, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);

Cipher cipher = Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, key);

return new String(cipher.doFinal(toByte(data)));
}

// Helper methods

private static byte[] toByte(String hexString) {
int len = hexString.length()/2;

byte[] result = new byte[len];

for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}

public static String toHex(byte[] stringBytes) {
StringBuffer result = new StringBuffer(2*stringBytes.length);

for (int i = 0; i < stringBytes.length; i++) {
result.append(HEX.charAt((stringBytes[i]>>4)&0x0f)).append(HEX.charAt(stringBytes[i]&0x0f));
}

return result.toString();
}

private final static String HEX = "0123456789ABCDEF";

If you want to code/decode data do as in the following example


String text = "This is a plain text";
String key = "super-secret-key-0123123451";

String crypted = cipher(key, text);
String decrypted = decipher(key, crypted);

// We have tested it in a Android activity code by displaying data :)
Log.d("CRYPTO-TEST", "plain: " + text);
Log.d("CRYPTO-TEST", "crypted: " + crypted);
Log.d("CRYPTO-TEST", "decrypted: " + decrypted);

Have a nice day!

Posted by itcuties (Questions:2, Answers:18)
Answered On July 19, 2013 5:34 am #
No account?



It's FREE!

Like what you have found?


... and keep us going