This is a creation in Article, where the information may have evolved or changed.
Recent projects need to be among the ranks of the go language, I do not know is a blessing or woe ah, hi, feel it!
Today, a des plus decryption algorithm, mainly used in the Golang standard library, and online Daniel's documents, here first thank you, and attach the original link address:
http://blog.studygolang.com/2013/01/go%E5%8A%A0%E5%AF%86%E8%A7%A3%E5%AF%86%E4%B9%8Bdes/
Good, no nonsense, directly on the code:
Func desencrypt (Origdata, Key []byte) ([]byte, error) {
block, err: = des. Newcipher (Key)
If err! = Nil {
return nil, err
}
Origdata = Pkcs5padding (Origdata, block. BlockSize ())
Origdata = Zeropadding (Origdata, block. BlockSize ())
Blockmode: = cipher. Newcbcencrypter (block, key)
Crypted: = Make ([]byte, Len (origdata))
Depending on the description of the Cryptblocks method, initializing crypted as follows can also
crypted: = Origdata
Blockmode.cryptblocks (crypted, Origdata)
Return crypted, Nil
}
Des decryption
Func desdecrypt (crypted, Key []byte) ([]byte, error) {
block, err: = des. Newcipher (Key)
If err! = Nil {
return nil, err
}
Blockmode: = cipher. Newcbcdecrypter (block, key)
Origdata: = Make ([]byte, Len (crypted))
Origdata: = crypted
Blockmode.cryptblocks (Origdata, crypted)
Origdata = pkcs5unpadding (origdata)
Origdata = zerounpadding (origdata)
Return Origdata, Nil
}
Func pkcs5padding (ciphertext []byte, blockSize int) []byte {
padding: = Blocksize-len (ciphertext)%blocksize
Padtext: = bytes. Repeat ([]byte{byte (padding)}, padding)
Return append (ciphertext, padtext ...)
}
Func zerounpadding (Origdata []byte) []byte {
Length: = Len (origdata)
unpadding: = Int (origdata[length-1])
Return origdata[:(length-unpadding)]
}