Go语言实现AzDG可逆加密算法实例
作者:不吃皮蛋 时间:2024-02-12 17:50:01
本文实例讲述了Go语言实现AzDG可逆加密算法。分享给大家供大家参考。具体实现方法如下:
package main
import (
"fmt"
"crypto/md5"
"encoding/base64"
"time"
)
var cipher = "密鑰"
var h = md5.New()
func cipherEncode(sourceText string) string {
h.Write([]byte(cipher))
cipherHash := fmt.Sprintf("%x", h.Sum(nil))
h.Reset()
inputData := []byte(sourceText)
loopCount := len(inputData)
outData := make([]byte,loopCount)
for i:= 0; i < loopCount ; i++ {
outData[i] = inputData[i] ^ cipherHash[i%32]
}
return fmt.Sprintf("%s", outData)
}
func encode(sourceText string) string {
h.Write([]byte(time.Now().Format("2006-01-02 15:04:05")))
noise := fmt.Sprintf("%x", h.Sum(nil))
h.Reset()
inputData := []byte(sourceText)
loopCount := len(inputData)
outData := make([]byte,loopCount*2)
for i, j := 0,0; i < loopCount ; i,j = i+1,j+1 {
outData[j] = noise[i%32]
j++
outData[j] = inputData[i] ^ noise[i%32]
}
return base64.StdEncoding.EncodeToString([]byte(cipherEncode(fmt.Sprintf("%s", outData))))
}
func decode(sourceText string) string {
buf, err := base64.StdEncoding.DecodeString(sourceText)
if err != nil {
fmt.Println("Decode(%q) failed: %v", sourceText, err)
return ""
}
inputData := []byte(cipherEncode(fmt.Sprintf("%s", buf)))
loopCount := len(inputData)
outData := make([]byte,loopCount)
for i, j := 0,0; i < loopCount ; i,j = i+2,j+1 {
outData[j] = inputData[i] ^ inputData[i+1]
}
return fmt.Sprintf("%s", outData)
}
func main() {
s := encode("張學友")
fmt.Println(s)
fmt.Println(decode(s))
}
希望本文所述对大家的Go语言程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
扩展性很好的一个分页存储过程分享
高性能PHP框架Symfony2经典入门教程
Python如何加载模型并查看网络
![](https://img.aspxhome.com/file/2023/3/115123_0s.png)
Python zip函数打包元素实例解析
MySQL5.7.10 安装文档教程详解
浅谈python中真正关闭socket的方法
Python实现基于POS算法的区块链
![](https://img.aspxhome.com/file/2023/1/67291_0s.png)
pycharm解决关闭flask后依旧可以访问服务的问题
![](https://img.aspxhome.com/file/2023/7/67747_0s.jpg)
MySQL数据库磁盘优化
javascript中注册和移除事件的4种方式
Pytorch中的model.train() 和 model.eval() 原理与用法解析
基于Python编写一个简单的垃圾邮件分类器
浅谈python中scipy.misc.logsumexp函数的运用场景
如何在Linux 系统中使用apt 包管理器安装 Git LFS
微信小程序转化为uni-app项目的方法示例
![](https://img.aspxhome.com/file/2023/7/56297_0s.png)
SQL语句之WHERE子句的使用方法
Anaconda最新版2023安装教程Spyder安装教程图文详解
![](https://img.aspxhome.com/file/2023/7/96467_0s.png)
Perl命令行应用程序详解
SQL中的开窗函数详解可代替聚合函数使用
![](https://img.aspxhome.com/file/2023/7/86877_0s.png)
解决python中使用plot画图,图不显示的问题
![](https://img.aspxhome.com/file/2023/6/90346_0s.jpg)