下面是 PHP 加密的代码
```php
/**
* des-ecb加密
* @param string $data 要被加密的数据
* @param string $key 加密密钥(64位的字符串)
*/
function des_ecb_encrypt($data, $key){
return openssl_encrypt ($data, 'des-ecb', $key);
}
```
请问在 go 中如何解密?
我在网上找了一些 DEC ECB 的解密方法,比如
```go
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
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
```
始终会提示 crypto/cipher: input not full blocks
```
<?php
$key = '12345678';
$data = 'adsdfasfd';
$str = openssl_encrypt ($data, 'des-ecb', $key);
var_dump($str);
```
string(24) "WHIttCXTRINe86ptkV8eQw=="
#2
更多评论
```
<?php
$key = '12345678';
$data = 'adsdfasfd';
$str = openssl_encrypt ($data, 'des-ecb', $key);
var_dump($str);
// string(24) "WHIttCXTRINe86ptkV8eQw=="
```
```
package main
import (
"encoding/base64"
"fmt"
"log"
"github.com/forgoer/openssl"
)
func main() {
key := "12345678"
data := "WHIttCXTRINe86ptkV8eQw=="
decodeString, err := base64.StdEncoding.DecodeString(data)
if err != nil {
log.Fatalln("base64 decode失败,", err)
}
decrypt, err := openssl.DesECBDecrypt([]byte(decodeString), []byte(key), openssl.PKCS7_PADDING)
if err != nil {
log.Fatalln("解密失败,", err)
}
fmt.Println(string(decrypt))
}
// adsdfasfd
```
好好学习,积累工作经验
#1