Using Golang+java to implement ECB-based 3eds encryption and decryption

Source: Internet
Author: User

http://www.100hack.com/2014/04/14/golang%E4%B8%AD%E7%9A%84des%E5%8A%A0%E5%AF%86ecb%E6%A8%A1%E5%BC%8F/

Henry (454213807) 0:26:14
Continue to ask the ECB question in the afternoon.
Found in go issues.
https://code.google.com/p/go/issues/detail?id=5597
Someone has written a patch for the standard library. But the project manager refused. In addition, the address of the patch is still
https://codereview.appspot.com/7860047/
There is also a complete example.
I copied it directly, ran it, and was consistent with Java

Des Crypto-ECB mode in Golang

Golang has actually implemented the ECB model, but the library does not provide, see someone submitted the ECB's package, because Des's ECB mode is deliberately not put out and is not safe, so it is not merged into the go trunk, in fact, we do not have to be so "safe", wrote a simple package.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374

Package Main

Import (

"Bytes"

"Crypto/des"

"Errors"

)

Func pkcs5padding (ciphertext []byte, blockSize int) []byte {

padding: = Blocksize-len (ciphertext)%blocksize

Padtext: = bytes. Repeat ([]byte{byte (padding)}, padding)

Return append (ciphertext, padtext ...)

}

Func pkcs5unpadding (Origdata []byte) []byte {

Length: = Len (origdata)

unpadding: = Int (origdata[length-1])

Return origdata[:(length-unpadding)]

}

Func zeropadding (ciphertext []byte, blockSize int) []byte {

padding: = Blocksize-len (ciphertext)%blocksize

Padtext: = bytes. Repeat ([]byte{0}, padding)

Return append (ciphertext, padtext ...)

}

Func zerounpadding (Origdata []byte) []byte {

return bytes. Trimfunc (Origdata,

Func (R rune) bool {

return r = = Rune (0)

})

}

Func desencrypt (SRC, key []byte) ([]byte, error) {

block, err: = des. Newcipher (Key)

If err! = Nil {

return nil, err

}

BS: = block. BlockSize ()

src = zeropadding (src, BS)

src = pkcs5padding (src, BS)

If Len (src)%bs! = 0 {

return nil, errors. New ("Need a multiple of the blocksize")

}

Out: = Make ([]byte, Len (SRC))

DST: = out

For Len (src) > 0 {

Block. Encrypt (DST, Src[:bs])

src = src[bs:]

DST = Dst[bs:]

}

Return out, Nil

}

Func desdecrypt (SRC, key []byte) ([]byte, error) {

block, err: = des. Newcipher (Key)

If err! = Nil {

return nil, err

}

Out: = Make ([]byte, Len (SRC))

DST: = out

BS: = block. BlockSize ()

If Len (src)%bs! = 0 {

return nil, errors. New ("Crypto/cipher:input not full blocks")

}

For Len (src) > 0 {

Block. Decrypt (DST, Src[:bs])

src = src[bs:]

DST = Dst[bs:]

}

out = zerounpadding (out)

out = pkcs5unpadding (out)

Return out, Nil

}

View Rawdes.go hosted with? by GitHub

123456789101112131415161718192021222324

Package Main

Import (

"FMT"

"Testing"

)

Func testdesencrypt (t *testing. T) {

Key: = []byte ("5e8487e6")

Origtext: = []byte ("Hello world123563332")

Erytext, err: = Desencrypt (Origtext, key)

If err! = Nil {

T.fatal (ERR)

}

Fmt. Printf ("%v\n", Erytext)

Destext, ERR2: = Desdecrypt (Erytext, key)

If err2! = Nil {

T.fatal (ERR2)

}

Fmt. Println (String (destext))

Fmt. Println (Len (Origtext), Len (String (destext)))

Fmt. Println (String (origtext) = = string (destext))

}

Using Golang+java to implement ECB-based 3eds encryption and decryption

Related Article

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.