码迷,mamicode.com
首页 > 编程语言 > 详细

java 对称加密

时间:2018-06-20 22:44:51      阅读:227      评论:0      收藏:0      [点我收藏+]

标签: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 对称加密

标签:TE   nbsp   pac   key   des   ini   turn   dem   encrypt   

原文地址:https://www.cnblogs.com/wucaifang/p/9206206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!