Java 對稱資料加密AES

來源:互聯網
上載者:User

標籤:format   code   base   key   擷取   cipher   ace   ase   main   

  昨天呢寫了一個非對稱資料加密,今天來寫對稱的資料加密AES。對稱資料加密就是只使用一個密鑰  進行加密和解密,AES可以使用128,192,和256位密鑰。

  然後就是我的工具類:

public class AESUtil {    public static byte[] getKeys(String data){        try {            // 建立AES的Key生產者            KeyGenerator kgen = KeyGenerator.getInstance("AES");            // 利用使用者密碼作為隨機數初始化出128位的key生產者            //SecureRandom 是產生安全隨機數序列,password.getBytes() 是種子,只要種子相同,序列就一樣,密鑰也一樣            kgen.init(128, new SecureRandom(data.getBytes()));            // 根據使用者密碼,產生一個密鑰            SecretKey secretKey = kgen.generateKey();            byte[] key = secretKey.getEncoded();            return key;        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();            System.out.println("沒有此演算法");        }        return null;    }    public static byte[] getKeys(){        try {            // 建立AES的Key生產者            KeyGenerator kgen = KeyGenerator.getInstance("AES");            // 利用使用者密碼作為隨機數初始化出128位的key生產者            //SecureRandom 是產生安全隨機數序列,沒有種子            kgen.init(128);            SecretKey secretKey = kgen.generateKey();            byte[] key = secretKey.getEncoded();            return key;        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();            System.out.println("沒有此演算法");        }        return null;    }    /**     * @param content     * @param secretkey     * @return     */    public static byte[] encrypt(String content, byte[] secretkey) {        try {            // 轉換為AES專用密鑰            SecretKeySpec key = new SecretKeySpec(secretkey, "AES");            Cipher cipher = Cipher.getInstance("AES");// 建立密碼器            byte[] byteContent = content.getBytes("utf-8");            cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化為加密模式的密碼器            byte[] result = cipher.doFinal(byteContent);// 加密            return result;        } catch (Exception e){            e.printStackTrace();        }        return null;    }    public static byte[] decrypt(byte[] content, SecretKey secretKey) {        try {            byte[] enCodeFormat = secretKey.getEncoded();            // 轉換為AES專用密鑰            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");            // 建立密碼器            Cipher cipher = Cipher.getInstance("AES");            // 初始化為解密模式的密碼器            cipher.init(Cipher.DECRYPT_MODE, key);            byte[] result = cipher.doFinal(content);            // 明文            return result;        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    public static byte[] decrypt(byte[] content, byte[] secretKey) {        try {            // 轉換為AES專用密鑰            SecretKeySpec key = new SecretKeySpec(secretKey, "AES");            // 建立密碼器            Cipher cipher = Cipher.getInstance("AES");            // 初始化為解密模式的密碼器            cipher.init(Cipher.DECRYPT_MODE, key);            byte[] result = cipher.doFinal(content);            // 明文            return result;        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    public static void main(String[] args) {        String content = "我是-冬竹";        String password = "767";        System.out.println("加密之前:" + content);        byte[] key = getKeys();        // 加密        byte[] encrypt = AESUtil.encrypt(content, key);        System.out.println("加密後的內容:" + Base64.encodeBase64URLSafeString(encrypt));        // 解密        byte[] decrypt = AESUtil.decrypt(encrypt, key);        System.out.println("解密後的內容:" + new String(decrypt));    }}

  

  這個過程就是隨機產生一個密鑰(可以有種子,也可以沒有),然後用這個祕密金鑰加密和解密,還是很簡單的啦(●ˇ∀ˇ●)

  然後為了安全常常是RSA和AES一起使用,一般來講就是:

    用戶端加密:

      1. 隨機產生AES密鑰;

      2. 對重要訊息進行AES加密

      3. 使用RSA對AES密鑰進行公開金鑰加密

    服務端解密:

      1. 使用RSA私密金鑰對AES密鑰解密

      2. 用AES金鑰組重要訊息的密文解密

所以我又類比了一下這個過程:

public class AES_RSAUtil {    public static void main(String[] args) throws Exception{        /*類比用戶端*/        String msg = "hello 冬竹";        byte[] key = AESUtil.getKeys();//擷取密鑰的編碼        byte[] bytes = AESUtil.encrypt(msg,key);        String seKey = Base64.encodeBase64URLSafeString(key);//轉成字串之後進行再加密        RSAUtil.init();        //RSA公開金鑰 加密後的 AES密鑰        String encryptKey = RSAUtil.encryptByPublicKey(seKey,RSAUtil.getPublicKey(RSAUtil.keyMap.get("PUBLIC_KEY")));        /*類比服務端*/        //解碼AES密鑰        String aesKey = RSAUtil.decryptByPrivateKey(encryptKey,RSAUtil.getPrivateKey(RSAUtil.keyMap.get("PRIVATE_KEY")));        //還原aesKey        byte[] secretKey = Base64.decodeBase64(aesKey);        String ming = new String(AESUtil.decrypt(bytes,secretKey));        System.out.println(ming);    }

  

  這個用到的依賴什麼的都是我昨天的那個啦,好了,因為Java的jdk裡已經幫我們寫好了這些密碼編譯演算法的細節,所以很方便

  最後推薦一下:

    1. AES密碼編譯演算法的詳細介紹與實現

    2.資料轉送加密——非對稱式加密演算法RSA+對稱式加密演算法AES

https://github.com/MoisAbby/XZUtils

Java 對稱資料加密AES

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.