About Me

My photo
India
Hey there, lovely people! I'm Hemant Menaria, and I'm passionate about programming. Having completed my MCA in 2011, I've delved into the world of coding with fervor. I believe in sharing knowledge, making complex concepts easy to grasp for everyone. JAVA, PHP, and ANDROID hold a special place in my heart, and I spend most of my time immersed in them. Currently, I'm deeply engaged in API/Webservice frameworks and crafting Hybrid mobile applications to enhance flexibility in the digital realm. If you ever find yourself stuck with a programming challenge, feel free to reach out to me at +91-8955499900 or drop me a line at hemantmenaria008@gmail.com. I'm always eager to help fellow enthusiasts navigate the intricacies of coding!

Friday, May 25, 2012

AES Encryption - Decryption

//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;       
    }
}

No comments:

Post a Comment