/// <summary> /// Decrypts the ciphertext. /// </summary> internal static byte[] DecryptWithKey(byte[] cipherText, SqlCipherMetadata md, string serverName) { Debug.Assert(serverName != null, @"serverName should not be null in DecryptWithKey."); // Initialize cipherAlgo if not already done. if (!md.IsAlgorithmInitialized()) { SqlSecurityUtility.DecryptSymmetricKey(md, serverName); } 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.Value.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, 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) { SQL.NullCipherText(); } return cipherText; }
/// <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> /// Decrypts the ciphertext. /// </summary> internal static byte[] DecryptWithKey(byte[] cipherText, SqlCipherMetadata md, string serverName) { Debug.Assert(serverName != null, @"serverName should not be null in DecryptWithKey."); // Initialize cipherAlgo if not already done. if (!md.IsAlgorithmInitialized()) { SqlSecurityUtility.DecryptSymmetricKey(md, serverName); } 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.Value.encryptedKey, fLast:true, countOfBytes:10); string valStr = GetBytesAsString(cipherText, fLast:false, countOfBytes:10); throw SQL.ThrowDecryptionFailed(keyStr, valStr, e); } }