程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> AES加密,aes

AES加密,aes

編輯:JAVA綜合教程

AES加密,aes


package com.edu.hpu;

 

import java.math.BigInteger;

import java.security.MessageDigest;

import java.security.SecureRandom;

 

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.spec.SecretKeySpec;

 

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

 

public class No14 {

public static void main(String[] args) throws Exception {  

        String content = "***";  

        System.out.println("加密前:" + content);  

  

        String key = "123";  

        System.out.println("加密密鑰和解密密鑰:" + (int)'1' + " " + (int)'2' + " " + (int)'3' );

          

        String encrypt = aesEncrypt(content, key);  

        System.out.println("密文:" + encrypt);  

          

        String decrypt = aesDecrypt(encrypt, key);  

        System.out.println("解密後:" + decrypt);  

    }  

      

    /** 

     * 將byte[]轉為各種進制的字符串 

     * @param bytes byte[] 

     * @param radix 可以轉換進制的范圍,從Character.MIN_RADIX到Character.MAX_RADIX,超出范圍後變為10進制 

     * @return 轉換後的字符串 

     */  

    public static String binary(byte[] bytes, int radix){  

        return new BigInteger(1, bytes).toString(radix);// 這裡的1代表正數  

    }  

      

    /** 

     * base 64 encode 

     * @param bytes 待編碼的byte[] 

     * @return 編碼後的base 64 code 

     */  

    public static String base64Encode(byte[] bytes){  

        return new BASE64Encoder().encode(bytes);  

    }  

      

    /** 

     * base 64 decode 

     * @param base64Code 待解碼的base 64 code 

     * @return 解碼後的byte[] 

     * @throws Exception 

     */  

    public static byte[] base64Decode(String base64Code) throws Exception{  

        return QStringUtil.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);  

    }  

      

    /** 

     * 獲取byte[]的md5值 

     * @param bytes byte[] 

     * @return md5 

     * @throws Exception 

     */  

    public static byte[] md5(byte[] bytes) throws Exception {  

        MessageDigest md = MessageDigest.getInstance("MD5");  

        md.update(bytes);  

          

        return md.digest();  

    }  

      

    /** 

     * 獲取字符串md5值 

     * @param msg  

     * @return md5 

     * @throws Exception 

     */  

    public static byte[] md5(String msg) throws Exception {  

        return QStringUtil.isEmpty(msg) ? null : md5(msg.getBytes());  

    }  

      

    /** 

     * 結合base64實現md5加密 

     * @param msg 待加密字符串 

     * @return 獲取md5後轉為base64 

     * @throws Exception 

     */  

    public static String md5Encrypt(String msg) throws Exception{  

        return QStringUtil.isEmpty(msg) ? null : base64Encode(md5(msg));  

    }  

      

    /** 

     * AES加密 

     * @param content 待加密的內容 

     * @param encryptKey 加密密鑰 

     * @return 加密後的byte[] 

     * @throws Exception 

     */  

    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {  

    //加密解密時都加上,否則不同機器加密結果不一致

    KeyGenerator kgen = KeyGenerator.getInstance("AES");  

        SecureRandom random=SecureRandom.getInstance("SHA1PRNG");

        random.setSeed(encryptKey.getBytes());

        kgen.init(128, random);  

        

        //一般寫法

        //KeyGenerator kgen = KeyGenerator.getInstance("AES");  

        //kgen.init(128, new SecureRandom(encryptKey.getBytes())); 

  

        Cipher cipher = Cipher.getInstance("AES");  

        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  

          

        return cipher.doFinal(content.getBytes("utf-8"));  

    }  

      

    /** 

     * AES加密為base 64 code 

     * @param content 待加密的內容 

     * @param encryptKey 加密密鑰 

     * @return 加密後的base 64 code 

     * @throws Exception 

     */  

    public static String aesEncrypt(String content, String encryptKey) throws Exception {  

        return base64Encode(aesEncryptToBytes(content, encryptKey));  

    }  

      

    /** 

     * AES解密 

     * @param encryptBytes 待解密的byte[] 

     * @param decryptKey 解密密鑰 

     * @return 解密後的String 

     * @throws Exception 

     */  

    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {  

    //加密解密時都加上,否則不同機器加密結果不一致

    KeyGenerator kgen = KeyGenerator.getInstance("AES");  

        SecureRandom random=SecureRandom.getInstance("SHA1PRNG");

        random.setSeed(decryptKey.getBytes());

        kgen.init(128, random);  

        

        //一般寫法

        //KeyGenerator kgen = KeyGenerator.getInstance("AES");  

        //kgen.init(128, new SecureRandom(decryptKey.getBytes())); 

          

        Cipher cipher = Cipher.getInstance("AES");  

        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  

        byte[] decryptBytes = cipher.doFinal(encryptBytes);  

          

        return new String(decryptBytes);  

    }  

      

    /** 

     * 將base 64 code AES解密 

     * @param encryptStr 待解密的base 64 code 

     * @param decryptKey 解密密鑰 

     * @return 解密後的string 

     * @throws Exception 

     */  

    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {  

        return QStringUtil.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);  

    }  

      

}  

 

class QStringUtil {

public static boolean isEmpty(String str){

if(str==null){

return true;

}else{

return false;

}

 

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved