zoukankan      html  css  js  c++  java
  • java 对称加密

    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);
        }
    }
  • 相关阅读:
    实验六 进程基础
    实验五 shell脚本编程
    实验四 Linux系统搭建C语言编程环境
    实验三 Linux系统用户管理及VIM配置
    实验二 Linux系统简单文件操作命令
    实验一 Linux系统与应用准备
    实验八 进程间的通信
    实验七 信号
    实验六 进程基础
    实验五 shell脚本编程
  • 原文地址:https://www.cnblogs.com/wucaifang/p/9206206.html
Copyright © 2011-2022 走看看