使用Golang如何使用DES和CBC加密长度为5个字符的明文?

huangapple go评论109阅读模式
英文:

Golang: How do I encrypt plain text that is 5 characters long with DES and CBC?

问题

目前正在尝试将长度为5个字符的明文加密为12个字符的加密字符串。我想要能够指定一个唯一的初始向量(不是随机生成的),一个唯一的密钥,并使用DES算法。我当前的代码要求明文的长度为8个字符(5个字符的名称加上3个空格)。

英文:

Currently trying to encrypt plaintext that is 5 characters long into a 12 character encrypted string. I want to be able to specify a unique IV (not randomly generated), a unique key, and use DES. My current code requires the plaintext to be 8 characters long (5 character name plus 3 spaces).

答案1

得分: 3

我已经遇到过这个问题。这是由于填充问题导致的。你想要的代码是:

package main

import (
	"crypto/cipher"
	"crypto/des"
	"encoding/base64"
	"fmt"
	"bytes"
)

func main() {
	originalText := "yolan"
	fmt.Println(originalText)

	key := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC}

	// encrypt value to base64
	cryptoText := encrypt(key, originalText)
	fmt.Println(cryptoText)

}

// encrypt string to base64 crypto using des
func encrypt(key []byte, text string) string {
	plaintext := []byte(text)

	block, err := des.NewCipher(key)
	if err != nil {
		panic(err)
	}

	iv := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC}
	
	blockSize := block.BlockSize()
	origData := PKCS5Padding(plaintext, blockSize)
	blockMode := cipher.NewCBCEncrypter(block, iv)
	cryted := make([]byte, len(origData))
	blockMode.CryptBlocks(cryted, origData)

	return base64.URLEncoding.EncodeToString(cryted)
}

func PKCS5Padding(src []byte, blockSize int) []byte {
	padding := blockSize - len(src)%blockSize
	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
	return append(src, padtext...)
}

func PKCS5UnPadding(src []byte) []byte {
	length := len(src)
	unpadding := int(src[length-1])
	return src[:(length - unpadding)]
}

你可以在Go Playground上测试它:代码链接

英文:

I have already faced this problem. This is because of padding issue. The code you wanted is a

Code link You Can test it at go playground.

  package main
import (
"crypto/cipher"
"crypto/des"
"encoding/base64"
"fmt"
"bytes"
)
func main() {
originalText := "yolan"
fmt.Println(originalText)
key := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC}
// encrypt value to base64
cryptoText := encrypt(key, originalText)
fmt.Println(cryptoText)
}
// encrypt string to base64 crypto using des
func encrypt(key []byte, text string) string {
plaintext := []byte(text)
block, err := des.NewCipher(key)
if err != nil {
panic(err)
}
iv := []byte{0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC, 0xBC}
blockSize := block.BlockSize()
origData := PKCS5Padding(plaintext, blockSize)
blockMode := cipher.NewCBCEncrypter(block, iv)
cryted := make([]byte, len(origData))
blockMode.CryptBlocks(cryted, origData)
return base64.URLEncoding.EncodeToString(cryted)
}
func PKCS5Padding(src []byte, blockSize int) []byte {
padding := blockSize - len(src)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
func PKCS5UnPadding(src []byte) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[:(length - unpadding)]
}

huangapple
  • 本文由 发表于 2017年1月6日 01:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/41490851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定