• Java Base64位加密和解密(包括其他加密参考)


    链接https://blog.csdn.net/longguangfu8/article/details/78948213

    常用加密解密算法【RSA、AES、DES、MD5】介绍和使用

    https://blog.csdn.net/u013565368/article/details/53081195

    java 加密工具类(MD5、RSA、AES等加密方式

    https://blog.csdn.net/u011659172/article/details/50627903

    关于base64编码Encode和Decode编码的几种方式

    Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便。在实际应用上,Base64除了能将Binary资料可视化之外,也常用来表示字串加密过后的内容。如果要使用Java 程式语言来实作Base64的编码与解码功能,可以参考本篇文章的作法。

    早期作法

    早期在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类别,用法如下:

    1.  
      final BASE64Encoder encoder = new BASE64Encoder();
    2.  
      final BASE64Decoder decoder = new BASE64Decoder();
    3.  
      final String text = "字串文字";
    4.  
      final byte[] textByte = text.getBytes("UTF-8");
    5.  
      //编码
    6.  
      final String encodedText = encoder.encode(textByte);
    7.  
      System.out.println(encodedText);
    8.  
      //解码
    9.  
      System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));
    10.  
       
    11.  
      final BASE64Encoder encoder = new BASE64Encoder();
    12.  
      final BASE64Decoder decoder = new BASE64Decoder();
    13.  
      final String text = "字串文字";
    14.  
      final byte[] textByte = text.getBytes("UTF-8");
    15.  
      //编码
    16.  
      final String encodedText = encoder.encode(textByte);
    17.  
      System.out.println(encodedText);
    18.  
       
    19.  
      //解码
    20.  
      System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));

    从以上程式可以发现,在Java用Base64一点都不难,不用几行程式码就解决了!只是这个sun.mis c套件所提供的Base64功能,编码和解码的效率并不太好,而且在以后的Java版本可能就不被支援了,完全不建议使用。

    Apache Commons Codec作法

    Apache Commons Codec有提供Base64的编码与解码功能,会使用到org.apache.commons.codec.binary套件下的Base64类别,用法如下:

    1.  
      final Base64 base64 = new Base64();
    2.  
      final String text = "字串文字";
    3.  
      final byte[] textByte = text.getBytes("UTF-8");
    4.  
      //编码
    5.  
      final String encodedText = base64.encodeToString(textByte);
    6.  
      System.out.println(encodedText);
    7.  
      //解码
    8.  
      System.out.println(new String(base64.decode(encodedText), "UTF-8"));
    9.  
       
    10.  
      final Base64 base64 = new Base64();
    11.  
      final String text = "字串文字";
    12.  
      final byte[] textByte = text.getBytes("UTF-8");
    13.  
      //编码
    14.  
      final String encodedText = base64.encodeToString(textByte);
    15.  
      System.out.println(encodedText);
    16.  
      //解码
    17.  
      System.out.println(new String(base64.decode(encodedText), "UTF-8"));

    以上的程式码看起来又比早期用sun.mis c套件还要更精简,效能实际执行起来也快了不少。缺点是需要引用Apache Commons Codec,很麻烦。

    Java 8之后的作法

    Java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码,用法如下:

    1.  
      final Base64.Decoder decoder = Base64.getDecoder();
    2.  
      final Base64.Encoder encoder = Base64.getEncoder();
    3.  
      final String text = "字串文字";
    4.  
      final byte[] textByte = text.getBytes("UTF-8");
    5.  
      //编码
    6.  
      final String encodedText = encoder.encodeToString(textByte);
    7.  
      System.out.println(encodedText);
    8.  
      //解码
    9.  
      System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
    10.  
       
    11.  
      final Base64.Decoder decoder = Base64.getDecoder();
    12.  
      final Base64.Encoder encoder = Base64.getEncoder();
    13.  
      final String text = "字串文字";
    14.  
      final byte[] textByte = text.getBytes("UTF-8");
    15.  
      //编码
    16.  
      final String encodedText = encoder.encodeToString(textByte);
    17.  
      System.out.println(encodedText);
    18.  
      //解码
    19.  
      System.out.println(new String(decoder.decode(encodedText), "UTF-8"));

    与sun.mis c套件和Apache Commons Codec所提供的Base64编解码器来比较的话,Java 8提供的Base64拥有更好的效能。实际测试编码与解码速度的话,Java 8提供的Base64,要比sun.mis c套件提供的还要快至少11倍,比Apache Commons Codec提供的还要快至少3倍。因此在Java上若要使用Base64,这个Java 8底下的java .util套件所提供的Base64类别绝对是首选!

  • 相关阅读:
    HDFS体系结构
    HDFS核心设计
    1)HDFS分布式文件系统 2)HDFS核心设计 3 )HDFS体系结构
    大数据的特征
    zookeeper原理
    Hadoop安装手册
    微信公众号开发
    一、Ajax 二、JSON数据格式 三、Ajax+Jquery 四、分页的实现
    设计模式--工厂模式
    Spring MVC 配置文件设置全局编码
  • 原文地址:https://www.cnblogs.com/jianzhixuan/p/9882132.html
Copyright © 2020-2023  润新知