From a715f9c49f732908691341951b1643f7b8fae91b Mon Sep 17 00:00:00 2001
From: ccy <3140717620@qq.com>
Date: 星期五, 23 十月 2026 14:48:42 +0800
Subject: [PATCH] 将加密字符修改为全部是英文大小写,去掉之前奇怪的标识符号

---
 shortcrypto.cpp |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/shortcrypto.cpp b/shortcrypto.cpp
index e6d80fd..f373ba2 100644
--- a/shortcrypto.cpp
+++ b/shortcrypto.cpp
@@ -12,6 +12,10 @@
 const char *ShortCrypto::V3_CHARS =
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
+// V2 瀛楃闆嗭細澶у皬鍐欏瓧姣嶏紙52瀛楃锛夛紝璁惧ID鍙惈瀛楁瘝銆佷笉鍚暟瀛椾笌鐗规畩瀛楃
+const char *ShortCrypto::BASE52_CHARS =
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
 namespace {
     QByteArray pkcs7Pad(const QByteArray &data, int blockSize) {
         int padLen = blockSize - (data.size() % blockSize);
@@ -63,8 +67,8 @@
                     reinterpret_cast<const uint8_t*>(adjustedIv.constData()));
     AES_CBC_encrypt_buffer(&ctx, reinterpret_cast<uint8_t*>(encrypted.data()), encrypted.size());
 
-    // 3. Base85 缂栫爜锛岀缉鐭瓧绗︽暟
-    QByteArray encoded = base85Encode(encrypted);
+    // 3. 绾瓧姣嶇紪鐮侊紙52瀛楃闆嗭級锛�48瀛楄妭瀵嗘枃 鈫� 68瀛楃
+    QByteArray encoded = baseNEncode(encrypted, BASE52_CHARS, 52);
     return QString::fromLatin1(encoded);
 }
 
@@ -83,10 +87,18 @@
         adjustedIv.append(QByteArray(16 - adjustedIv.size(), '\0'));
     }
 
-    // 1. Base85 瑙g爜
-    QByteArray encrypted = base85Decode(ciphertext);
+    // 1. 浼樺厛鎸夌函瀛楁瘝琛紙52瀛楃闆嗭級瑙g爜锛�52^瀛楃鏁� 涓� 256瀛楄妭鐣� 鐨勫ご绌洪棿涓嶈冻1瀛楄妭锛�
+    //    鐪熷�艰嚦澶氫涪澶�1涓墠瀵奸浂瀛楄妭锛岄暱搴︿笉瀵归綈鏃惰ˉ鍥�
+    QByteArray encrypted = baseNDecodeVar(ciphertext, BASE52_CHARS, 52);
+    if (!encrypted.isEmpty() && encrypted.size() % 16 != 0) {
+        encrypted.prepend('\0');
+    }
+    if (encrypted.isEmpty() || encrypted.size() % 16 != 0) {
+        // 鍥為��锛氭棭鏈烞ase85鐗堝瘑鏂�
+        encrypted = base85Decode(ciphertext);
+    }
     if (encrypted.isEmpty()) {
-        qWarning() << "ShortCrypto::decrypt: base85 decode failed";
+        qWarning() << "ShortCrypto::decrypt: decode failed";
         return QByteArray();
     }
 
@@ -234,6 +246,44 @@
     return num;
 }
 
+QByteArray ShortCrypto::baseNDecodeVar(const QString &text, const char *alphabet, int base)
+{
+    // 寤虹珛瀛楃绱㈠紩琛�
+    int charToVal[256];
+    memset(charToVal, -1, sizeof(charToVal));
+    for (int i = 0; i < base; ++i) {
+        charToVal[static_cast<unsigned char>(alphabet[i])] = i;
+    }
+
+    // 灏忕鍔ㄦ�佸瓧鑺傛暟缁勬墽琛� Horner锛歯um = num * base + v
+    // num[0] 涓烘渶浣庡瓧鑺傦紝杩涗綅鍚戝熬閮紙楂樹綅锛変紶鎾�
+    QByteArray num(1, '\0');
+    QByteArray input = text.toLatin1();
+    for (int i = 0; i < input.size(); ++i) {
+        int v = charToVal[static_cast<unsigned char>(input.at(i))];
+        if (v < 0) {
+            qWarning() << "ShortCrypto::baseNDecodeVar: invalid character:" << QChar(input.at(i));
+            return QByteArray();
+        }
+        unsigned int carry = static_cast<unsigned int>(v);
+        for (int b = 0; b < num.size(); ++b) {
+            unsigned int cur = (static_cast<unsigned int>(static_cast<unsigned char>(num.at(b))) * static_cast<unsigned int>(base)) + carry;
+            num[b] = static_cast<char>(cur & 0xFF);
+            carry = cur >> 8;
+        }
+        while (carry != 0) {
+            num.append(static_cast<char>(carry & 0xFF));
+            carry >>= 8;
+        }
+    }
+
+    // 杞ぇ绔苟鍘绘帀鍓嶅闆讹紙鍙橀暱妯″紡涓嬪墠瀵奸浂鐨勭湡瀹炰釜鏁扮敱璋冪敤鏂硅ˉ榻愯鍒欐仮澶嶏級
+    std::reverse(num.begin(), num.end());
+    int firstNonZero = 0;
+    while (firstNonZero < num.size() - 1 && num.at(firstNonZero) == 0) ++firstNonZero;
+    return num.mid(firstNonZero);
+}
+
 QByteArray ShortCrypto::base85Encode(const QByteArray &data)
 {
     QByteArray result;

--
Gitblit v1.9.1