java 對稱式加密

來源:互聯網
上載者:User

標籤:TE   nbsp   pac   key   des   ini   turn   dem   encrypt   

DSE

package com.aarony.test;import java.io.IOException;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** *  * 此類描述的是:對稱式加密 *  * @author: Aarony * @version: 2018年6月20日 下午9:20:34 */public class SymmetricEncryptionDESDemo {    /**     *      * 此方法描述的是: 解密     *      * @author: Aarony     * @version: 2018年6月20日 下午9:30:52     */    public static byte[] decryptDES(byte[] bytes, SecretKey secretKey) throws Exception {        Cipher cipher = Cipher.getInstance("AES");        cipher.init(Cipher.DECRYPT_MODE, secretKey);        return cipher.doFinal(bytes);    }    /**     *      * 此方法描述的是: 加密     *      * @author: Aarony     * @version: 2018年6月20日 下午9:29:02     */    public static byte[] encryptDES(byte[] bytes, SecretKey secretKey) throws Exception {        Cipher cipher = Cipher.getInstance("AES");        cipher.init(Cipher.ENCRYPT_MODE, secretKey);        return cipher.doFinal(bytes);    }    public static SecretKey loadKeyDES(String base64Key) throws IOException {        byte[] bytes = base642byte(base64Key);        SecretKey key = new SecretKeySpec(bytes, "AES");        return key;    }    /**     *      * 此方法描述的是:擷取base64 key     *      * @author: Aarony     * @version: 2018年6月20日 下午9:25:36     */    public static String genKeyDES() throws NoSuchAlgorithmException {        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");        keyGenerator.init(56);        SecretKey key = keyGenerator.generateKey();        return byte2base64(key.getEncoded());    }    /**     *      * 此方法描述的是:base64 解碼     *      * @author: Aarony     * @version: 2018年6月20日 下午9:16:57     */    public static byte[] base642byte(String base64) throws IOException {        BASE64Decoder decoder = new BASE64Decoder();        return decoder.decodeBuffer(base64);    }    /**     *      * 此方法描述的是: base 64編碼     *      * @author: Aarony     * @version: 2018年6月20日 下午9:15:14     */    public static String byte2base64(byte[] bytes) {        BASE64Encoder base = new BASE64Encoder();        return base.encode(bytes);    }}

 

AES

package com.aarony.test;import java.io.IOException;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/** *  * 此類描述的是:對稱式加密 *  * @author: Aarony * @version: 2018年6月20日 下午9:20:34 */public class SymmetricEncryptionAESDemo2 {    /**     *      * 此方法描述的是: 解密     *      * @author: Aarony     * @version: 2018年6月20日 下午9:30:52     */    public static byte[] decryptDES(byte[] bytes, SecretKey secretKey) throws Exception {        Cipher cipher = Cipher.getInstance("DES");        cipher.init(Cipher.DECRYPT_MODE, secretKey);        return cipher.doFinal(bytes);    }    /**     *      * 此方法描述的是: 加密     *      * @author: Aarony     * @version: 2018年6月20日 下午9:29:02     */    public static byte[] encryptDES(byte[] bytes, SecretKey secretKey) throws Exception {        Cipher cipher = Cipher.getInstance("DES");        cipher.init(Cipher.ENCRYPT_MODE, secretKey);        return cipher.doFinal(bytes);    }    public static SecretKey loadKeyDES(String base64Key) throws IOException {        byte[] bytes = base642byte(base64Key);        SecretKey key = new SecretKeySpec(bytes, "DES");        return key;    }    /**     *      * 此方法描述的是:擷取base64 key     *      * @author: Aarony     * @version: 2018年6月20日 下午9:25:36     */    public static String genKeyDES() throws NoSuchAlgorithmException {        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");        keyGenerator.init(128);        SecretKey key = keyGenerator.generateKey();        return byte2base64(key.getEncoded());    }    /**     *      * 此方法描述的是:base64 解碼     *      * @author: Aarony     * @version: 2018年6月20日 下午9:16:57     */    public static byte[] base642byte(String base64) throws IOException {        BASE64Decoder decoder = new BASE64Decoder();        return decoder.decodeBuffer(base64);    }    /**     *      * 此方法描述的是: base 64編碼     *      * @author: Aarony     * @version: 2018年6月20日 下午9:15:14     */    public static String byte2base64(byte[] bytes) {        BASE64Encoder base = new BASE64Encoder();        return base.encode(bytes);    }}

 

java 對稱式加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.