base64编码的相关知识点整理


=Start=

缘由:

最近在处理一些字符串编码加密方面的工作,简单梳理一下和base64编码相关的知识,方便后面有需要的时候参考。

正文:

参考解答:

Base64 编码说明和转换过程示例

Base64 是一种基于 64 个可打印字符来表示二进制数据的表示方法,由于 2^6=64,所以每 6 个比特为一个单元,对应某个可打印字符

Base64 常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据,包括 MIME 的电子邮件及 XML 的一些复杂数据。

Base64 编码要求把 3 个 8 位字节(38=24)转化为 4 个 6 位的字节(46=24),之后在 6 位的前面补两个 0,形成 8 位一个字节的形式。 如果剩下的字符不足 3 个字节,则用 0 填充,输出字符使用 =,因此编码后输出的文本末尾可能会出现 1 或 2 个 =。

为了保证所输出的编码位可读字符,Base64 制定了一个编码表,以便进行统一转换。编码表的大小为 2^6=64,这也是 Base64 名称的由来。

在 Base64 中的可打印字符包括字母 A-Z、a-z、数字 0-9,这样共有 62 个字符,此外两个可打印符号在不同的系统中而不同(一般是 + 和 / 字符)。

以下是 Base64 编码的基本步骤

  • 将数据划分为 3 个字节一组(24位)。
  • 将每个字节转换为 8 位二进制形式。
  • 将 24 位数据按照 6 位一组进行划分,得到 4 个 6 位的组。
  • 将每个 6 位的组转换为对应的 Base64 字符。
  • 如果剩下的字符不足 3 个字节,则**用 0 填充,输出字符使用 = **。
  • 将所有转换后的 Base64 字符连接起来,形成最终的编码结果。

编码 Man 的结果为 TWFu ,转换过程如下:

文本Man
ASCII 编码7797110
二进制位010011010110000101101110
索引1922546
Base64 编码TWFu

编码 Ma 的结果为 TWE= ,转换过程如下:

文本Ma用0填充
ASCII 编码77970
二进制位010011010110000100000000
索引19224用0填充的
Base64 编码TWE=

编码 M 的结果为 TQ== ,转换过程如下:

文本M用0填充用0填充
ASCII 编码7700
二进制位010011010000000000000000
索引1916用0填充用0填充
Base64 编码TQ==

解码 Base64 编码的过程与编码相反,将每个 Base64 字符转换为对应的6位二进制值,然后将这些 6 位值组合成原始的二进制数据。

Base64 编码具有以下特点:

  • 编码后的数据长度总是比原始数据长约 1/3 (因为编码是用4个8位字节表示3个8位字节的内容,长度从1变成了4/3,剩下不足3个字节的分组会用0进行填充)。
  • 编码后的数据可以包含 A-Z、a-z、0-9 和两个额外字符的任意组合。
  • Base64 编码是一种可逆的编码方式,可以通过解码还原原始数据。
如何判断一段字符串是否经过base64编码?
字符串长度是4的倍数
=只会出现在字符串最后,可能没有或者有一个等号或者有两个等号
字符串只可能包含A-Z,a-z,0-9,+,/,=字符(其中+和/字符和具体的系统有关)

对于某些字符串可能符合base64编码特征,但是却不是base64编码格式,需要进行进一步判断,如果解码后含有乱码(即ascii码不在32~126),说明虽然符合base64编码特征,但是不是base64编码,那么保留原始字符串
一段统计文件中符合base64编码行比率的代码
#!/usr/bin/env python3
# coding=utf-8

import base64
import sys

'''
a    YQ==
ab    YWI=
abc    YWJj

hello    aGVsbG8=
hello7    aGVsbG83
'''

base64_char_set = {
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
    ,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
    ,'0','1','2','3','4','5','6','7','8','9'
    ,'+','/','='
}

# true->1, false->0
def is_str_base64_encode(astr):

    # base64编码后的数据长度肯定是 4 的倍数
    str_len = len(astr)
    if 0 == str_len or str_len%4 != 0:
        return 0

    # base64编码后的数据中,等号(=)只会出现在字符串最后,可能没有或者一个等号或者两个等号
    if astr.count('=') > 2:
        return 0
    elif astr.count('=') != astr[-2:].count('='):
        return 0

    # base64编码后的字符串只可能包含(A-Z,a-z,0-9,+,/,=)字符
    # 但这个判断涉及到正则处理比较消耗资源就不进行了,最主要是,即便满足这种情况也不一定是base64编码
    for x in astr:
        if x in base64_char_set:
            continue
        else:
            return 0
    #for

    # 直接尝试解码,能解码成功则说明OK,这个最准确
    try:
        base64.b64decode(astr)
    except Exception as e:
        print(e)
        return 0

    return 1

