简体   繁体   English

Google KMS 在解密数据时出错

[英]Google KMS giving error when decrypting data

When i try to decrypt my data using the Google KMS i am getting this error.当我尝试使用 Google KMS 解密我的数据时,我收到了这个错误。 Below is my code for the decryption.下面是我的解密代码。 The error is hitting on the line where there is string plaintext .错误出现在有string plaintext的行上。 Thanks in advance提前致谢

Code代码

    public static string Encrypt(string plaintext)
    {
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();
        //projects/progforthecloudt2020/locations/global/keyRings/pfckeyring001/cryptoKeys/pfckeys
        CryptoKeyName kn = CryptoKeyName.FromUnparsed(new 
        Google.Api.Gax.UnparsedResourceName("GOOGLE RESOURCE ID REMOVED"));
        string cipher = client.Encrypt(kn, ByteString.CopyFromUtf8(plaintext)).Ciphertext.ToBase64();

        return cipher;
    }

    public static string Decrypt(string cipher)
    {
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();
        CryptoKeyName kn = CryptoKeyName.FromUnparsed(new Google.Api.Gax.UnparsedResourceName("GOOGLE RESOURCE ID REMOVED"));
        string plaintext = client.Decrypt(kn, ByteString.CopyFromUtf8(cipher)).Plaintext.ToBase64();

        return plaintext;
    }

Error错误

Grpc.Core.RpcException: 'Status(StatusCode=InvalidArgument, Detail="Decryption failed: the ciphertext is invalid.")'

You're base64 encoding the result of your encryption call, but then you aren't base64 decoding it in your decrypt call.你是 base64 编码你的加密调用的结果,但是你不是 base64 在你的解密调用中解码它。 You shouldn't need to base64 encode the data.您不需要 base64 编码数据。

public static void Encrypt(string projectId, string locationId, string keyRingId, string cryptoKeyId, string plaintextFile, string ciphertextFile)
{
    KeyManagementServiceClient client = KeyManagementServiceClient.Create();
    CryptoKeyName cryptoKeyName =
        new CryptoKeyName(projectId, locationId, keyRingId, cryptoKeyId);

    byte[] plaintext = File.ReadAllBytes(plaintextFile);
    EncryptResponse result = client.Encrypt(cryptoKeyName, ByteString.CopyFrom(plaintext));

    // Output encrypted data to a file.
    File.WriteAllBytes(ciphertextFile, result.Ciphertext.ToByteArray());
    Console.Write($"Encrypted file created: {ciphertextFile}");
}


public static void Decrypt(string projectId, string locationId, string keyRingId, string cryptoKeyId, string ciphertextFile, string plaintextFile)
{
    KeyManagementServiceClient client = KeyManagementServiceClient.Create();
    CryptoKeyName cryptoKeyName =
        new CryptoKeyName(projectId, locationId, keyRingId, cryptoKeyId);

    byte[] ciphertext = File.ReadAllBytes(ciphertextFile);
    DecryptResponse result = client.Decrypt(cryptoKeyName, ByteString.CopyFrom(ciphertext));

    // Output decrypted data to a file.
    File.WriteAllBytes(plaintextFile, result.Plaintext.ToByteArray());
    Console.Write($"Decrypted file created: {plaintextFile}");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM