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

    }

  • 相关阅读:
    28.注解2.md
    29.Junit测试框架.md
    WCF学习笔记(2)-WCF的通讯过程
    WCF学习笔记(1)-一个完整的例子
    Sql2008事务日志已满处理
    面向对象六大原则
    计算机基础(1)-原码、反码、补码
    Spring.Net学习笔记(7)-事务
    Spring.Net学习笔记(6)-方法注入
    Spring.Net学习笔记(5)-集合注入
  • 原文地址:https://www.cnblogs.com/zolo/p/5849153.html
Copyright © 2011-2022 走看看