byte[] IDataProtector.Protect(byte[] plaintext) { var response = _kms.Encrypt(_keyPathName, ByteString.CopyFrom(plaintext)); return(response.Ciphertext.ToByteArray()); }
/// <inheritdoc /> public EncryptedXmlInfo Encrypt(XElement plaintextElement) { // Steps: // 1) Generate a local symmetric key // 2) Encrypt the XML with that key // 3) Encrypt the local key data with KMS // 4) Return an element containing: // - The KMS crypto key used for encryption // - The encrypted key data // - The encrypted payload var keyPair = CreateLocalKey(); byte[] locallyEncryptedData; using (keyPair.algorithm) { locallyEncryptedData = EncryptElement(keyPair.algorithm, plaintextElement); } ByteString encryptedKeyData = _kmsClient.Encrypt(_keyName, keyPair.proto.ToByteString()).Ciphertext; var encryptedElement = new XElement(EncryptedElement, new XComment("This key is encrypted with Google KMS."), new XAttribute(KmsKeyNameAttribute, _keyName), new XAttribute(LocalKeyDataAttribute, encryptedKeyData.ToBase64()), new XElement(PayloadElement, Convert.ToBase64String(locallyEncryptedData))); return(new EncryptedXmlInfo(encryptedElement, typeof(KmsXmlDecryptor))); }
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("projects/programmingforthecloudbf/locations/global/keyRings/BFKeyring/cryptoKeys/BFkey")); string cipher = client.Encrypt(kn, ByteString.CopyFromUtf8(plaintext)).Ciphertext.ToBase64(); return(cipher); }
public static string Encrypt(string plaintext) { KeyManagementServiceClient client = KeyManagementServiceClient.Create(); CryptoKeyName kn = CryptoKeyName.FromUnparsed( new Google.Api.Gax.UnparsedResourceName("projects/jurgen-cloud-project/locations/global/keyRings/pftckeyring/cryptoKeys/pftckeys")); string cipher = client.Encrypt(kn, ByteString.CopyFromUtf8(plaintext)).Ciphertext.ToBase64(); return(cipher); }
// [END kms_add_member_to_keyring_policy] // [START kms_encrypt] 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 string Encrypt(string plaintext) { // Create the client. KeyManagementServiceClient client = KeyManagementServiceClient.Create(); // Build the key name. CryptoKeyName keyName = new CryptoKeyName(projectId, locationId, keyRingId, keyId); //Encrypt data string cipher = client.Encrypt(keyName, ByteString.CopyFromUtf8(plaintext)).Ciphertext.ToBase64(); return(cipher); }
public void DecryptsData() { var plaintext = "testing1234"; // Create some ciphertext. KeyManagementServiceClient client = KeyManagementServiceClient.Create(); CryptoKeyName keyName = new CryptoKeyName(_fixture.ProjectId, _fixture.LocationId, _fixture.KeyRingId, _fixture.SymmetricKeyId); var result = client.Encrypt(keyName, ByteString.CopyFromUtf8(plaintext)); // Run the sample code. var response = _sample.DecryptSymmetric( projectId: _fixture.ProjectId, locationId: _fixture.LocationId, keyRingId: _fixture.KeyRingId, keyId: _fixture.SymmetricKeyId, ciphertext: result.Ciphertext.ToByteArray()); Assert.Equal(plaintext, response); }
public byte[] EncryptSymmetric( string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key", string message = "Sample message") { // Create the client. KeyManagementServiceClient client = KeyManagementServiceClient.Create(); // Build the key name. CryptoKeyName keyName = new CryptoKeyName(projectId, locationId, keyRingId, keyId); // Convert the message into bytes. Cryptographic plaintexts and // ciphertexts are always byte arrays. byte[] plaintext = Encoding.UTF8.GetBytes(message); // Call the API. EncryptResponse result = client.Encrypt(keyName, ByteString.CopyFrom(plaintext)); // Return the ciphertext. return(result.Ciphertext.ToByteArray()); }