0

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


Zombo
  • 1
  • 62
  • 391
  • 407
Black_Dreams
  • 572
  • 1
  • 5
  • 11
  • 3
    I didn't test anything but maybe the key is the **hex string** representation of the key, so you could check with a "hex string to byte array converter" and use the result as input for your decryption function :-) – Michael Fehr Apr 01 '21 at 11:10
  • 2
    The key must be [hex](https://golang.org/pkg/encoding/hex/) decoded: `key, err := hex.DecodeString("2A07EEF4384E7CD9671E1ED5BCF60029")`. The decryption is _testing go_. – Topaco Apr 01 '21 at 11:18
  • Yes It worked thanks MichaelFehr and Topaco. Thanks a lot. I will update post and post the solution with that. – Black_Dreams Apr 01 '21 at 11:23

0 Answers0