Skip to content

Instantly share code, notes, and snippets.

@pranaypatel512
Created April 20, 2016 07:31
Show Gist options
  • Save pranaypatel512/64682fc01839873cc9edab3444749287 to your computer and use it in GitHub Desktop.
Save pranaypatel512/64682fc01839873cc9edab3444749287 to your computer and use it in GitHub Desktop.
[AES (acronym of Advanced Encryption Standard) is a symmetric encryption algorithm.
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* <p>
* [AES (acronym of Advanced Encryption Standard) is a symmetric encryption algorithm.
* The algorithm was developed by two Belgian cryptographer Joan Daemen and Vincent Rijmen.
* AES was designed to be efficient in both hardware and software, and supports a block length of 128 bits and key lengths of 128, 192, and 256 bits.]
* more info: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
*/
public class Encryption {
/*
* [How to use] ->
* String encryptedText = Encryption.encrypt(masterpassword, simpletext) // get encrypted text from simple text
* ...
* String simpletext = Encryption.decrypt(masterpassword, encryptedText) // get simple text from encrypted text
*
* */
/**
* This method use for encrypt plain text to hex
*
* @param masterPassword : master password for encrypt your text.Remember it for decryption process
* @param simpletext : this is plainText which you want to decrypt
*/
public static String encrypt(String masterPassword, String simpletext) throws Exception {
byte[] rawKey = getRawKey(masterPassword.getBytes());
byte[] result = encrypt(rawKey, simpletext.getBytes());
return toHex(result);
}
/**
* This method use for decrypt hex text to plain text
*
* @param masterPassword : master password for decryption your text.Master password which you are used at time of encryption
* @param encryptedText : this is encryptedText which you encrypt
*/
public static String decrypt(String masterPassword, String encryptedText) throws Exception {
byte[] rawKey = getRawKey(masterPassword.getBytes());
byte[] enc = toByte(encryptedText);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
keyGenerator.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey;
skey = keyGenerator.generateKey();
return skey.getEncoded();
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(clear);
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(encrypted);
}
/**
* This method use for simple text to hex text
*
* @param text : text to converting into hex
*/
public static String toHex(String text) {
return toHex(text.getBytes());
}
/**
* This method use for hex text to simple text
*
* @param hex : hex to converting into plaint text
*/
public static String fromHex(String hex) {
return new String(toByte(hex));
}
/**
* This method use for hex text to byte
*/
public 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;
}
/**
* This method use for byte to hex
*/
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (byte aBuf : buf) {
appendHex(result, aBuf);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment