zoukankan      html  css  js  c++  java
  • 关于.net和java des加密

    在.net和java环境中对于des加密,有几点要协同的地方:

    1. 密钥和密钥向量Key IV
    2. 加密模式CipherMode
    3. 填充模式PaddingMode
    4. 密文编码方式

    下面一段.net的des加密方式

     1 public string GetConsoleUrl(string vmName)
     2 {
     3     string rs = string.Empty;
     4     using (CloudContext db = new CloudContext())
     5     {
     6         var configs = new ConfigurationRepository(db).GetAll().Where(x => x.Key.Equals("ConsoleUrl") || x.Key.Equals("ConsoleKey"));
     7         if (configs != null)
     8         {
     9             string url = string.Empty;
    10             string key = string.Empty;
    11 
    12             if (configs.FirstOrDefault(x => x.Key.Equals("ConsoleUrl")) != null)
    13                 url = configs.First(x => x.Key.Equals("ConsoleUrl")).Value;
    14             if (configs.FirstOrDefault(x => x.Key.Equals("ConsoleKey")) != null)
    15                 key = configs.First(x => x.Key.Equals("ConsoleKey")).Value;
    16 
    17             if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(key))
    18             {
    19                 byte[] rgbKey = Encoding.UTF8.GetBytes(key);
    20                 byte[] inputByteArray = Encoding.UTF8.GetBytes(vmName + "|" + DateTime.Now.ToString("yyyyMMddhhmmssfff"));
    21                 byte[] rgbIV = rgbKey;
    22 
    23                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    24                 des.Mode = CipherMode.ECB;
    25                 MemoryStream ms = new MemoryStream();
    26                 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
    27                 cs.Write(inputByteArray, 0, inputByteArray.Length);
    28                 cs.FlushFinalBlock();
    29 
    30                 StringBuilder sb = new StringBuilder();
    31                 foreach (byte b in ms.ToArray())
    32                 {
    33                     sb.AppendFormat("{0:X2}", b);   
    34                 }
    35                 rs = string.Format("http://{0}?token={1}", url, sb.ToString());
    36                 ms.Close();
    37                 ms.Dispose();
    38 
    39                 return rs;
    40             }
    41         }
    42     }
    43     return rs;
    44 }
  • 相关阅读:
    手把手教你如何逐步安装OpenStack
    掌握OpenStack部署的最佳实践 打破部署失败的魔咒
    大数据服务大比拼:AWS VS. AzureVS.谷歌
    fullcalender
    也谈---基于 HTTP 长连接的“服务(转载)
    调用页面脚本
    mssql 低版本数据库 使用高版本的命令
    行变列 pivot
    HighCharts -在包含容器尺寸发生变化时重新渲染
    ES6 import export
  • 原文地址:https://www.cnblogs.com/yuefei/p/3613480.html
Copyright © 2011-2022 走看看