I want to do AESMode with ECB Encryption and Decryption
in Golang.
Key: 2A07EEF4384E7CD9671E1ED5BCF60029
Encrypted Sample Msg base64 Encoded: g08cALdBhD8Q21d4pfjBKg==
I'm not able to decrypt the message. Can anyone help me how can I do AESMODE.ECB enc/dec in Golang.
Currently I'm using github.com/andreburgaud/crypt2go/ecb lib
Encrypt Code
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)
}
Decrypt Code
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)
}
I'm getting result Correct when I tried with this Test Case
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)
}
Problem The problem is I have to use AESMODE.ECB to fetch the value from below encrypted message This key and Message is coming from 3rd party and they Said we have to use AESMODE.ECB without padding to decrypt this message.
Key: `2A07EEF4384E7CD9671E1ED5BCF60029`
Encrypted Sample Msg base64 Encoded: `g08cALdBhD8Q21d4pfjBKg==`
But I'm not able to do that. Anyone help how can I do that ?
Solution Testing Func Updated
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)
}