美文网首页
2019-07-24 jdk8后使用 base64替换BASE6

2019-07-24 jdk8后使用 base64替换BASE6

作者: IUE | 来源:发表于2019-07-24 23:39 被阅读0次

jdk8之后,sun.misc.BASE64Decoder和sun.misc.BASE64Encoder被移除

从JDK 1.8开始,就提供了java.util.Base64.Decoder和java.util.Base64.Encoder的JDK公共API,可代替sun.misc.BASE64Decoder和sun.misc.BASE64Encoder的JDK内部API

 

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
//import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class DesUtil {



    /*
     * 加密 1.构造密钥生成器 2.根据ecnodeRules规则初始化密钥生成器 3.产生密钥 4.创建和初始化密码器 5.内容加密 6.返回字符串
     */
    public static String AESEncode(String encodeRules, String content) throws Exception {
        GetCipher getCipher = new GetCipher(encodeRules).invoke();
        SecretKey key = getCipher.getKey();
        Cipher cipher = getCipher.getCipher();
        // 7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
        cipher.init(Cipher.ENCRYPT_MODE, key);
        // 8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
        byte[] byte_encode = content.getBytes("utf-8");
        // 9.根据密码器的初始化方式--加密:将数据加密
        byte[] byte_AES = cipher.doFinal(byte_encode);
        // 10.将加密后的数据转换为字符串
        // 这里用Base64Encoder中会找不到包
        // 解决办法:
        // 在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。
        //String AES_encode=new String(new BASE64Encoder().encode(byte_AES));
        String AES_encode = new String(Base64.encodeBase64(byte_AES));
        // 11.将字符串返回
        return AES_encode;
    }

    /*
     * 解密 解密过程: 1.同加密1-4步 2.将加密后的字符串反纺成byte[]数组 3.将加密内容解密
     */
    public static String AESDncode(String encodeRules, String content) throws Exception {
        GetCipher getCipher = new GetCipher(encodeRules).invoke();
        SecretKey key = getCipher.getKey();
        Cipher cipher = getCipher.getCipher();
        // 7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
        cipher.init(Cipher.DECRYPT_MODE, key);
        // 8.将加密并编码后的内容解码成字节数组
        // byte [] byte_content= new BASE64Decoder().decodeBuffer(content);
        // byte[] byte_content = Base64.getDecoder().decode(content);
        byte[] byte_content = Base64.decodeBase64(content);
        /*
         * 解密
         */
        byte[] byte_decode = cipher.doFinal(byte_content);
        String AES_decode = new String(byte_decode, "utf-8");
        return AES_decode;
    }

    private static class GetCipher {
        private String encodeRules;
        private SecretKey key;
        private Cipher cipher;

        public GetCipher(String encodeRules) {
            this.encodeRules = encodeRules;
        }

        public SecretKey getKey() {
            return key;
        }

        public Cipher getCipher() {
            return cipher;
        }

        public GetCipher invoke() throws NoSuchAlgorithmException, NoSuchPaddingException {
            // 1.构造密钥生成器,指定为AES算法,不区分大小写
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            // 2.根据ecnodeRules规则初始化密钥生成器
            // 生成一个128位的随机源,根据传入的字节数组
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(encodeRules.getBytes());
            keygen.init(128, secureRandom);
//          keygen.init(128, new SecureRandom(encodeRules.getBytes()));
            // 3.产生原始对称密钥
            SecretKey original_key = keygen.generateKey();
            // 4.获得原始对称密钥的字节数组
            byte[] raw = original_key.getEncoded();
            // 5.根据字节数组生成AES密钥
            key = new SecretKeySpec(raw, "AES");
            // 6.根据指定算法AES自成密码器
            cipher = Cipher.getInstance("AES");
            return this;
        }
    }
}

相关文章

  • 2019-07-24 jdk8后使用 base64替换BASE6

    jdk8之后,sun.misc.BASE64Decoder和sun.misc.BASE64Encoder被移除 从...

  • python3 解码base64遇到的问题与解决

    解决方法在解码前使用这个方法 message替换成你要转码的变量名 即可 因为:Base64编码说明Base64编...

  • 加密的各种姿势

    一、Base6 原理: base64的编码都是按字符串长度,以每3个8bit的字符为一组 然后针对每组,首先获取每...

  • flutter字符串多个批量查找和替换

    使用方法:splitMapJoin 替换前: 替换后: 代码:

  • Spel

    前言:此文章主要解决你不能使用jdk8处理集合的情况还有一些模板的使用,使用字符串调用方法1.模板替换 resul...

  • 一个浪漫 gJWsgsW9gKKJhh8wfYSBfeS7fvc

    base64编码 tr 正则替换 gJWsgsW9gKKJhh8wfYSBfeS7fvcQmQ++ 提示:解码规则...

  • 原型的使用

    使用对象的动态特性 直接替换原型对象 直接替换原型会出现的问题 替换原型之后,在替换前创建出来的对象和替换后创建出...

  • BASE64 编码简析

    Base64编码: <1>·Base64编码简介: <2>·使用Base64的原因: <3>·编码原理: 成这个字...

  • Base系列加密解密

    Base编码系列:[Base64][Base32] [Base16] [Base64] Base64编码是使用64...

  • JAVA JDK(8+)实现Base64加密

    /* base64算法是基于64个字符的一种替换算法。base64加密的产生式电子邮件的“历史问题”——邮件只能传...

网友评论

      本文标题:2019-07-24 jdk8后使用 base64替换BASE6

      本文链接:https://www.haomeiwen.com/subject/fjpqrctx.html