001    package com.khubla.pragmatach.framework.crypto;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.ByteArrayOutputStream;
005    import java.security.MessageDigest;
006    import java.util.Arrays;
007    
008    import javax.crypto.Cipher;
009    import javax.crypto.CipherInputStream;
010    import javax.crypto.spec.SecretKeySpec;
011    
012    import org.apache.commons.codec.binary.Base64;
013    import org.apache.commons.io.IOUtils;
014    
015    /**
016     * @author tome
017     */
018    public class AES {
019            /**
020             * keyspec
021             */
022            private final static String KEYSPEC = "AES/ECB/ISO10126Padding";
023    
024            /**
025             * decrypt
026             */
027            public static String decrypt(String encryptedPayload, String key)
028                            throws Exception {
029                    try {
030                            final SecretKeySpec secretKeySpec = new SecretKeySpec(
031                                            makeAESKey(key), "AES");
032                            final Cipher cipher = Cipher.getInstance(KEYSPEC);
033                            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
034                            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
035                            final CipherInputStream CipherInputStream = new CipherInputStream(
036                                            new ByteArrayInputStream(
037                                                            Base64.decodeBase64(encryptedPayload
038                                                                            .getBytes("UTF-8"))), cipher);
039                            IOUtils.copy(CipherInputStream, baos);
040                            return baos.toString("UTF-8");
041                    } catch (final Exception e) {
042                            throw new Exception("Exception in decrypt", e);
043                    }
044            }
045    
046            /**
047             * encrypt
048             */
049            public static String encrypt(String payload, String key) throws Exception {
050                    try {
051                            final SecretKeySpec secretKeySpec = new SecretKeySpec(
052                                            makeAESKey(key), "AES");
053                            final Cipher cipher = Cipher.getInstance(KEYSPEC);
054                            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
055                            final CipherInputStream CipherInputStream = new CipherInputStream(
056                                            new ByteArrayInputStream(payload.getBytes("UTF-8")), cipher);
057                            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
058                            IOUtils.copy(CipherInputStream, baos);
059                            return Base64.encodeBase64String(baos.toByteArray());
060                    } catch (final Exception e) {
061                            throw new Exception("Exception in encrypt", e);
062                    }
063            }
064    
065            /**
066             * make a nice AES key
067             */
068            private static byte[] makeAESKey(String key) throws Exception {
069                    try {
070                            final MessageDigest sha = MessageDigest.getInstance("SHA-1");
071                            final byte[] rawkey = sha.digest(key.getBytes("UTF-8"));
072                            return Arrays.copyOf(rawkey, 16); // use only first 128 bit
073                    } catch (final Exception e) {
074                            throw new Exception("Exception in makeAESKey", e);
075                    }
076            }
077    }