日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
aes加密

package com.zyhao.openec.util;

創(chuàng)新互聯(lián)建站服務(wù)項目包括新賓網(wǎng)站建設(shè)、新賓網(wǎng)站制作、新賓網(wǎng)頁制作以及新賓網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,新賓網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到新賓省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.KeyGenerator;

import javax.crypto.NoSuchPaddingException;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class MainTest {

    //AES:高級加密標(biāo)準(zhǔn),新一代標(biāo)準(zhǔn),加密速度更快,安全性更高(不用說優(yōu)先選擇)

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

String aesKey = getKey();

System.out.println(aesKey);

//        //加密  

String oriString = "我是加密的原文:hello world|hi!";

        byte[] oriText = oriString.getBytes(); 

        byte[] be = getAESEncode(hexStringToBytes(aesKey),oriText); 

        String encodeString = byteToHexString(be);

        System.out.println(encodeString);

        //解密  

        byte[] encodeByte=hexStringToBytes(encodeString);

        byte[] bd = getAESDecode(hexStringToBytes(aesKey),encodeByte); 

        System.out.println(new String(bd)); 

    }  

  

    /** 

     * AES生成密鑰 

     * 自動生成AES128位密鑰 

     * 傳入保存密鑰文件路徑 

     * filePath 表示文件存儲路徑加文件名;例如d:\aes.txt 

     * @throws NoSuchAlgorithmException  

     * @throws IOException  

     */  

    public static byte[] getAutoCreateAESKey() throws NoSuchAlgorithmException, IOException{  

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

        kg.init(128);//要生成多少位,只需要修改這里即可128, 192或256  

        SecretKey sk = kg.generateKey();  

        byte[] b = sk.getEncoded();  

        return b;

    }  

      

    /** 

     * 加密 

     * 使用對稱密鑰進(jìn)行加密 

     * keyFilePath 密鑰存放路徑 

     * text 要加密的字節(jié)數(shù)組 

     * 加密后返回一個字節(jié)數(shù)組 

     * @throws IOException  

     * @throws NoSuchPaddingException  

     * @throws NoSuchAlgorithmException  

     * @throws InvalidKeyException  

     * @throws BadPaddingException  

     * @throws IllegalBlockSizeException  

     */  

    public static byte[] getAESEncode(byte[] key,byte[] text) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{  

        SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");  

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

        cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);  

        byte[] bjiamihou = cipher.doFinal(text);  

        return bjiamihou;  

    }  

      

    /** 

     * 解密 

     * 使用對稱密鑰進(jìn)行解密 

     * keyFilePath 密鑰存放路徑 

     * text 要解密的字節(jié)數(shù)組 

     * 解密后返回一個字節(jié)數(shù)組 

     * @throws IOException  

     * @throws NoSuchPaddingException  

     * @throws NoSuchAlgorithmException  

     * @throws InvalidKeyException  

     * @throws BadPaddingException  

     * @throws IllegalBlockSizeException  

     */  

    public static byte[] getAESDecode(byte[] key,byte[] text) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{  

        SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");  

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

        cipher.init(Cipher.DECRYPT_MODE, sKeySpec);  

        byte[] bjiemihou = cipher.doFinal(text);  

        return bjiemihou;  

    } 

  /**

   * 隨機生成秘鑰

   */

    public static String getKey() {

   try {

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

   kg.init(128);

   //要生成多少位,只需要修改這里即可128, 192或256

   SecretKey sk = kg.generateKey();

   byte[] b = sk.getEncoded();

   String s = byteToHexString(b);

   System.out.println(s);

   System.out.println("十六進(jìn)制密鑰長度為"+s.length());

   System.out.println("二進(jìn)制密鑰的長度為"+s.length()*4);

   return s;

}catch (NoSuchAlgorithmException e) {

   e.printStackTrace();

   System.out.println("沒有此算法。");

   }

        return null;

    }

//        /**

//     * 使用指定的字符串生成秘鑰

//     */

//    public static void getKeyByPass() {

//     //生成秘鑰

//     String password="testkey";

//     try {

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

//        // kg.init(128);//要生成多少位,只需要修改這里即可128, 192或256

//        //SecureRandom是生成安全隨機數(shù)序列,password.getBytes()是種子,只要種子相同,序列就一樣,所以生成的秘鑰就一樣。

//        kg.init(128, new SecureRandom(password.getBytes()));

//        SecretKey sk = kg.generateKey();

//        byte[] b = sk.getEncoded();

//        String s = byteToHexString(b);

//        System.out.println(s);

//        System.out.println("十六進(jìn)制密鑰長度為"+s.length());

//        System.out.println("二進(jìn)制密鑰的長度為"+s.length()*4);

//     }catch (NoSuchAlgorithmException e) {

//        e.printStackTrace();

//        System.out.println("沒有此算法。");

//        }

//     }

/**

* byte數(shù)組轉(zhuǎn)化為16進(jìn)制字符串

* @param bytes

* @return

*/

    public static String byteToHexString(byte[] bytes) {

   StringBuffer sb = new StringBuffer();

   for (int i = 0; i < bytes.length; i++) {

       String strHex=Integer.toHexString(bytes[i]);

       if(strHex.length() > 3) {

           sb.append(strHex.substring(6));

       } else {

           if(strHex.length() < 2) {

               sb.append("0" + strHex);

           } else {

               sb.append(strHex);

           }

       }

   }

   return sb.toString();

}

    /**  

     * Convert hex string to byte[]  

     * @param hexString the hex string  

     * @return byte[]  

     */  

    public static byte[] hexStringToBytes(String hexString) {   

        if (hexString == null || hexString.equals("")) {   

            return null;   

        }   

        hexString = hexString.toUpperCase();   

        int length = hexString.length() / 2;   

        char[] hexChars = hexString.toCharArray();   

        byte[] d = new byte[length];   

        for (int i = 0; i < length; i++) {   

            int pos = i * 2;   

            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));   

        }   

        return d;   

    } 

    /**  

     * Convert char to byte  

     * @param c char  

     * @return byte  

     */  

     private static byte charToByte(char c) {   

        return (byte) "0123456789ABCDEF".indexOf(c);   

    }

}


分享文章:aes加密
網(wǎng)站URL:http://www.dlmjj.cn/article/jjpepc.html