ubuntu base64 encode/decode

本文深入探讨了Base64编码的原理与应用,包括其在MIME协议中的角色,生成和解码的方法,以及在Python中的实现技巧。Base64编码是一种快速、易于识别但不安全的数据转换方式,适合于非关键信息的加密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍

Base64编码是一种“防君子不防小人”的编码方式。广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符。
优点:速度快,ascii字符,肉眼不可理解
缺点:编码比较长,非常容易被破解,仅适用于加密非关键信息的场合

  • 生成SECRETY_KEY
In [3]: import os

In [5]: import base64

In [6]: s = os.urandom(64)

In [7]: SECRETY_KEY = base64.b64encode(s)

In [8]: print(SECRETY_KEY)
b'VKXV/D7japENU5OcH+40gvd/1RyfBV9OSbll71twKAK1xfmFNgxPscfmvawR8LYQGIrl1mpiT4tBTmdbKVyHqg=='

python3 base64

base64 encoding takes 8-bit binary byte data and encodes it uses only the characters A-Z, a-z, 0-9, +, /* so it can be transmitted over channels that do not preserve all 8-bits of data, such as email.

Hence, it wants a string of 8-bit bytes. You create those in Python 3 with the b’’ syntax.

If you remove the b, it becomes a string. A string is a sequence of Unicode characters. base64 has no idea what to do with Unicode data, it’s not 8-bit. It’s not really any bits, in fact. ?

import base64
def stringToBase64(s):
    return base64.b64encode(s.encode('utf-8'))

def base64ToString(b):
    return base64.b64decode(b).decode('utf-8')
quickly generate a value for Flask.secret_key (or SECRET_KEY):
$ python -c 'import os; print(os.urandom(16))'
b'_5#y2L"F4Q8z\n\xec]/'

base64 - base64 encode/decode data and print to standard output

  • SYNOPSIS
    base64 [OPTION]… [FILE]
  • DISCRIPTION
# man base64
       Base64 encode or decode FILE, or standard input, to standard output.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -d, --decode
              decode data

       -i, --ignore-garbage
              when decoding, ignore non-alphabet characters

       -w, --wrap=COLS
              wrap  encoded  lines  after  COLS  character  (default  76).  Use 0 to disable line
              wrapping

       --help display this help and exit

       --version
              output version information and exit

       The data are encoded as described for the base64 alphabet in RFC 4648.  When decoding, the
       input  may  contain  newlines in addition to the bytes of the formal base64 alphabet.  Use
       --ignore-garbage to attempt to recover from any other non-alphabet bytes  in  the  encoded
       stream.

一、base64加密

格式:base64

从标准输入中读取数据,按Ctrl + D结束输入。将输入的内容编码为base64字符串输出。

格式:echo
“str” | base64
将字符串str + 换行
编码为base64字符串输出。

格式:echo - n
“str” | base64
将字符串str编码为base64字符串输出。

格式:base64
file
从指定的文件file中读取数据,编码为base64字符串输出。

➜  ~ base64 
hello
aGVsbG8K
➜  ~ echo "hello" | base64
aGVsbG8K

二、base64解密

格式:base64 - d
从标准输入中读取已经进行base64编码的内容,解码输出。

格式:base64 - d - i
从标准输入中读取已经进行base64编码的内容,解码输出。加上 - i参数,忽略非字母表字符,比如换行符。

格式:echo
“str” | base64 - d
将base64编码的字符串str + 换行
解码输出。

格式:echo - n
“str” | base64 - d
将base64编码的字符串str解码输出。 注意与上面的差别。

格式:base64 - d
file
从指定的文件file中读取base64编码的内容,解码输出。

➜  ~ base64 -d           
aGVsbG8K
hello
➜  ~ echo "aGVsbG8K" | base64 -d
hello

使用python执行系统命令

import os
os.system(''' echo -n "aGVsbG8K" | base64 -d ''')

ubuntu3个通配符

  • 匹配任意多个任何字符
    ? 匹配一个字符
    [] 匹配中括号里面的一个字符
(base) dwh@dwh:~$ dpkg -l | grep -i nvidia ii libnvidia-cfg1-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA binary OpenGL/GLX configuration library ii libnvidia-common-470 470.256.02-0ubuntu0.20.04.1 all Shared files used by the NVIDIA libraries ii libnvidia-compute-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA libcompute package ii libnvidia-compute-470:i386 470.256.02-0ubuntu0.20.04.1 i386 NVIDIA libcompute package rc libnvidia-compute-535:amd64 535.183.01-0ubuntu0.20.04.1 amd64 NVIDIA libcompute package ii libnvidia-decode-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA Video Decoding runtime libraries ii libnvidia-decode-470:i386 470.256.02-0ubuntu0.20.04.1 i386 NVIDIA Video Decoding runtime libraries ii libnvidia-encode-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 NVENC Video Encoding runtime library ii libnvidia-encode-470:i386 470.256.02-0ubuntu0.20.04.1 i386 NVENC Video Encoding runtime library ii libnvidia-extra-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 Extra libraries for the NVIDIA driver ii libnvidia-fbc1-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA OpenGL-based Framebuffer Capture runtime library ii libnvidia-fbc1-470:i386 470.256.02-0ubuntu0.20.04.1 i386 NVIDIA OpenGL-based Framebuffer Capture runtime library ii libnvidia-gl-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA OpenGL/GLX/EGL/GLES GLVND libraries and Vulkan ICD ii libnvidia-gl-470:i386 470.256.02-0ubuntu0.20.04.1 i386 NVIDIA OpenGL/GLX/EGL/GLES GLVND libraries and Vulkan ICD ii libnvidia-ifr1-470:amd64 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA OpenGL-based Inband Frame Readback runtime library ii libnvidia-ifr1-470:i386 470.256.02-0ubuntu0.20.04.1 i386 NVIDIA OpenGL-based Inband Frame Readback runtime library ii nvidia-compute-utils-470 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA compute utilities rc nvidia-compute-utils-535 535.183.01-0ubuntu0.20.04.1 amd64 NVIDIA compute utilities ii nvidia-dkms-470 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA DKMS package rc nvidia-dkms-535 535.183.01-0ubuntu0.20.04.1 amd64 NVIDIA DKMS package ii nvidia-driver-470 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA driver metapackage ii nvidia-kernel-common-470 470.256.02-0ubuntu0.20.04.1 amd64 Shared files used with the kernel module rc nvidia-kernel-common-535 535.183.01-0ubuntu0.20.04.1 amd64 Shared files used with the kernel module ii nvidia-kernel-source-470 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA kernel source package ii nvidia-prime 0.8.16~0.20.04.2 all Tools to enable NVIDIA's Prime ii nvidia-settings 470.57.01-0ubuntu0.20.04.3 amd64 Tool for configuring the NVIDIA graphics driver ii nvidia-utils-470 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA driver support binaries ii screen-resolution-extra 0.18build1 all Extension for the nvidia-settings control panel ii xserver-xorg-video-nvidia-470 470.256.02-0ubuntu0.20.04.1 amd64 NVIDIA binary Xorg driver
04-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值