在C#中使用Google KMS API的加密值长度与在邮递员中直接从API生成的加密文本的长度不同

Joe*_*Joe 1 encryption google-cloud-platform google-cloud-kms

我正在尝试使用Google KeyManagementService在C#中加密文本。我无法解密KeyManagementServiceClient.Encrypt使用KeyManagementServiceClient.Decrypt方法生成的密码。解密给了我:

Grpc.Core.RpcException: Status(StatusCode=InvalidArgument, Detail="Decryption failed: the ciphertext is invalid.")
Run Code Online (Sandbox Code Playgroud)

我尝试使用google try this API链接获取密码,并且由google API生成的密码长度与C#结果不同。

例如:纯文字: text

来自C#的密码:

TaRQSJ2KXrdmJJT6MmlD8RrcxzPJEa1jwAXWJ1puXg6nbl80aBcSLQBqSYOONfXhpZx8SyxCdB6mqTgr8uLJoAjva+Q4kN/p0+9RL2Sp2mHq4wjmZQ==
Run Code Online (Sandbox Code Playgroud)

来自API的密码:

TaRQSJ2KXv9ntnS7IszL077KNPtGJnqF9pSNiWANsq8gD0whezUSKwBqSYOOKKMifiWrfPDnHL5xETHPPlms0ztjkqa5hjdtkHwpzByLzi68A40
Run Code Online (Sandbox Code Playgroud)

有没有人分阶段进行发行?

这是我的示例代码

Grpc.Core.RpcException: Status(StatusCode=InvalidArgument, Detail="Decryption failed: the ciphertext is invalid.")
Run Code Online (Sandbox Code Playgroud)

小智 5

您已经对加密结果进行了base64编码,但是在尝试对其进行解密之前,尚未对它进行base64解码。

在行中

ByteString ciphertext = ByteString.CopyFrom(Encoding.ASCII.GetBytes(cipher));
Run Code Online (Sandbox Code Playgroud)

它应该看起来像

ByteString ciphertext = ByteString.FromBase64(cipher);
Run Code Online (Sandbox Code Playgroud)

(还请注意,如果您要这样做,则ByteString类具有用于往返Base64的内置方法。)