On the Go Language des plus decryption algorithm

Source: Internet
Author: User
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)]
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.