zoukankan      html  css  js  c++  java
  • 使用golang+java实现基于ecb的3eds加解密

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

    }

  • 相关阅读:
    MySQL-基本sql命令
    Java for LeetCode 203 Remove Linked List Elements
    Java for LeetCode 202 Happy Number
    Java for LeetCode 201 Bitwise AND of Numbers Range
    Java for LeetCode 200 Number of Islands
    Java for LeetCode 199 Binary Tree Right Side View
    Java for LeetCode 198 House Robber
    Java for LeetCode 191 Number of 1 Bits
    Java for LeetCode 190 Reverse Bits
    Java for LeetCode 189 Rotate Array
  • 原文地址:https://www.cnblogs.com/zolo/p/5849153.html
Copyright © 2011-2022 走看看