def main():
    line_count = 0
    b64_count = 0
    with open(sys.argv[1], 'rb') as fp:
        for line in fp:
            line = line.decode(errors='ignore').strip() # https://stackoverflow.com/a/50359833
            if line:
                line_count += 1
                b64_count += is_str_base64_encode(line)
            #if
        #for
        print('{0}:b64_line_rate = {3}\nline_count = {1}\nb64_count = {2}\n'.format(sys.argv[1], line_count, b64_count, b64_count/line_count))
    #with

if __name__ == '__main__':
    main()
参考链接:

Base64 编码说明
https://c.runoob.com/front-end/693/

Base64笔记
https://www.ruanyifeng.com/blog/2008/06/base64.html

Base64编码的前世今生
https://www.artacode.com/post/base/base64/

Why do we use Base64?
https://stackoverflow.com/questions/3538021/why-do-we-use-base64

Base64
https://en.wikipedia.org/wiki/Base64

What is base 64 encoding used for?
https://stackoverflow.com/questions/201479/what-is-base-64-encoding-used-for

Base64编码知识详解
https://www.cnblogs.com/jimojianghu/p/15993027.html

java-判断字符串是否经过base64编码
https://blog.csdn.net/u010637238/article/details/103615589

如何识别是否是base64编码加密
https://blog.csdn.net/qq_46541895/article/details/112503930

如何判断一个字符串是否经过base64加密过?
https://www.zhihu.com/question/20304015

如何判断一个字符串是否经过base64加密过?判断是否是base64 java-程序员宅基地
https://www.cxyzjd.com/article/Xionghuimin/103208425

How do you decode Base64 data in Python?
https://stackoverflow.com/questions/3470546/how-do-you-decode-base64-data-in-python

base64 — Base16, Base32, Base64, Base85 Data Encodings
https://docs.python.org/3/library/base64.html

Python的base64模块
https://ixyzero.com/blog/archives/1612.html

Java中的base64编解码和字节数组的读写
https://ixyzero.com/blog/archives/5255.html

Go语言学习#3-加解密、哈希操作
https://ixyzero.com/blog/archives/4130.html

Linux下C语言实现的base64编解码
https://ixyzero.com/blog/archives/3569.html

Linux下C语言实现的字符串zlib压缩和base64编码
https://ixyzero.com/blog/archives/3575.html

=END=


《“base64编码的相关知识点整理”》 有 1 条评论

  1. Cryptography with Python – Quick Guide使用 Python 进行加密 – 快速指南
    https://www.tutorialspoint.com/cryptography_with_python/cryptography_with_python_quick_guide.htm
    `
    Double Strength Encryption 双倍强度加密
    Python Overview and Installation Python 概述和安装
    Cryptography with Python – Reverse Cipher 用 Python 学习密码学 – 可逆密码

    Cryptography with Python – Caesar Cipher 用 Python 进行密码学研究 – 凯撒密码
    Cryptography with Python – ROT13 Algorithm 使用 Python 进行加密 – ROT13 算法

    Transposition Cipher 移位密码
    Encryption of Transposition Cipher 移位密码的加密
    Decryption of Transposition Cipher 移位密码的解密

    Encryption of files 文件加密
    Decryption of files 文件解密

    Base64 Encoding and Decoding Base64 编码和解码

    Cryptography with Python – XOR Process 用 Python 进行加密 – XOR 过程

    Multiplicative Cipher 乘法密码

    Cryptography with Python – Affine Cipher 用 Python 进行加密 – 仿射密码

    Hacking Monoalphabetic Cipher 破解单字母密码

    Simple Substitution Cipher 简单替换密码
    Testing of Simple Substitution Cipher 测试简单替换密码
    Decryption of Simple Substitution Cipher 简单置换密码的解密

    Python Modules of Cryptography 密码学的 Python 模块

    Understanding Vignere Cipher 了解 Vignere 密码
    Implementing Vignere Cipher 实施 Vignere 密码

    One Time Pad Cipher 一次性密码键盘
    Implementation of One Time Pad Cipher 一次性密码键盘的实现

    Symmetric and Asymmetric Cryptography 对称和非对称密码学

    Understanding RSA Algorithm 了解 RSA 算法
    Creating RSA Keys 创建 RSA 密钥
    RSA Cipher Encryption RSA 密码加密
    RSA Cipher Decryption RSA 密码解密
    Hacking RSA Cipher 黑客破解 RSA 密码
    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注