AES 加密


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import android.util.Log;

import com.pingan.gamecenter.encrypt.Base64;



public class Aes {
    public static void main(String[] args) {
        String encry = encryptWithUTF8("123456", "82DE2BA3AD6F4E7B");
        Log.e("$$$$$$$$$$$$$$$", encry);
        System.out.println("#########"+encry);
    }
    // public static final String KEY = "1234567XDE234lom";
//  private final static Log logger = LogFactory.getLog(AES.class);

    public static String encrypt(String input, String key) {

        byte[] crypted = null;

        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
        } catch (Exception e) {
//          logger.error("encrypt error:", e);
        }

        return new String(Base64.encodeToByte(crypted, false));

    }

    public static String decrypt(String input, String key) throws Exception {

        byte[] output = null;

        try {

            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");

            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, skey);

            output = cipher.doFinal(Base64.decode(input));

        } catch (Exception e) {
//          logger.error("decrypt error:", e);
            throw e;

        }

        return new String(output);

    }

    public static String encryptWithUTF8(String input, String key) {

        byte[] crypted = null;

        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes("UTF-8"));
        } catch (Exception e) {
//          logger.error("encryptWithUTF8 error:", e);
        }

        return new String(Base64.encodeToByte(crypted, false));

    }

    /**
     * Encrypt with any of tools.
     * 
     * @param string
     *            -Need encrypt string.
     * @param encryptName
     *            -Encrypt tools,can be null or "",default SHA-256.
     * @author EX-ZHOUKAI003
     * @date 2013.12.09
     * **/
    public static String encryptWithAny(String string, String encryptName) {
        MessageDigest md;
        StringBuffer sb = new StringBuffer();

        if ("".equals(encryptName)) {
            encryptName = "SHA-256";
        }

        try {
            md = MessageDigest.getInstance(encryptName);
            md.update(string.getBytes("UTF-8"));
            for (byte b : md.digest()) {
                sb.append(String.format("%02X", b));
            }
        } catch (NoSuchAlgorithmException e) {
//          logger.error("encryptWithAny error:", e);
        } catch (UnsupportedEncodingException e) {
//          logger.error("encryptWithAny error:", e);
        }

        return sb.toString();
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章