关于.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 }

 

你可能感兴趣的:(java)