You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a bug when using the decrypt function on the incoming challenge in the on_subscribe call from the gateway. The decryption process does not result in any errors but there is some extra padding at the end of the decrypted string returned from the function. If you return this string to the gateway in the response body under "answer" key, the gateway throws "Encryption verification is failed" error.
To fix the problem, I propose the following changes to the aesDecrypt and the aesEncrypt functions in the utility file:
blockSize := cipher.BlockSize()
padding := blockSize - len(payload)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
payload = append(payload, padText...)
encrypted := make([]byte, len(payload))
for i := 0; i < len(payload); i += blockSize {
cipher.Encrypt(encrypted[i:i+blockSize], payload[i:i+blockSize])
}
return encrypted, nil
}
This results in the encryption function properly using PKCS#7 scheme to properly add the padding and the decrypt function to use the same to remove the additional padding. This resolves the beforementioned bug.
// Pad the payload using PKCS#7 padding
paddedData := pkcs7.Pad(payload, cipher.BlockSize())
// Encrypt the padded data
encrypted := make([]byte, len(paddedData))
cipher.NewEncrypter(key).CryptBlocks(encrypted, paddedData)
return encrypted, nil
I am facing same problem.. I tried ur code but it throws
2024/06/02 10:59:34 http: panic serving 10.0.8.139:36126: runtime error: slice bounds out of range [:-164]
Hello Bhanu,
I would request that you join the daily tech integration meetings with ONDC team which happen at about 10 AM each day. They will asssit you with the problem.
During the onboarding process, I used the golang utilities for encrypting and decrypting which can be found on this URL: https://github.com/ONDC-Official/reference-implementations/blob/main/utilities/signing_and_verification/golang/crypto.go#L44
There is a bug when using the decrypt function on the incoming challenge in the on_subscribe call from the gateway. The decryption process does not result in any errors but there is some extra padding at the end of the decrypted string returned from the function. If you return this string to the gateway in the response body under "answer" key, the gateway throws "Encryption verification is failed" error.
To fix the problem, I propose the following changes to the aesDecrypt and the aesEncrypt functions in the utility file:
func aesDecrypt(cipherText []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}
}
func aesEncrypt(payload []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}
}
This results in the encryption function properly using PKCS#7 scheme to properly add the padding and the decrypt function to use the same to remove the additional padding. This resolves the beforementioned bug.
You can also use a public package to do the same:
import "github.com/zenazn/pkcs7pad"
func aesEncrypt(payload []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}
}
func aesDecrypt(cipherText []byte, key []byte) ([]byte, error) {
cipher, err := aes.NewCipher(key)
if err != nil {
fmt.Println("Error creating AES cipher", err)
return nil, err
}
}
The text was updated successfully, but these errors were encountered: