You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中实现可逆短编码:将92位Base64字符串缩短并可还原

Reversible Shortening of a 92-Character Cryptographically Secure Base64 String

Hey Matt, great question! Let’s walk through how to safely shorten your Base64 string s into a shorter reversible string n—no data loss, full reversibility guaranteed.

Core Idea: Use a Denser Character Set

Base64 uses 64 unique characters, which means each character carries 6 bits of information. To shorten the string, we need a character set that lets each character carry more bits (since total bits in your data are fixed: 92 chars × 6 bits = 552 bits). More bits per character = fewer total characters.

Here are the most practical, ready-to-use options:

1. Base85 (Standard, Widely Supported)

Base85 uses 85 printable ASCII characters, so each character carries ~6.41 bits. For your 552-bit input:

  • 552 ÷ 6.41 ≈ 86 characters (that’s 6 shorter than your original 92!)

Example Implementation (Python)

import base64

# Encode s to n
s = "your_92_character_crypto_base64_string_here"
raw_bytes = base64.b64decode(s)
n = base64.b85encode(raw_bytes).decode("utf-8")

# Decode n back to s
restored_bytes = base64.b85decode(n)
restored_s = base64.b64encode(restored_bytes).decode("utf-8")

# Verify reversibility
assert restored_s == s

2. Base91 (Even Denser)

Base91 uses 91 printable characters, squeezing ~6.52 bits per character. This cuts your string length to ~85 characters (7 shorter than 92). It’s slightly less standard than Base85, but easy to implement with a third-party library.

Example Implementation (Python)

First install the base91 package (pip install base91), then:

import base64
import base91

# Encode s to n
s = "your_92_character_crypto_base64_string_here"
raw_bytes = base64.b64decode(s)
n = base91.encode(raw_bytes)

# Decode n back to s
restored_bytes = base91.decode(n)
restored_s = base64.b64encode(restored_bytes).decode("utf-8")

assert restored_s == s

3. Custom Dense Character Set (For Maximum Shortening)

If you need even shorter lengths and control over compatible characters (e.g., avoiding URL-reserved symbols), you can use a custom set of up to 94 printable ASCII characters (all non-control characters). This gives ~6.55 bits per character, bringing your string to ~84 characters.

The logic is straightforward:

  • Convert your raw byte array into a single integer.
  • Repeatedly divide the integer by the size of your character set, using remainders to pick characters from your set.
  • Reverse the result to get n.
  • To reverse, convert n back to an integer using your character set’s index values, then convert the integer back to bytes.

Key Notes for All Options

  • Compatibility: Always pick a character set that works with all systems where n will be used (e.g., avoid special characters if storing in filenames, use URL-safe subsets for web usage).
  • Security: Since you’re just re-encoding the raw bytes from the Base64 string, this doesn’t compromise the cryptographic security of your original data.
  • Reversibility: All these methods are fully lossless—you’ll get back the exact original Base64 string s every time.

内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:59:50