【问题标题】:asp.net mvc kendo ui grid encrypt column dataasp.net mvc kendo ui grid 加密列数据
【发布时间】:2016-06-23 12:59:37
【问题描述】:
var grid = $("#grid").kendoGrid({ 数据源:数据源, 可分页:真, 身高:430, 工具栏:[“创建”], 列: [ “产品名称”, { 字段:“身份证”, 标题:“产品编号”, 宽度:“100 像素” }, { 字段:“单价”, 标题:“单价”, 格式:“{0:c}”, 宽度:“100 像素” }, { 字段:“UnitsInStock”, 标题:“库存单位”, 宽度:“100 像素” }, { 字段:“停产”, 宽度:“100 像素” }, { 命令:[“编辑”,“销毁”], 标题: ” ”, 宽度:“172px” } ], 可编辑:“内联” }).data("kendoGrid");

如何加密 kendo ui 网格上的列 Product ID 以使用户看不到我的真实 ID?我正在使用 ASP.NET MVC 5。

谢谢!

【问题讨论】:

    标签: asp.net asp.net-mvc kendo-ui kendo-asp.net-mvc kendo-ui-mvc


    【解决方案1】:

    您可以将 kendo 网格客户端模板用于将 Id 值传递给 javascript 函数的列,而不是直接加密,使用您的算法对其进行加密,然后将其返回。 类似的东西。

    columns.Bound(client => client.Id).ClientTemplate("#=Encrypt(Id)#");
    

     <script> 
    function Encrypt(id)
    {
    // Logic to Encrypt ID
    return encryptedID.toString();
    }
    </script>
    

    如果这只是为了向用户展示,那么此解决方案有效

    第二个解决方案是隐藏列(我的意思是没有向用户显示 ID)

    如果你觉得这有帮助,请标记为答案

    【讨论】:

      【解决方案2】:

      Enrcypt data before you load data in your code behind after that bind your data.

      为了做到这一点,使用下面的 encrpyt 类

      public class DataEncryptor
      {
          TripleDESCryptoServiceProvider symm;
      
          #region Factory
          public DataEncryptor()
          {
              this.symm = new TripleDESCryptoServiceProvider();
              this.symm.Padding = PaddingMode.PKCS7;
          }
          public DataEncryptor(TripleDESCryptoServiceProvider keys)
          {
              this.symm = keys;
          }
      
          public DataEncryptor(byte[] key, byte[] iv)
          {
              this.symm = new TripleDESCryptoServiceProvider();
              this.symm.Padding = PaddingMode.PKCS7;
              this.symm.Key = key;
              this.symm.IV = iv;
          }
      
          #endregion
      
          #region Properties
          public TripleDESCryptoServiceProvider Algorithm
          {
              get { return symm; }
              set { symm = value; }
          }
          public byte[] Key
          {
              get { return symm.Key; }
              set { symm.Key = value; }
          }
          public byte[] IV
          {
              get { return symm.IV; }
              set { symm.IV = value; }
          }
      
          #endregion
      
          #region Crypto
      
          public byte[] Encrypt(byte[] data) { return Encrypt(data, data.Length); }
          public byte[] Encrypt(byte[] data, int length)
          {
              try
              {
                  // Create a MemoryStream.
                  var ms = new MemoryStream();
      
                  // Create a CryptoStream using the MemoryStream 
                  // and the passed key and initialization vector (IV).
                  var cs = new CryptoStream(ms,
                      symm.CreateEncryptor(symm.Key, symm.IV),
                      CryptoStreamMode.Write);
      
                  // Write the byte array to the crypto stream and flush it.
                  cs.Write(data, 0, length);
                  cs.FlushFinalBlock();
      
                  // Get an array of bytes from the 
                  // MemoryStream that holds the 
                  // encrypted data.
                  byte[] ret = ms.ToArray();
      
                  // Close the streams.
                  cs.Close();
                  ms.Close();
      
                  // Return the encrypted buffer.
                  return ret;
              }
              catch (CryptographicException ex)
              {
                  Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
              }
              return null;
          }
      
          public string EncryptString(string text)
          {
              return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(text)));
          }
      
          public byte[] Decrypt(byte[] data) { return Decrypt(data, data.Length); }
          public byte[] Decrypt(byte[] data, int length)
          {
              try
              {
                  // Create a new MemoryStream using the passed 
                  // array of encrypted data.
                  MemoryStream ms = new MemoryStream(data);
      
                  // Create a CryptoStream using the MemoryStream 
                  // and the passed key and initialization vector (IV).
                  CryptoStream cs = new CryptoStream(ms,
                      symm.CreateDecryptor(symm.Key, symm.IV),
                      CryptoStreamMode.Read);
      
                  // Create buffer to hold the decrypted data.
                  byte[] result = new byte[length];
      
                  // Read the decrypted data out of the crypto stream
                  // and place it into the temporary buffer.
                  cs.Read(result, 0, result.Length);
                  return result;
              }
              catch (CryptographicException ex)
              {
                  Console.WriteLine("A cryptographic error occured: {0}", ex.Message);
              }
              return null;
          }
      
          public string DecryptString(string data)
          {
              return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(data))).TrimEnd('\0');
          }
      
          #endregion
      
      }
      

      并像这样使用它:

      string message="A very secret message here.";
      DataEncryptor keys=new DataEncryptor();
      string encr=keys.EncryptString(message);
      
      // later
      string actual=keys.DecryptString(encr);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多