发新话题
打印

AES

AES加密/解密

package com.gameplus.common.crypto;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;

/**
* AES
*/
public class AES {
        private KeyGenerator keyGenerator;

        private Key key2;

        private MyKey myKey = new MyKey();

        private Cipher cipher;

        /**
         * 构造
         */
        public AES() {
                try {
                        cipher = Cipher.getInstance("AES");
                } catch (NoSuchPaddingException nspe) {
                        nspe.printStackTrace();
                } catch (NoSuchAlgorithmException nsae) {
                        nsae.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        /**
         * 构造
         *
         * @param key
         */
        public void setKey2(String key) {
                myKey.setKey(key);
                key2 = (Key) myKey;
        }

        /**
         * 加密
         *
         * @param encryptStr
         * @return
         */
        public String getEncryptedData(String encryptStr) {
                setKey2(key);
                String encryptedData = "";
                try {
                        cipher.init(Cipher.ENCRYPT_MODE, key2);
                        byte[] plaintext = encryptStr.getBytes("UTF-8");
                        byte[] ciphertext = cipher.doFinal(plaintext);
                        String ciphertextHex = byte2hex(ciphertext);
                        return ciphertextHex;
                } catch (BadPaddingException bpe) {
                        bpe.printStackTrace();
                } catch (IllegalBlockSizeException ibe) {
                        ibe.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return encryptedData;
        }

        /**
         * 解密
         *
         * @param decryptStr
         * @return
         * @throws Exception
         */
        public String getDecryptedData(String decryptStr) {
                setKey2(key);
                String decryptedData = "";
                try {
                        cipher.init(Cipher.DECRYPT_MODE, key2);
                        byte[] ciphertext = hex2byte(decryptStr);
                        byte[] decryptedStr = cipher.doFinal(ciphertext);
                        decryptedData = new String(decryptedStr, "UTF-8");
                        return decryptedData;
                } catch (BadPaddingException bpe) {
                        bpe.printStackTrace();
                } catch (IllegalBlockSizeException ibe) {
                        ibe.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }

        /**
         * MyKey
         *
         * @author andy TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
         */
        private class MyKey implements Key {
                private String key;

                public MyKey() {
                        key = new String("00000000000000000000000000000000");
                }

                /*
                 * (非 Javadoc)
                 *
                 * @see java.security.Key#getEncoded()
                 */
                public byte[] getEncoded() {
                        return hex2byte(key);
                }

                /*
                 * (非 Javadoc)
                 *
                 * @see java.security.Key#getAlgorithm()
                 */
                public String getAlgorithm() {
                        return "AES";
                }

                /*
                 * (非 Javadoc)
                 *
                 * @see java.security.Key#getFormat()
                 */
                public String getFormat() {
                        return "RAW";
                }

                public void setKey(String key) {
                        this.key = key;
                }
        }

        public static void main(String[] args) {
                AES aes = new AES();
                String s = aes.getEncryptedData("13564177");
                System.out.println(s);

                System.out.println(aes
                                .getDecryptedData("AB6051F6AFEFC1C8D9CEDE61471E6AFA"));
        }

        public String encrypt(String source) {
                return getEncryptedData(source);
        }

        public String decrypt(String source) {
                return getDecryptedData(source);
        }

}      
www.ltebuy.net

TOP

发新话题