/// <summary> /// Decrypts the ciphertext. /// </summary> internal static byte[] DecryptWithKey(byte[] cipherText, SqlCipherMetadata md, SqlConnection connection, SqlCommand command) { // Initialize cipherAlgo if not already done. if (!md.IsAlgorithmInitialized()) { DecryptSymmetricKey(md, connection, command); } Debug.Assert(md.IsAlgorithmInitialized(), "Decryption Algorithm is not initialized"); try { byte[] plainText = md.CipherAlgorithm.DecryptData(cipherText); // this call succeeds or throws. if (null == plainText) { throw SQL.NullPlainText(); } return(plainText); } catch (Exception e) { // compute the strings to pass string keyStr = GetBytesAsString(md.EncryptionKeyInfo.encryptedKey, fLast: true, countOfBytes: 10); string valStr = GetBytesAsString(cipherText, fLast: false, countOfBytes: 10); throw SQL.ThrowDecryptionFailed(keyStr, valStr, e); } }
/// <summary> /// Encrypts the plaintext. /// </summary> internal static byte[] EncryptWithKey(byte[] plainText, SqlCipherMetadata md, SqlConnection connection, SqlCommand command) { // Initialize cipherAlgo if not already done. if (!md.IsAlgorithmInitialized()) { DecryptSymmetricKey(md, connection, command); } Debug.Assert(md.IsAlgorithmInitialized(), "Encryption Algorithm is not initialized"); byte[] cipherText = md.CipherAlgorithm.EncryptData(plainText); // this call succeeds or throws. if (null == cipherText || 0 == cipherText.Length) { throw SQL.NullCipherText(); } return(cipherText); }
internal SqlCipherMetadata cipherMD; // Cipher related metadata for encrypted columns. /// <summary> /// Is the algorithm handle for the cipher encryption initialized ? /// </summary> /// <returns></returns> internal bool IsAlgorithmInitialized() { if (null != cipherMD) { return(cipherMD.IsAlgorithmInitialized()); } return(false); }
/// <summary> /// Encrypts the plaintext. /// </summary> internal static byte[] EncryptWithKey(byte[] plainText, SqlCipherMetadata md, string serverName) { Debug.Assert(serverName != null, @"serverName should not be null in EncryptWithKey."); // Initialize cipherAlgo if not already done. if (!md.IsAlgorithmInitialized()) { SqlSecurityUtility.DecryptSymmetricKey(md, serverName); } Debug.Assert(md.IsAlgorithmInitialized(), "Encryption Algorithm is not initialized"); byte[] cipherText = md.CipherAlgorithm.EncryptData(plainText); // this call succeeds or throws. if (null == cipherText || 0 == cipherText.Length) { throw SQL.NullCipherText(); } return(cipherText); }