<i id='RycPr'><tr id='RycPr'><dt id='RycPr'><q id='RycPr'><span id='RycPr'><b id='RycPr'><form id='RycPr'><ins id='RycPr'></ins><ul id='RycPr'></ul><sub id='RycPr'></sub></form><legend id='RycPr'></legend><bdo id='RycPr'><pre id='RycPr'><center id='RycPr'></center></pre></bdo></b><th id='RycPr'></th></span></q></dt></tr></i><div id='RycPr'><tfoot id='RycPr'></tfoot><dl id='RycPr'><fieldset id='RycPr'></fieldset></dl></div>
    • <bdo id='RycPr'></bdo><ul id='RycPr'></ul>
    1. <legend id='RycPr'><style id='RycPr'><dir id='RycPr'><q id='RycPr'></q></dir></style></legend>
      <tfoot id='RycPr'></tfoot>

      <small id='RycPr'></small><noframes id='RycPr'>

        C# 从 SQL Server EncryptByPassPhrase 解密字节?

        C# Decrypt bytes from SQL Server EncryptByPassPhrase?(C# 从 SQL Server EncryptByPassPhrase 解密字节?)

              <tbody id='6ehQV'></tbody>

            • <small id='6ehQV'></small><noframes id='6ehQV'>

                <legend id='6ehQV'><style id='6ehQV'><dir id='6ehQV'><q id='6ehQV'></q></dir></style></legend>
                  <bdo id='6ehQV'></bdo><ul id='6ehQV'></ul>
                  <tfoot id='6ehQV'></tfoot>
                  <i id='6ehQV'><tr id='6ehQV'><dt id='6ehQV'><q id='6ehQV'><span id='6ehQV'><b id='6ehQV'><form id='6ehQV'><ins id='6ehQV'></ins><ul id='6ehQV'></ul><sub id='6ehQV'></sub></form><legend id='6ehQV'></legend><bdo id='6ehQV'><pre id='6ehQV'><center id='6ehQV'></center></pre></bdo></b><th id='6ehQV'></th></span></q></dt></tr></i><div id='6ehQV'><tfoot id='6ehQV'></tfoot><dl id='6ehQV'><fieldset id='6ehQV'></fieldset></dl></div>

                • 本文介绍了C# 从 SQL Server EncryptByPassPhrase 解密字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  遵循在C#中复制T-SQL DecryptByPassPhrase,我是无法使用 MSSQL 进行简单加密以在 C# 中解密.某些列中的加密值是必需的,因为该表会定期导出到 Excel 和 Access 中,因此简单的加密足以阻止"值,而无需让开发人员(重新)执行视图等.

                  Following Replicate T-SQL DecryptByPassPhrase in C#, I am unable to get a simple encryption with MSSQL to descrypt in C#. The encrypted values in certain columns is necessary because the table is exported into Excel and Access on a regular basis so simple encryption is more than enough to "block" values without having to involve developers to (re)do views, etc.

                  在 SQL Server 2012 中:

                  In SQL Server 2012:

                      select EncryptByPassPhrase( N'hello' , N'world'  ) 
                  -- returns 0x01000000AA959FFB3A8E4B06B734051437E198C8B72000A058ACE91D617123DA102287EB
                  

                  在 C# 中:

                  byte[] buf = System.Text.Encoding.UTF8.GetBytes( "0x010000003A95FA870ED699A5F90D33C2BF01491D9132F61BA162998E96F37117AF5DA0905D51EB6FB298EC88" );
                  // bytes emitted from the database
                  var cp = new TripleDESCryptoServiceProvider();
                  var m = new MemoryStream(buf);
                  cp.Key = System.Text.Encoding.UTF8.GetBytes( "hello" ); // throws
                  cp.IV = System.Text.Encoding.UTF8.GetBytes( "hello" ); // throws
                  CryptoStream cs = new CryptoStream( m , cp.CreateDecryptor( cp.Key , cp.IV ) , CryptoStreamMode.Read );
                  StreamReader reader = new StreamReader( cs );
                  string plainText = reader.ReadToEnd();
                  

                  工作的 C# 代码应该是什么样的?

                  What should working C# code look like?

                  谢谢.

                  推荐答案

                  SQL Server 2017 使用 SHA256 密码哈希 + AES-256 加密

                  SQL Server 2017 uses SHA256 hashing of password + AES-256 encryption

                  旧版本使用密码的 SHA1 散列 + 3DES-128 加密

                  Older versions use SHA1 hashing of password + 3DES-128 encryption

                  IV 大小与块大小相同:AES = 128 位,3DES = 64 位

                  IV size is the same as block size: AES = 128 bits, 3DES = 64 bits

                  填充模式:PKCS #7密码模式:CBC

                  Padding mode: PKCS #7 Cipher mode: CBC

                  服务器 2017 加密的数据以0x02"开头,旧版本以0x01"开头.

                  Data encrypted by server 2017 starts with "0x02", older versions start with "0x01".

                  // Example decrypt:
                  // UInt32 - "magic" (0xbaadf00d): 0d f0 ad ba
                  // UInt16 - unknown (always zero): 00 00
                  // UInt16 - decrypted data length (16): 10 00
                  // byte[] - decrypted data: 4c 65 74 54 68 65 53 75 6e 53 68 69 6e 69 6e 67
                  
                  DecryptCombined("0x02000000266AD4F387FA9474E825B013B0232E73A398A5F72B79BC90D63BD1E45AE3AA5518828D187125BECC285D55FA7CAFED61", "Radames");
                  DecryptCombined("0x010000007854E155CEE338D5E34808BA95367D506B97C63FB5114DD4CE687FE457C1B5D5", "banana");
                  
                  
                  void DecryptCombined(string FromSql, string Password)
                  {
                      // Encode password as UTF16-LE
                      byte[] passwordBytes = Encoding.Unicode.GetBytes(Password);
                  
                      // Remove leading "0x"
                      FromSql = FromSql.Substring(2);
                  
                      int version = BitConverter.ToInt32(StringToByteArray(FromSql.Substring(0, 8)), 0);
                      byte[] encrypted = null;
                  
                      HashAlgorithm hashAlgo = null;
                      SymmetricAlgorithm cryptoAlgo = null;
                      int keySize = (version == 1 ? 16 : 32);
                  
                      if (version == 1)
                      {
                          hashAlgo = SHA1.Create();
                          cryptoAlgo = TripleDES.Create();
                          cryptoAlgo.IV = StringToByteArray(FromSql.Substring(8, 16));
                          encrypted = StringToByteArray(FromSql.Substring(24));
                      }
                      else if (version == 2)
                      {
                          hashAlgo = SHA256.Create();
                          cryptoAlgo = Aes.Create();
                          cryptoAlgo.IV = StringToByteArray(FromSql.Substring(8, 32));
                          encrypted = StringToByteArray(FromSql.Substring(40));
                      }
                      else
                      {
                          throw new Exception("Unsupported encryption");
                      }
                  
                      cryptoAlgo.Padding = PaddingMode.PKCS7;
                      cryptoAlgo.Mode = CipherMode.CBC;
                  
                      hashAlgo.TransformFinalBlock(passwordBytes, 0, passwordBytes.Length);
                      cryptoAlgo.Key = hashAlgo.Hash.Take(keySize).ToArray();
                  
                      byte[] decrypted = cryptoAlgo.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
                      int decryptLength = BitConverter.ToInt16(decrypted, 6);
                      UInt32 magic = BitConverter.ToUInt32(decrypted, 0);
                      if (magic != 0xbaadf00d)
                      {
                          throw new Exception("Decrypt failed");
                      }
                  
                      byte[] decryptedData = decrypted.Skip(8).ToArray();
                      bool isUtf16 = (Array.IndexOf(decryptedData, (byte)0) != -1);
                      string decryptText = (isUtf16 ? Encoding.Unicode.GetString(decryptedData) : Encoding.UTF8.GetString(decryptedData));
                  
                      Console.WriteLine("Result: {0}", decryptText);
                  }
                  
                  // Method taken from https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array?answertab=votes#tab-top
                  public static byte[] StringToByteArray(string hex)
                  {
                      return Enumerable.Range(0, hex.Length)
                                       .Where(x => x % 2 == 0)
                                       .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                                       .ToArray();
                  }
                  

                  这篇关于C# 从 SQL Server EncryptByPassPhrase 解密字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)(超出最大存储过程、函数、触发器或视图嵌套级别(限制 32))
                  How to debug stored procedure in VS 2015?(如何在 VS 2015 中调试存储过程?)
                  How to find a text inside SQL Server procedures / triggers?(如何在 SQL Server 程序/触发器中查找文本?)
                  SQL Server stored procedure return code oddity(SQL Server 存储过程返回码奇怪)
                  Conditional SQL ORDER BY ASC/DESC for alpha columns(alpha 列的条件 SQL ORDER BY ASC/DESC)
                  Export stored procedure result set to Excel in SSMS(在SSMS中将存储过程结果集导出到Excel)
                    <legend id='tbs0F'><style id='tbs0F'><dir id='tbs0F'><q id='tbs0F'></q></dir></style></legend>
                      <tbody id='tbs0F'></tbody>

                    <i id='tbs0F'><tr id='tbs0F'><dt id='tbs0F'><q id='tbs0F'><span id='tbs0F'><b id='tbs0F'><form id='tbs0F'><ins id='tbs0F'></ins><ul id='tbs0F'></ul><sub id='tbs0F'></sub></form><legend id='tbs0F'></legend><bdo id='tbs0F'><pre id='tbs0F'><center id='tbs0F'></center></pre></bdo></b><th id='tbs0F'></th></span></q></dt></tr></i><div id='tbs0F'><tfoot id='tbs0F'></tfoot><dl id='tbs0F'><fieldset id='tbs0F'></fieldset></dl></div>

                        <small id='tbs0F'></small><noframes id='tbs0F'>

                        • <tfoot id='tbs0F'></tfoot>
                          • <bdo id='tbs0F'></bdo><ul id='tbs0F'></ul>