当前位置:   article > 正文

Base64加密解密

base64

一.简介

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64并不是安全领域的加密算法,其实Base64只能算是一个编码算法,对数据内容进行编码来适合传输。标准Base64编码解码无需额外信息即完全可逆,即使你自己自定义字符集设计一种类Base64的编码方式用于数据加密,在多数场景下也较容易破解。Base64编码本质上是一种将二进制数据转成文本数据的方案。对于非二进制数据,是先将其转换成二进制形式,然后每连续6比特(2的6次方=64)计算其十进制值,根据该值在A--Z,a--z,0--9,+,/ 这64个字符中找到对应的字符,最终得到一个文本字符串。基本规则如下几点:

1.标准Base64只有64个字符(英文大小写、数字和+、/)以及用作后缀等号。


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


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


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

二.常用详解

1.Bitmap转Base64

  1. public static String bitmapToBase64(Bitmap bitmap) {
  2. String result = "";
  3. ByteArrayOutputStream bos = null;
  4. try {
  5. if (null != bitmap) {
  6. bos = new ByteArrayOutputStream();
  7. //将bitmap放入字节数组流中
  8. bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bos);
  9. // 将bos流缓存在内存中的数据全部输出,清空缓存
  10. bos.flush();
  11. bos.close();
  12. byte[] bitmapByte = bos.toByteArray();
  13. result = Base64.encodeToString(bitmapByte, Base64.DEFAULT);
  14. }
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. } finally {
  18. if (null != bos) {
  19. try {
  20. bos.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. return result;
  27. }

附:bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bos);

这个是图像压缩的方法,三个参数分别是压缩后的图像的格式(png),图像显示的质量(0—100),100表示最高质量,图像处理的输出流(out)。

Bitmap.compress方法确实可以压缩图片,但压缩的是存储大小,即你放到disk上的大小。

2.Base64转Bitmap

  1. public static Bitmap base64ToBitmap(String base64String) {
  2. byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
  3. Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  4. return bitmap;
  5. }

3.字符串进行Base64编码

  1. public static String stringToBase64(String string){
  2. String encodedString = Base64.encodeToString(string.getBytes(), Base64.DEFAULT);
  3. return encodedString;
  4. }

4.字符串进行Base64解码

  1. public static String base64ToString(String string){
  2. String decodedString =new String(Base64.decode(string,Base64.DEFAULT));
  3. return decodedString;
  4. }

5.对文件进行Base64编码

  1. public static String fileToBase64(File file){
  2. String encodedString="";
  3. FileInputStream inputFile = null;
  4. try {
  5. inputFile = new FileInputStream(file);
  6. byte[] buffer = new byte[(int) file.length()];
  7. inputFile.read(buffer);
  8. inputFile.close();
  9. encodedString = Base64.encodeToString(buffer, Base64.DEFAULT);
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. }
  13. return encodedString;
  14. }

6.对文件进行Base64解码

  1. public static void base64ToFile(String string,File file){
  2. FileOutputStream fos = null;
  3. try {
  4. byte[] decodeBytes = Base64.decode(string.getBytes(), Base64.DEFAULT);
  5. fos = new FileOutputStream(file);
  6. fos.write(decodeBytes);
  7. fos.close();
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. }

7.整个工具类

  1. package com.wjn.okhttpmvpdemo.mode.utils;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.util.Base64;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. /**
  11. * Base64 加密解密 工具类
  12. */
  13. public class Base64Utils {
  14. /**
  15. * Bitmap转Base64
  16. * */
  17. public static String bitmapToBase64(Bitmap bitmap) {
  18. String result = "";
  19. ByteArrayOutputStream bos = null;
  20. try {
  21. if (null != bitmap) {
  22. bos = new ByteArrayOutputStream();
  23. //将bitmap放入字节数组流中
  24. bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bos);
  25. // 将bos流缓存在内存中的数据全部输出,清空缓存
  26. bos.flush();
  27. bos.close();
  28. byte[] bitmapByte = bos.toByteArray();
  29. result = Base64.encodeToString(bitmapByte, Base64.DEFAULT);
  30. }
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. } finally {
  34. if (null != bos) {
  35. try {
  36. bos.close();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. return result;
  43. }
  44. /**
  45. * Base64转Bitmap
  46. * */
  47. public static Bitmap base64ToBitmap(String base64String) {
  48. byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
  49. Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  50. return bitmap;
  51. }
  52. /**
  53. * 字符串进行Base64编码
  54. * */
  55. public static String stringToBase64(String string){
  56. String encodedString = Base64.encodeToString(string.getBytes(), Base64.DEFAULT);
  57. return encodedString;
  58. }
  59. /**
  60. * 字符串进行Base64解码
  61. * */
  62. public static String base64ToString(String string){
  63. String decodedString =new String(Base64.decode(string,Base64.DEFAULT));
  64. return decodedString;
  65. }
  66. /**
  67. * 对文件进行Base64编码
  68. * */
  69. public static String fileToBase64(File file){
  70. String encodedString="";
  71. FileInputStream inputFile = null;
  72. try {
  73. inputFile = new FileInputStream(file);
  74. byte[] buffer = new byte[(int) file.length()];
  75. inputFile.read(buffer);
  76. inputFile.close();
  77. encodedString = Base64.encodeToString(buffer, Base64.DEFAULT);
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. return encodedString;
  82. }
  83. /**
  84. * 对文件进行Base64解码
  85. * */
  86. public static void base64ToFile(String string,File file){
  87. FileOutputStream fos = null;
  88. try {
  89. byte[] decodeBytes = Base64.decode(string.getBytes(), Base64.DEFAULT);
  90. fos = new FileOutputStream(file);
  91. fos.write(decodeBytes);
  92. fos.close();
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }

8.调用

  1. //Bitmap转Base64
  2. Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.zan);
  3. String result= Base64Utils.bitmapToBase64(bitmap);
  4. textView.setText(result);
  5. //Base64转Bitmap
  6. Bitmap bitmap1=Base64Utils.base64ToBitmap(result);
  7. imageView.setImageBitmap(bitmap1);
  8. //字符串使用Base64加密解密
  9. String string="Base64加密解密......";
  10. String encodedString = Base64Utils.stringToBase64(string);
  11. String decodedString =Base64Utils.base64ToString(encodedString);
  12. textView.setText("加密后:"+encodedString+"\n"+"解密后:"+decodedString);
  13. //File转Base64
  14. File file=new File("/storage/emulated/0/base64test.txt");
  15. String string=Base64Utils.fileToBase64(file);
  16. textView.setText(string);
  17. //Base64转File
  18. File newfile=new File("/storage/emulated/0/base64test_new.txt");
  19. Base64Utils.base64ToFile(string,newfile);

9.针对Base64.DEFAULT参数说明

无论是编码还是解码都会有一个参数Flags,Android提供了以下几种

DEFAULT 这个参数是默认,使用默认的方法来加密

NO_PADDING 这个参数是略去加密字符串最后的”=”

NO_WRAP 这个参数意思是略去所有的换行符(设置后CRLF就没用了)

CRLF 这个参数看起来比较眼熟,它就是Win风格的换行符,意思就是使用CR LF这一对作为一行的结尾而不是Unix风格的LF

URL_SAFE 这个参数意思是加密时不使用对URL和文件名有特殊意义的字符来作为加密字符,具体就是以-和_取代+和/

10.总结

Base64编码看似简单,但是其在实际开发中使用相当广泛。目前项目中只是用到这么多,以后用到更复杂的情况的时候再做补充。

11.补充

使用Base64方式转换Bitmap与字符流时会涉及编码和解码

编码

  1. String url="http://www.abc.com.8080:/abc/def/ghi/aaa.html?name=张三&age=30";
  2. String encodeContent = null;
  3. try {
  4. encodeContent = URLEncoder.encode(url, "utf-8");
  5. } catch (UnsupportedEncodingException e) {
  6. e.printStackTrace();
  7. }
  8. Log.d("TAG", "encodeContent----:" + encodeContent);

解码

  1. String decodeContent = null;
  2. try {
  3. decodeContent = URLDecoder.decode(encodeContent, "utf-8");
  4. } catch (UnsupportedEncodingException e) {
  5. e.printStackTrace();
  6. }
  7. Log.d("TAG", "decodeContent----:" + decodeContent);

结果

  1. encodeContent----:http%3A%2F%2Fwww.abc.com.8080%3A%2Fabc%2Fdef%2Fghi%2Faaa.html%3Fname%3D%E5%BC%A0%E4%B8%89%26age%3D30
  2. decodeContent----:http://www.abc.com.8080:/abc/def/ghi/aaa.html?name=张三&age=30

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/202853
推荐阅读
相关标签
  

闽ICP备14008679号