The below code is a simple way to do encryption and decryption in Java. Contrary to Hashing, encryption algorithms are reversible and the original string can be retrieved from the encrypted string using the right key and algorithm. The below code uses "AES/ECB/PKCS5Padding" to encrypt and decrypt.
Feel free to use the below code anywhere you like
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class EncryptUtils {
public static final String DEFAULT_ENCODING = "UTF-8";
public static final String DEFAULT_KEY = "";
/**
* Encrypts a String with a key based on the "AES/ECB/PKCS5Padding" algorithm
*
* @param key - The key to be used to encrypt the String
* @param strToEncrypt - The value to be encrypted
* @return String Encrypted Value
* @exception NoSuchAlgorithmException
* @exception InvalidKeyException
* @exception IllegalBlockSizeException
* @exception BadPaddingException
* @exception NoSuchPaddingException
*/
public static String encryptString(String key, String strToEncrypt) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
final SecretKeySpec secretKey = new SecretKeySpec(getKeyAsBytes(key), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
return encryptedString;
}
/**
* Decrypts an encrypted value with a key based on the "AES/ECB/PKCS5Padding" algorithm
*
* @param key - The key to be used to decrypt the String
* @param strToDecrypt - The value to be decrypted
* @return String Encrypted Value
* @exception NoSuchAlgorithmException
* @exception InvalidKeyException
* @exception IllegalBlockSizeException
* @exception BadPaddingException
* @exception NoSuchPaddingException
*/
public static String decryptString(String key, String strToDecrypt) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
final SecretKeySpec secretKey = new SecretKeySpec(getKeyAsBytes(key), "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
return decryptedString;
}
/**
* Converts a String Key into bytes.
* The output of this value is used internally for encryption & Decryption
* @param key - Encryption key
* @return byte[] - The key value as a byte array
* @throws IllegalArgumentException
*/
private static byte[] getKeyAsBytes(String key) throws IllegalArgumentException {
byte[] raw = key.getBytes(Charset.forName("US-ASCII"));
if (raw.length != 16) {
throw new IllegalArgumentException("Invalid key size.");
}
return raw;
}
}
No comments:
Post a Comment