Golang无填充ECB加密和解密的AESMode
我想在Golang中使用ECB加密和解密执行AESMode 键:Golang无填充ECB加密和解密的AESMode,go,encryption,aes,ecb,Go,Encryption,Aes,Ecb,我想在Golang中使用ECB加密和解密执行AESMode 键:2A07EEF4384E7CD9671E1ED5BCF60029 加密的示例消息base64 Encoded:g08cALdBhD8Q21d4pfjBKg== 我无法解密这封信。有人能帮我在Golang做AESMODE.ECB enc/dec吗 目前我正在使用 解放党 加密代码 func EncryptASEWithECB(pt, key []byte) string { block, err := aes.NewCiph
2A07EEF4384E7CD9671E1ED5BCF60029
加密的示例消息base64 Encoded:g08cALdBhD8Q21d4pfjBKg==
我无法解密这封信。有人能帮我在Golang做AESMODE.ECB enc/dec吗
目前我正在使用
解放党
加密代码
func EncryptASEWithECB(pt, key []byte) string {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
mode := ecb.NewECBEncrypter(block)
ct := make([]byte, len(pt))
mode.CryptBlocks(ct, pt)
return base64.StdEncoding.EncodeToString(ct)
}
func DecryptASEWithECB(ct, key []byte) string {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
mode := ecb.NewECBDecrypter(block)
pt := make([]byte, len(ct))
mode.CryptBlocks(pt, ct)
return string(pt)
}
func TestEncryptWithECB(b *testing.T) {
key := []byte("2A07EEF4384E7CD9671E1ED5BCF60029")
plaintext := []byte("exampleplaintext")
encrypted := utils.EncryptASEWithECB(plaintext,key)
fmt.Printf("encrypted : %s\n", encrypted)
//plaintext = []byte(encrypted)
sDec,err:= base64.StdEncoding.DecodeString(encrypted)
fmt.Println(string(sDec),err)
decrypted := utils.DecryptASEWithECB(sDec, key)
fmt.Printf("decrypted : %s\n", decrypted)
}
解密代码
func EncryptASEWithECB(pt, key []byte) string {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
mode := ecb.NewECBEncrypter(block)
ct := make([]byte, len(pt))
mode.CryptBlocks(ct, pt)
return base64.StdEncoding.EncodeToString(ct)
}
func DecryptASEWithECB(ct, key []byte) string {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
mode := ecb.NewECBDecrypter(block)
pt := make([]byte, len(ct))
mode.CryptBlocks(pt, ct)
return string(pt)
}
func TestEncryptWithECB(b *testing.T) {
key := []byte("2A07EEF4384E7CD9671E1ED5BCF60029")
plaintext := []byte("exampleplaintext")
encrypted := utils.EncryptASEWithECB(plaintext,key)
fmt.Printf("encrypted : %s\n", encrypted)
//plaintext = []byte(encrypted)
sDec,err:= base64.StdEncoding.DecodeString(encrypted)
fmt.Println(string(sDec),err)
decrypted := utils.DecryptASEWithECB(sDec, key)
fmt.Printf("decrypted : %s\n", decrypted)
}
当我尝试这个时,我得到的结果是正确的
测试用例
func EncryptASEWithECB(pt, key []byte) string {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
mode := ecb.NewECBEncrypter(block)
ct := make([]byte, len(pt))
mode.CryptBlocks(ct, pt)
return base64.StdEncoding.EncodeToString(ct)
}
func DecryptASEWithECB(ct, key []byte) string {
block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
}
mode := ecb.NewECBDecrypter(block)
pt := make([]byte, len(ct))
mode.CryptBlocks(pt, ct)
return string(pt)
}
func TestEncryptWithECB(b *testing.T) {
key := []byte("2A07EEF4384E7CD9671E1ED5BCF60029")
plaintext := []byte("exampleplaintext")
encrypted := utils.EncryptASEWithECB(plaintext,key)
fmt.Printf("encrypted : %s\n", encrypted)
//plaintext = []byte(encrypted)
sDec,err:= base64.StdEncoding.DecodeString(encrypted)
fmt.Println(string(sDec),err)
decrypted := utils.DecryptASEWithECB(sDec, key)
fmt.Printf("decrypted : %s\n", decrypted)
}
问题
问题是我必须使用AESMODE.ECB从下面的加密消息中获取值
此密钥和消息来自第三方,他们说我们必须使用AESMODE.ECB(无填充)来解密此消息
Key: `2A07EEF4384E7CD9671E1ED5BCF60029`
Encrypted Sample Msg base64 Encoded: `g08cALdBhD8Q21d4pfjBKg==`
但我不能这么做。
有人帮忙吗?我怎么能这样做
解决方案
测试功能已更新
key, err := hex.DecodeString("2A07EEF4384E7CD9671E1ED5BCF60029")
func TestEncWithECB(b *testing.T) {
//key :=
//plaintext = []byte(encrypted)
key,_ := hex.DecodeString("2A07EEF4384E7CD9671E1ED5BCF60029") //encode key in bytes to string and keep as secret, put in a vault
fmt.Printf("key to encrypt/decrypt : %s\n", key)
sDec,err:= base64.StdEncoding.DecodeString("g08cALdBhD8Q21d4pfjBKg==")
fmt.Println(string(sDec),err)
decrypted := utils.DecryptASEWithECB(sDec, key)
fmt.Printf("decrypted : %s\n", decrypted)
}
我没有测试任何东西,但可能密钥是密钥的十六进制字符串表示形式,因此您可以使用“十六进制字符串到字节数组转换器”进行检查,并将结果用作解密函数的输入:-)密钥必须解码:
key,err:=hex.DecodeString(“2A07EEF4384E7CD9671E1ED5BCF60029”)
。解密正在测试中。是的,它成功了,谢谢MichaelFehr和Topaco。谢谢。我会更新帖子,并用它发布解决方案。