一、前言
在项目需要添加安全模块,客户端调用服务端发布的service必须要经过验证,加密算法采用DES,客户端采用C#进行加密,服务端使用Java进行解密。废话不多说,直接上代码。
二、客户端
客户端采用C#进行开发,C#进行DES加密解密代码清单如下:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Security.Cryptography;usingSystem.IO;namespaceDESHelper
{public classDESHelper
{private static string m_encryptkey = "ENCRYPTT";private static string m_str = "IAMACOEDR";public static stringDESEncrypt()
{string str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +m_str;
DESCryptoServiceProvider des= newDESCryptoServiceProvider();byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(str);//建立加密对象的密钥和偏移量//原文使用ASCIIEncoding.ASCII方法的GetBytes方法//使得输入密码必须输入英文文本
des.Key =ASCIIEncoding.ASCII.GetBytes(m_encryptkey);
des.IV=ASCIIEncoding.ASCII.GetBytes(m_encryptkey);
MemoryStream ms=