Copyright (c) 2014 Sumit Gouthaman. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Softwar...
If you think the Android project RavenChat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.sumitgouthaman.raven.utils.crypto;
/*fromwww.java2s.com*/import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by sumit on 18/5/14.
*//**
* Class that abstracts out the encryption process.
*
* Uses AES encryption.
*/publicclass EncryptionUtils {
/**
* Encrypts the String
* @param plaintext
* @param key
* @return - The encrypted text
*/publicstatic String encrypt(String plaintext, String key) {
byte[] keyarr = Base64Utils.decode(key);
SecretKeySpec sks = new SecretKeySpec(keyarr, "AES");
byte[] encodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, sks);
encodedBytes = c.doFinal(plaintext.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
String ciphertext = Base64Utils.encode(encodedBytes);
return ciphertext;
}
/**
* Decrypts the text
* @param ciphertext
* @param key
* @return - The plaintext
*/publicstatic String decrypt(String ciphertext, String key) {
byte[] keyarr = Base64Utils.decode(key);
SecretKeySpec sks = new SecretKeySpec(keyarr, "AES");
byte[] encodedBytes = Base64Utils.decode(ciphertext);
byte[] decodedBytes = null;
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, sks);
decodedBytes = c.doFinal(encodedBytes);
} catch (Exception e) {
e.printStackTrace();
}
String plaintext = new String(decodedBytes);
return plaintext;
}
}