Skip to content

Fix wrong description - #14

Open
polRk wants to merge 1 commit into
yandex-cloud:masterfrom
polRk:patch-1
Open

Fix wrong description#14
polRk wants to merge 1 commit into
yandex-cloud:masterfrom
polRk:patch-1

Conversation

@polRk

@polRkpolRk commented May 25, 2022

Copy link
Copy Markdown

Remove base64 requirements for ciphertext in SymmetricDecryptRequest.

When I submit ciphertext encoded in base64, I get an error message

rpc error: code = InvalidArgument desc = Bad ciphertext

In raw format, after - OK

Full code

typeYCKMSstruct {
sdk*ycsdk.SDKkeyIDstring
}
funcNew(sdk*ycsdk.SDK, keyIDstring) KMS {
return&YCKMS{sdk: sdk, keyID: keyID}
}
func (s*YCKMS) Encrypt(aadContextstring, plaintextstring) (keyIDstring, versionIDstring, ciphertext []byte, errerror) {
aadContextBuf:=make([]byte, base64.RawStdEncoding.EncodedLen(len([]byte(aadContext))))
plaintextBuf:=make([]byte, base64.RawStdEncoding.EncodedLen(len(plaintext)))
base64.RawStdEncoding.Encode(aadContextBuf, []byte(aadContext))
base64.RawStdEncoding.Encode(plaintextBuf, []byte(plaintext))
result, err:=s.sdk.KMSCrypto().SymmetricCrypto().Encrypt(context.Background(), &kms.SymmetricEncryptRequest{
KeyId: s.keyID,
AadContext: aadContextBuf,
Plaintext: plaintextBuf,
})
iferr!=nil {
return"", "", nil, err
}
returnresult.KeyId, result.VersionId, result.Ciphertext, nil
}
func (s*YCKMS) Decrypt(aadContextstring, ciphertext []byte) ([]byte, error) {
aadContextBuf:=make([]byte, base64.RawStdEncoding.EncodedLen(len([]byte(aadContext))))
// ciphertextBuf := make([]byte, base64.RawStdEncoding.EncodedLen(len(ciphertext)))base64.RawStdEncoding.Encode(aadContextBuf, []byte(aadContext))
// base64.RawStdEncoding.Encode(ciphertextBuf, ciphertext)result, err:=s.sdk.KMSCrypto().SymmetricCrypto().Decrypt(context.Background(), &kms.SymmetricDecryptRequest{
KeyId: s.keyID,
AadContext: aadContextBuf,
Ciphertext: ciphertext, })
iferr!=nil {
returnnil, err
}
returnresult.Plaintext, nil
}

Usage

keyID, versionID, ciphertext, err:=k.Encrypt(Bot.ID, Bot.Token)
iferr!=nil {
panic(fmt.Errorf("cannot encrypt Bot.Token: %w", err))
}
plaintext, err:=k.Decrypt(Bot.ID, Bot.TokenCiphertext)
iferr!=nil {
panic(fmt.Errorf("cannot dencrypt Bot.Token: %w", err))
}
plaintext, _=base64.RawStdEncoding.DecodeString(string(plaintext))
fmt.Println(string(plaintext))

Remove base64 requirements for ciphertext in SymmetricDecryptRequest
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@polRk