您的位置 首页 java

密码算法之Base64编解码原理,原来如此

1、Base64编码介绍

Base64不能称为加解密算法,Base64编码可以把二进制数据转换为可打印的ASCII字符,常用于email消息中的二进制数据编码和HTTP协议中的basic认证。

Base64编码之后的ASCII字符串包括64个可打印字符,如下:

26个大写字母[A…Z]

26个小写字母[a…z]

10个数字[0…9]

2个其它符号,原始Base64中这两个字符是指‘+’,‘/’

Base64编码后的字符串只包含上述字符,是可以在网络上安全传输的,哪怕你的源数据是文本,再无需担心因为控制字符的混淆而丢失数据。

Base64解码是将可打印的ASCII字符转换为二进制数据,并放在byte数组中。而byte数组中的内容是一串不可见的二进制数据。Base64编码的核心作用不在于安全性,而在于让内容能够在各个网关之间无差错的传输。

2、Base64编码原理

Base64编码是按字符串长度,以每3个字符(1Byte=8bit)为一组,然后针对每组,首先获取每个字符的ASCII编码,然后将ASCII编码转换成8bit的二进制,得到一组3*8=24bit的字节。然后再将24bit划分为4个6bit的字节,并在每个6bit的字节前面都填两个高位0,得到4个8bit的字节,然后将这4个8bit的字节转换成十进制,得到编码后的字符。

[1]待转换字符串

[2]字符串转二进制数据流

[3]每3个8位的二进制转换成4个6位二进制

[4]6位二进制转十进制

[5]根据十进制数值从The Base64 Alphabet表中获取字符

[6]转换后的字符串

Base64编码本质上是一种将二进制数据转成文本数据的方案。对于非二进制数据,是先将其转换成二进制形式,然后每连续6bit计算其十进制值,根据该值在A-Z,a-z,0-9,+,/这64个字符中找到对应的字符,最终得到一个文本字符串。

基本规则如下:

标准Base64只有64个字符(英文大小写、数组、+、/)已经作用后缀等号;

Base64是把3个字节编成4个可打印字符,所以Base64编码后的字符串一定能被4整除(不算用作后缀的等号);

等号一定用作后缀,且数目一定是0个、1个或者2个。这是因为如果原文长度不能被3整除,Base64要在后面添加凑齐3n位。为了正确还原,添加了几个就要加上几个等号。

严格来说Base64不能算是一种加密,只能说是编码转换。

3、Base64解码原理

解码原理是将4个字节转换成3个字节,先读入4个6位(用或运算),每次左移6位,再右移3次,每次8位还原。

3.1 对字符串进行Base64编码

 /**
 *Base64Util编解码工具类 
 * @author Administrator
 */
public class Base64Util {
			    private static Base64.Decoder decoder = Base64.getDecoder(); 
					private static Base64.Encoder encoder = Base64.getEncoder();
			    /** 
          * 字符串Base64编码
          */
					public static String getEncodedString(String value){
			        String str = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8)); 
            	return str;
			    }

			测试:
			String str1 = "xiaoming123";
			String s1 = getEncodedString(str1);
			System.out.println("字符串Base64编码结果:" + s1);  

3.2 对字符串进行Base64解码

 /**
 * 字符串Base64解码
 */
public static String getDecodedString(String value){
			    byte[] result = decoder.decode(value.getBytes(StandarCharactes.UTF-8));
  				return new String(result);
			}

测试:
			String str1 = "xiaoming123";
			String s1 = getEncodedString(str1);
			String s2 = getDecodedString(s1);
			System.out.println("字符串Base64解码结果:" + s2);  

3.3 对字符数组进行Base64编码

 /**
 * 字符数组Base64编码
 */
public static String getEncodedByte(byte[] value){
			    String str = encoder.encodeToString(value); 
  				return str;
}  

3.4 对字符数组进行Base64解码

 /**
 * 字符数组Base64解码
 */
public static byte[] getDecodedByte(String value){
			    byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8));
 					 return result;
}  

3.5 对文件进行Base64编码

 /**
 * 文件Base64编码
 */
public static String getEncodeFile(File file){
	    try {
			        //将文件放入文件流中
			        FileInputStream inputStream = new FileInputStream(file);
			        //创建字节数组对象,长度为传入文件的字节长度
			        byte[] buffer = new byte[(int) file.length()];
			        //在流中读取字节数组内容
			        inputStream.read(buffer);
			        //关闭流
			        inputStream.close();
			        //将流中读取到的字节内容做编码处理
			        String str = encoder.encodeToString(buffer);
        			return str;
	    } catch (FileNotFoundException e) {
			        e.printStackTrace();
	    } catch (IOException e) {
			        e.printStackTrace();
	    }
			    return null;
}

测试:
			File file = new File("e:\Client.java");
			String s3 = getEncodeFile(file);
			System.out.println("文件Base64编码结果:" + s3);  

3.6 对文件进行Base64解码

 /**
 * 文件Base64解码
 */
public static String getDecodedFile(File file){
	    try {
			        byte[] decodeBytes = decoder.decode(getEncodeFile(file).getBytes());
			        FileOutputStream outputStream = new FileOutputStream(file);
			        outputStream.write(decodeBytes);
			        outputStream.close();        return  new String(decodeBytes);
	    } catch (FileNotFoundException e) {
			        e.printStackTrace();
	    } catch (IOException e) {
			        e.printStackTrace();
	    }
				    return null;
}

测试:
			String s4 = getDecodedFile(file);
			System.out.println("文件Base64解码结果:" + s4);  

————————————————

愿你就像早晨八九点钟的太阳,活力十足,永远年轻。

————————————————

一入IT深似海,从此学习是常态,上面内容只是冰山一角。关注” JohnnyHL “,决战秋名山,coding到天亮。

文章来源:智云一二三科技

文章标题:密码算法之Base64编解码原理,原来如此

文章地址:https://www.zhihuclub.com/183193.shtml

关于作者: 智云科技

热门文章

网站地图