ZeroBot-Plugin/plugin/crypter/hou.go
github-actions[bot] 35292a69fc
Some checks are pending
打包最新版为 Docker Image / build docker (push) Waiting to run
最新版 / Build binary CI (386, linux) (push) Waiting to run
最新版 / Build binary CI (386, windows) (push) Waiting to run
最新版 / Build binary CI (amd64, linux) (push) Waiting to run
最新版 / Build binary CI (amd64, windows) (push) Waiting to run
最新版 / Build binary CI (arm, linux) (push) Waiting to run
最新版 / Build binary CI (arm64, linux) (push) Waiting to run
PushLint / lint (push) Waiting to run
chore(lint): 改进代码样式 (#1192)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-09-02 13:13:22 +08:00

89 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package crypter 齁语加解密
package crypter
import (
"strings"
)
// 齁语密码表
var houCodebook = []string{
"齁", "哦", "噢", "喔", "咕", "咿", "嗯", "啊",
"", "哈", "", "唔", "哼", "❤", "呃", "呼",
}
// 索引: 0 1 2 3 4 5 6 7
// 8 9 10 11 12 13 14 15
// 创建映射表
var houCodebookMap = make(map[string]int)
// 初始化映射表
func init() {
for idx, ch := range houCodebook {
houCodebookMap[ch] = idx
}
}
func encodeHou(text string) string {
if text == "" {
return "请输入要加密的文本"
}
var encoded strings.Builder
textBytes := []byte(text)
for _, b := range textBytes {
high := (b >> 4) & 0x0F
low := b & 0x0F
encoded.WriteString(houCodebook[high])
encoded.WriteString(houCodebook[low])
}
return encoded.String()
}
func decodeHou(code string) string {
if code == "" {
return "请输入要解密的齁语密文"
}
// 过滤出有效的齁语字符
var validChars []string
for _, r := range code {
charStr := string(r)
if _, exists := houCodebookMap[charStr]; exists {
validChars = append(validChars, charStr)
}
}
if len(validChars)%2 != 0 {
return "齁语密文长度错误,无法解密"
}
// 解密过程
var byteList []byte
for i := 0; i < len(validChars); i += 2 {
highIdx, highExists := houCodebookMap[validChars[i]]
lowIdx, lowExists := houCodebookMap[validChars[i+1]]
if !highExists || !lowExists {
return "齁语密文包含无效字符"
}
originalByte := byte((highIdx << 4) | lowIdx)
byteList = append(byteList, originalByte)
}
result := string(byteList)
if !isValidUTF8(result) {
return "齁语解密失败,结果不是有效的文本"
}
return result
}
// 检查字符串是否为有效的UTF-8编码
func isValidUTF8(s string) bool {
// Go的string类型默认就是UTF-8如果转换没有出错说明是有效的
return len(s) > 0 || s == ""
}