//AES Encryption - Decryption
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import org.bouncycastle.crypto.digests.SHA1Digest;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.NoSuchPaddingException;
import org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator;
import javax.crypto.Cipher;
import org.bouncycastle.crypto.params.KeyParameter;
public class AESEncryptionDecrytionString {
/**
* @param args
*/
static byte[] iv ="OFRna73m*aze01xY".getBytes();
static byte[] salt="saltkey".getBytes();
static String password="passwordkey";
static int iterations=2,keySize=128;
static String cipher_instance="AES/CBC/PKCS5Padding",algo="AES";
public static String encrypt(String strDataToEncrypt) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strCipherText = new String();
try
{
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(password.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
SecretKey secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes("UTF8");
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
strCipherText =new String(byteCipherText);//.apache.commons.codec.binary.Base64.encodeBase64String(byteCipherText);// BASE64Encoder().encode(byteCipherText);
byte[] b=org.apache.commons.codec.binary.Base64.encodeBase64(byteCipherText);
strCipherText=new String(b);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strCipherText;
}
public static String decrypt(String strCipherText) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strDecryptedText = new String();
try
{
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(password.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.DECRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteCipherText=org.apache.commons.codec.binary.Base64.decodeBase64(strCipherText.getBytes());
byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
strDecryptedText = new String(byteDecryptedText);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strDecryptedText;
}
public static String encryptString(String strDataToEncrypt,String newkey) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strCipherText = new String();
try
{
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(newpassword.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
SecretKey secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes("UTF8");
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
strCipherText =new String(byteCipherText);//.apache.commons.codec.binary.Base64.encodeBase64String(byteCipherText);// BASE64Encoder().encode(byteCipherText);
byte[] b=org.apache.commons.codec.binary.Base64.encodeBase64(byteCipherText);
strCipherText=new String(b);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strCipherText;
}
public static String decryptString(String strCipherText,String newkey) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strDecryptedText = new String();
try
{
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(newkey.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.DECRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteCipherText=org.apache.commons.codec.binary.Base64.decodeBase64(strCipherText.getBytes());
byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
strDecryptedText = new String(byteDecryptedText);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strDecryptedText;
}
}
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import org.bouncycastle.crypto.digests.SHA1Digest;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.NoSuchPaddingException;
import org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator;
import javax.crypto.Cipher;
import org.bouncycastle.crypto.params.KeyParameter;
public class AESEncryptionDecrytionString {
/**
* @param args
*/
static byte[] iv ="OFRna73m*aze01xY".getBytes();
static byte[] salt="saltkey".getBytes();
static String password="passwordkey";
static int iterations=2,keySize=128;
static String cipher_instance="AES/CBC/PKCS5Padding",algo="AES";
public static String encrypt(String strDataToEncrypt) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strCipherText = new String();
try
{
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(password.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
SecretKey secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes("UTF8");
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
strCipherText =new String(byteCipherText);//.apache.commons.codec.binary.Base64.encodeBase64String(byteCipherText);// BASE64Encoder().encode(byteCipherText);
byte[] b=org.apache.commons.codec.binary.Base64.encodeBase64(byteCipherText);
strCipherText=new String(b);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strCipherText;
}
public static String decrypt(String strCipherText) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strDecryptedText = new String();
try
{
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(password.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.DECRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteCipherText=org.apache.commons.codec.binary.Base64.decodeBase64(strCipherText.getBytes());
byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
strDecryptedText = new String(byteDecryptedText);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strDecryptedText;
}
public static String encryptString(String strDataToEncrypt,String newkey) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strCipherText = new String();
try
{
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(newpassword.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
SecretKey secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes("UTF8");
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
strCipherText =new String(byteCipherText);//.apache.commons.codec.binary.Base64.encodeBase64String(byteCipherText);// BASE64Encoder().encode(byteCipherText);
byte[] b=org.apache.commons.codec.binary.Base64.encodeBase64(byteCipherText);
strCipherText=new String(b);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strCipherText;
}
public static String decryptString(String strCipherText,String newkey) throws NoSuchAlgorithmException, NoSuchPaddingException
{
String strDecryptedText = new String();
try
{
SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES");
PKCS5S1ParametersGenerator generator = new
PasswordDeriveBytes(new SHA1Digest());
generator.init(newkey.getBytes(), salt, iterations);
byte[] key = ((KeyParameter)
generator.generateDerivedParameters(keySize)).getKey();
secretKey = new SecretKeySpec(key, algo);
Cipher aesCipher = Cipher.getInstance(cipher_instance);
aesCipher.init(Cipher.DECRYPT_MODE,secretKey,new IvParameterSpec(iv));
byte[] byteCipherText=org.apache.commons.codec.binary.Base64.decodeBase64(strCipherText.getBytes());
byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
strDecryptedText = new String(byteDecryptedText);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return strDecryptedText;
}
}