【问题标题】:C# equivalent of DecryptByPassPhraseC# 等效于 DecryptByPassPhrase
【发布时间】:2015-10-26 15:50:07
【问题描述】:

我需要解密使用 EncryptByPassPhrase 在数据库上加密但不访问数据库的值。

如何从密码中获取加密密钥?

看过

Replicate T-SQL DecryptByPassPhrase in C#

C# Decrypt bytes from SQL Server EncryptByPassPhrase?

我的代码是:

public static string AESDatabaseDecrypt(string encryptedString)
{
    passphrase =     "S0meFakePassPhrase01234!";
    encryptedString = "AQAAAOmuc52dnbVwTqEx1kp+4WhI89LYKHh3jg=="; // temporarily hard coded


    // setup encryption settings to match decryptbypassphrase
    TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
    provider.Key = UTF8Encoding.UTF8.GetBytes(passphrase).Take(16).ToArray(); // stuck on getting key from passphrase
    provider.KeySize = 128;
    provider.Padding = PaddingMode.Zeros;
    // setup data to be decrypted
    byte[] encryptedStringAsByteArray = Convert.FromBase64String(encryptedString);

    // hack some extra bytes up to a multiple of 8
    encryptedStringAsByteArray = encryptedStringAsByteArray.Concat(new byte[] { byte.MinValue, byte.MinValue, byte.MinValue, byte.MinValue }).ToArray(); // add 4 empty bytes to make 32 bytes
    MemoryStream encryptedStringAsMemoryStream = new MemoryStream(encryptedStringAsByteArray);
    // decrypt
    CryptoStream cryptoStream = new CryptoStream(encryptedStringAsMemoryStream, provider.CreateDecryptor(), CryptoStreamMode.Read);
    // return the result
    StreamReader cryptoStreamReader = new StreamReader(cryptoStream);
    string decryptedString = cryptoStreamReader.ReadToEnd();
}

【问题讨论】:

  • 我从未找到解决方案 - 我们最终向数据库询问了值

标签: c# sql encryption


【解决方案1】:

嗯,我的意思是,我首先想到的是你实际上并没有从你的方法中返回一个值。尝试将return decryptedString; 添加到末尾,看看会得到什么。

【讨论】:

    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多