This article describes the Go language implementation AZDG reversible encryption algorithm. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Package Main
Import (
"FMT"
"Crypto/md5"
"Encoding/base64"
"Time"
)
var cipher = "Dense keys"
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 ("Zhang Friend")
Fmt. PRINTLN (s)
Fmt. Println (decode (s))
}
I hope this article will help you with your go language program.