本文实例讲述了Go语言实现AzDG可逆加密算法。分享给大家供大家参考。具体实现方法如下:
package mainimport (
"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语言程序设计有所帮助。
到此这篇关于Go语言如何实现AzDG可逆加密算法实例就介绍到这了。卓越的人一大优点是:在不利与艰苦的遭遇里百折不挠。更多相关Go语言如何实现AzDG可逆加密算法实例内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!