• Java进阶开发-基于Base64的加密与解密操作


    基于Base64的加密与解密操作

      正常来讲加密基本上永远伴随着解密,所谓的加密或者解密往往都是需要有一些所谓的规则。在JDK1.8开始提供有一组新的加密处理操作,Base64处理。在这个类里面有两个内部类:

    Base64.Encoder

    加密处理

    public byte[] encode(byte[] src)

    Base64.Decoder

    解密处理

    public byte[] decode(String src)

      ·范例:实现加密与解密操作

     1 import java.util.Base64;
     2 public class Main {
     3     public static void main(String[] args) {
     4         String msg="hello,wanyu!";//要加密的信息
     5         String encMsg;
     6         String decMsg;
     7         encMsg=new String(Base64.getEncoder().encode(msg.getBytes()));
     8         decMsg=new String(Base64.getDecoder().decode(encMsg.getBytes()));
     9         System.out.println(encMsg);
    10         System.out.println(decMsg);
    11     }
    12 }

        虽然Base64可以实现加密与解密的处理,但是其由于式一公版的算法,所以如果直接对数据进行加密往往并不安全,那么最好的做法是使用盐值操作。

      ·范例:盐值加密

     1 import java.util.Base64;
     2 public class Main {
     3     public static void main(String[] args) {
     4         String salt="Mufasa";
     5         String msg="hello,wanyu!"+"{"+salt+"}";//要加密的信息
     6         String encMsg;
     7         String decMsg;
     8         encMsg=new String(Base64.getEncoder().encode(msg.getBytes()));
     9         decMsg=new String(Base64.getDecoder().decode(encMsg.getBytes()));
    10         System.out.println(encMsg);
    11         System.out.println(decMsg);
    12     }
    13 }

        即便现在有盐值实际上发现加密效果也不是很好,最好的做法是多次加密。

      ·范例:基于Base64的自定义加密

     1 import java.util.Base64;
     2 class StringUtil{
     3     private static final String SALT="Mufasa";  //公共的盐值
     4     private static final int REPEAT = 3;        //加密3次
     5     /**
     6      * 加密处理
     7      * @param str 要加密的字符串,需要与盐值整合
     8      * @return 加密后的数据
     9      */
    10     public static String encode(String str){
    11         String temp=str+"{"+SALT+"}";//盐值不对外公布
    12         byte[] data = temp.getBytes();//将字符串变为字节数组
    13         for(int x=0;x<REPEAT;x++){
    14             data=Base64.getEncoder().encode(data);//重复加密
    15         }
    16         return new String(data);
    17     }
    18     /**
    19      * 进行解密处理
    20      * @param str 输入的密文
    21      * @return 返回明文
    22      */
    23     public static String decode(String str){
    24         byte[] data=str.getBytes();
    25         for(int x=0;x<REPEAT;x++){
    26             data=Base64.getDecoder().decode(data);
    27         }   //正则表达式
    28         return new String(data).replaceAll("\{\w+\}","");
    29     }
    30 }
    31 public class Main {
    32     public static void main(String[] args) {
    33         String encStr=StringUtil.encode("hello,this is a Base64 code");
    34         System.out.println(encStr);
    35         String decStr=StringUtil.decode(encStr);
    36         System.out.println(decStr);
    37     }
    38 }

      其中盐值SALT、重复次数REPEAT自定义。最好的做法是使用2-3种加密程序,同时进行、同时找到一些完全不可解密的操作

  • 相关阅读:
    转载:oracle事务的ACID特性
    oracle对象之存储函数
    转载:散列冲突的解决策略
    [Linux]Redhat7配置本地镜像源
    [Linux]Redhat7配置CentOS7 YUM源
    java120经典面试题
    Dijkstra算法
    树的重构
    第三章垃圾收集器与内存分配策略
    内存区域与内存溢出异常
  • 原文地址:https://www.cnblogs.com/Mufasa/p/11136413.html
Copyright © 2020-2023  润新知