Base64加密解密

Base64 是一种基于 64 个可打印字符来表示二进制数据的编码方式,常用于需要通过文本协议传输二进制数据的场景(如 URL、邮件)。以下是不同场景下生成 Base64 编码的方法:

一、编程语言实现

  1. Python
python 复制代码
import base64

# 字符串转Base64
text = "Hello, World!"
encoded = base64.b64encode(text.encode('utf-8')).decode('utf-8')
print(encoded)  # 输出: SGVsbG8sIFdvcmxkIQ==

# Base64转回字符串
decoded = base64.b64decode(encoded).decode('utf-8')
print(decoded)  # 输出: Hello, World!
  1. JavaScript (浏览器环境)
javascript 复制代码
// 字符串转Base64
const text = "Hello, World!";
const encoded = btoa(unescape(encodeURIComponent(text)));
console.log(encoded); // 输出: SGVsbG8sIFdvcmxkIQ==

// Base64转回字符串
const decoded = decodeURIComponent(escape(atob(encoded)));
console.log(decoded); // 输出: Hello, World!
  1. Java
java 复制代码
import java.util.Base64;

public class Main {
    public static void main(String[] args) {
        // 字符串转Base64
        String text = "Hello, World!";
        String encoded = Base64.getEncoder().encodeToString(text.getBytes());
        System.out.println(encoded); // 输出: SGVsbG8sIFdvcmxkIQ==

        // Base64转回字符串
        byte[] decodedBytes = Base64.getDecoder().decode(encoded);
        String decoded = new String(decodedBytes);
        System.out.println(decoded); // 输出: Hello, World!
    }
}

二、命令行工具

  1. Linux/macOS
bash 复制代码
# 字符串转Base64
echo -n "Hello, World!" | base64
# 输出: SGVsbG8sIFdvcmxkIQ==

# Base64转回字符串
echo "SGVsbG8sIFdvcmxkIQ==" | base64 -d
# 输出: Hello, World!

# 文件转Base64
base64 image.jpg > image.txt

# Base64转回文件
base64 -d image.txt > restored.jpg
  1. Windows PowerShell
powershell 复制代码
# 字符串转Base64
[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("Hello, World!"))
# 输出: SGVsbG8sIFdvcmxkIQ==

# Base64转回字符串
[System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("SGVsbG8sIFdvcmxkIQ=="))
# 输出: Hello, World!

三、在线工具

直接使用浏览器访问以下网站:

Base64 Encode/Decode

CyberChef(功能更强大)

四、URL 安全的 Base64

标准 Base64 中的 + 和 / 在 URL 中可能被转义,需替换为 - 和 _,并移除结尾的 =:

python 复制代码
# Python示例
import base64

text = "Hello, World!"
standard = base64.b64encode(text.encode('utf-8')).decode('utf-8')
url_safe = standard.replace('+', '-').replace('/', '_').rstrip('=')
print(url_safe)  # 输出: SGVsbG8sIFdvcmxkIQ

五、注意事项

编码类型:确保编解码时使用相同的字符集(如 UTF-8)。

二进制数据:Base64 编码会使数据体积增加约 33%(每 3 字节转为 4 字节)。

安全性:Base64 是编码而非加密,不具备安全性,敏感数据需配合加密算法使用。

通过以上方法,你可以在不同场景下生成和解析 Base64 编码。

相关推荐
啊阿狸不会拉杆2 分钟前
《机器学习导论》第3章 -贝叶斯决策理论
人工智能·python·算法·机器学习·numpy·深度优先·贝叶斯决策理论
阿蔹3 分钟前
力扣面试题二Python
python·算法·leetcode·职场和发展
2501_9421917712 分钟前
汽车脏污检测与识别 - YOLO11-C3k2-PSFSConv优化模型详解
python
喵手15 分钟前
Python爬虫实战:构建“下载-去重-入库”的图片采集流水线(附SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·sqlite持久化存储·采集图片·采集图片存储入库
爱内卷的学霸一枚16 分钟前
Python并发编程与性能优化实战指南
开发语言·python·性能优化
Blurpath住宅代理19 分钟前
如何在Python爬虫中使用代理IP?从配置到轮换的完整指南
网络·爬虫·python·住宅ip·住宅代理·动态住宅代理
DeniuHe22 分钟前
Pytorch中统计学相关的函数
pytorch·python·深度学习
newbiai22 分钟前
电商直播AI视频生成工具哪个方便快捷?
人工智能·python·音视频
ID_1800790547324 分钟前
Python结合淘宝关键词API进行商品数据挖掘与
开发语言·python·数据挖掘
梦因you而美24 分钟前
Python win32com操作Excel:彻底禁用链接更新及各类弹窗(实测有效)
python·excel·win32com·禁用链接更新·excel弹框