使用golang+java實現基於ecb的3eds加解密

來源:互聯網
上載者: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
繼續下午問的 ECB 問題.
在 go 的 issues 裡找到
https://code.google.com/p/go/issues/detail?id=5597
有人為標準庫寫好了補丁. 但是專案管理者拒絕了. 另外這個補丁的地址還在
https://codereview.appspot.com/7860047/
還有完整的例子.
我直接拷貝下來, 跑了一下, 和 java 的一致

Golang中的DES加密ECB模式

Golang其實已經實現了ECB模式,但庫卻不提供,看有人提交了ECB的封裝,因為DES的ECB模式是故意不放出來的,也是不安全的,所以就沒有合并到Go主幹中,可實際中我們又不用那麼“安全”,就寫了個簡單的封裝。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374

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))

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.