且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Base64 加密与解密|学习笔记

更新时间:2022-09-04 07:50:12

开发者学堂课程【Java 高级编程Base64 加密与解密】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/20/detail/359


Base64 加密与解密


内容介绍:

1. Base64 的两个加密类

2. 范例:实现加密与解密操作.

3. 范例:使用盐值加密

4. 范例:多次加密处理

 

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

• Base64.Encoder:进行加密才能处理;

– 加密处理:public byte[] encode(byte[] src);

• Base64.Decoder:进行解密处理;

– 解密处理: public byte[] decode(String src);

 

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

public class avaAPIDemo { 

public static void main(string[ ] args) throws Exception { 

String msg =  “www .mldn.cn" ; //要发送的信息内容

string encMsg = new string(Base64.getEncoder( ).encode(msg.getBytes())) ; 

//数据加密

system. out.print1n(encMsg); 

string oldMsg = new string(Base64.getDecoder( ) .decode(encMsg)); 

system.out.print1n(oldMsg);

虽然 Base64 可以实现加密与解密的处理,但是其由于其是一个公版的算法,所以如果对其进行加密,***的做法是使用盐值操作。+

 

范例:使用盐值加密

public class avaAPIDemo {  

public static void main(string[ ] args) throws Exception { 

String salt = "mldnjava"; 

String msg =  “www .mldn.cn" + "+ salt +" ; //要发送的信息内容

string encMsg = new string(Base64.getEncoder( ).encode(msg.getBytes())) ; 

//数据加密

system. out.print1n(encMsg); 

string oldMsg = new string(Base64.getDecoder( ) .decode(encMsg)); 

system.out.print1n(oldMsg);

即便有盐值处理,但加密效果并不完善,所以要用到多次加密

 

范例:多次加密处理

c1ass stringutil { 

private static final string sALT= "mldnjava" ; //公共的盐值

private static final int REPEAT = 5 ; //加密次数

/** 

*加密处理 

*@param str要加密的字符串,需要与盐值整合 

*@param repeat 加密的重复次数 

*@return 加密后的数据 

*/ 

public static String encode(String str) {//加密处理

String temp = str + "{"+ SALT + “}; //盐值对外不公布

byte data [] = temp.getBytes() ; //将字符串变为字节数组

for (int x= e ; x <REPEAT ; ×++) { 

data = Base64.getEncoder( ).encode(data) ;//重复加密

} 

return .new string(data) ; 

} 

***的做法是使用 2-3 种加密程序,同时在找到一些完全不可解密的加密算法。