Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong description #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Fix wrong description #14

wants to merge 1 commit into from

Conversation

polRk
Copy link

@polRk polRk commented May 25, 2022

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

type YCKMS struct {
	sdk   *ycsdk.SDK
	keyID string
}

func New(sdk *ycsdk.SDK, keyID string) KMS {
	return &YCKMS{sdk: sdk, keyID: keyID}
}

func (s *YCKMS) Encrypt(aadContext string, plaintext string) (keyID string, versionID string, ciphertext []byte, err error) {
	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,
	})
	if err != nil {
		return "", "", nil, err
	}

	return result.KeyId, result.VersionId, result.Ciphertext, nil
}

func (s *YCKMS) Decrypt(aadContext string, 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, 
	})
	if err != nil {
		return nil, err
	}

	return result.Plaintext, nil
}

Usage

	keyID, versionID, ciphertext, err := k.Encrypt(Bot.ID, Bot.Token)
	if err != nil {
		panic(fmt.Errorf("cannot encrypt Bot.Token: %w", err))
	}

	plaintext, err := k.Decrypt(Bot.ID, Bot.TokenCiphertext)
	if err != 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 free to 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