Go语言实现AzDG可逆加密算法实例
本文实例讲述了go语言实现azdg可逆加密算法。分享给大家供大家参考。具体实现方法如下:
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语言程序设计有所帮助。
上一篇: 最新织梦全国多城市分站地区插件dedecms分站网站源码插件
下一篇: 人类简史读书笔记