Android的Base64理解

什么Base64算法?

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64并不是安全领域的加密算法,其实Base64只能算是一个编码算法,对数据内容进行编码来适合传输。在计算机网络发展的早期,由于“历史原因”,电子邮件不支持非ASCII码字符,如果要传送的电子邮件带有非ASCII码字符(诸如中文)或者图片,用户收到的电子邮件将会是一堆乱码,因此发明了Base64编码算法Base64编码算法是一种用64个字符(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)来表示任意二进制数据的方法。

Base64的编码算法机制?

标准Base64编码解码无需额外信息即完全可逆,即使你自己自定义字符集设计一种类Base64的编码方式用于数据加密,在多数场景下也较容易破解。Base64编码本质上是一种将二进制数据转成文本数据的方案。对于非二进制数据,是先将其转换成二进制形式,然后每连续6比特(2的6次方=64)计算其十进制值,根据该值在A--Z,a--z,0--9,+,/ 这64个字符中找到对应的字符,最终得到一个文本字符串。(比如:“安卓” 这个字符串,是先将其转换成二进制形式,然后每连续6比特(2的6次方=64)计算其十进制值,根据该值(十进制值)在A--Z,a--z,0--9,+,/ 这64个字符中找到对应的字符,最终得到一个文本字符串,即经过Base64编码后的字符串是由(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)64种字符组成的)。


Base64编码的用处?

    在计算机中任何数据都是按ascii码存储的,而ascii码的128~255之间的值是不可见字符。而在网络上交换数据时,比如说从A地传到B地,往往要经过多个路由设备,由于不同的设备对字符的处理方式有一些不同,这样那些不可见字符就有可能被处理错误,这是不利于传输的。所以就先把数据先做一个Base64编码,统统变成可见字符,这样出错的可能性就大降低了。

注意几点:

  • 标准Base64只有64个字符(英文大小写、数字和+、/)以及用作后缀等号;
  • Base64是把3个字节变成4个可打印字符,所以Base64编码后的字符串一定能被4整除(不算用作后缀的等号),即经过Bse64算法后,字符的长度必定能被4整除;
  • 等号(=)一定用作后缀,即在Base64编码后的字符串的最后,且数目一定是0个、1个或2个。这是因为如果原文长度不能被3整除,Base64要在后面添加\0凑齐3n位。为了正确还原,添加了几个\0就加上几个等号。显然添加等号的数目只能是0、1或2;
  • 严格来说Base64不能算是一种加密,只能说是编码转换。

Base64具体实现 看代码:

public class Base64Activity extends AppCompatActivity {

   private EditText editText;
   private String encodedString ;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_base64);
      editText = (EditText) findViewById(R.id.tv);
   }

   /**
    * Base64解密
    * @param view
    */
   public void baseEncode(View view) {
      String str = editText.getText().toString();
      //这个是编码模式(把指定内容编码)
      byte[] result = Base64.encode(str.getBytes(), Base64.DEFAULT);
      editText.setText(new String(result));
   }

   /**
    * Base64加密
    * @param view
    */
   public void baseDecode(View view) {
      String str = editText.getText().toString();
      //这个是加密模式(把指定内容解码)
      byte[] result = Base64.decode(str.getBytes(), Base64.DEFAULT);
      editText.setText(new String(result));

   }

   /**
    * 文件Base64解密
    */
   public void base64FileEncode(){
      File desFile = new File("/storage/emulated/0/pimsecure_debug_1.txt");
      FileOutputStream fos = null;
      try {
         byte[] decodeBytes = Base64.decode(encodedString.getBytes(), Base64.DEFAULT);
         fos = new FileOutputStream(desFile);
         fos.write(decodeBytes);
         fos.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   /**
    * 文件Base64加密
    */
   public void base64FileDecode(){
      File file = new File("/storage/emulated/0/pimsecure_debug.txt");
      FileInputStream inputFile = null;
      try {
         inputFile = new FileInputStream(file);
         byte[] buffer = new byte[(int) file.length()];
         inputFile.read(buffer);
         inputFile.close();
         encodedString = Base64.encodeToString(buffer, Base64.DEFAULT);
         Log.e("Base64", "Base64---->" + encodedString);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_base64"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:text="Hello World!"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="baseEncode"
        android:text="base64编码"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="baseDecode"
        android:text="base64解码"/>
</LinearLayout>

结果就不演示了。

































































猜你喜欢

转载自blog.csdn.net/xiaol206/article/details/71925110