DES加密算法约定

在使用接口过程中所有需要用到加密的地方,都采用DES算法。

约定如下:加密模式为CBC,填充为PKCS5Padding,Key为希财网分配给P2P平台的client_secret的前8位,向量IV与Key相同

如果加密结果中包含& + = 等特殊符号,请使用URLEncode编码

以下分别是C#、Java、PHP对该算法的实现代码

1、C#

		public string Encrypt(string Text, string Key)
		{
		DESCryptoServiceProvider des = new DESCryptoServiceProvider();
		byte[] inputByteArray;
		inputByteArray = Encoding.Default.GetBytes(Text);
		des.Key = ASCIIEncoding.ASCII.GetBytes(Key);
		des.IV = ASCIIEncoding.ASCII.GetBytes(Key);
		des.Mode = CipherMode.CBC;
		System.IO.MemoryStream ms = new System.IO.MemoryStream();
		using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
		{
		cs.Write(inputByteArray, 0, inputByteArray.Length);
		cs.FlushFinalBlock();
		cs.Close();
		}
		string str = Convert.ToBase64String(ms.ToArray());
		ms.Close();
		return str;

		}


		public string Decrypt(string Text, string Key)
		{
		DESCryptoServiceProvider des = new DESCryptoServiceProvider();
		byte[] inputByteArray = Convert.FromBase64String(Text);//Encoding.UTF8.GetBytes(source);

		des.Key = ASCIIEncoding.ASCII.GetBytes(Key);
		des.IV = ASCIIEncoding.ASCII.GetBytes(Key);
		des.Mode = CipherMode.CBC;
		System.IO.MemoryStream ms = new System.IO.MemoryStream();
		using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
		{
		cs.Write(inputByteArray, 0, inputByteArray.Length);
		cs.FlushFinalBlock();
		cs.Close();
		}
		string str = Encoding.UTF8.GetString(ms.ToArray());
		ms.Close();
		return str;
		}
	

1、Java

		//加密
		public static String encrypt (String data,String key)
		throws Exception
		{
		byte[] bt=encrypt(data.getBytes(),key.getBytes());
		String strs=new BASE64Encoder().encode(bt);
		return strs;
		}
		//解密
		public static String decrypt(String data,String key)
		throws Exception
		{
		if(data==null)
		return null;
		BASE64Decoder decorder=new BASE64Decoder();
		byte[] buf=decorder.decodeBuffer(data);
		byte[] bt=decrypt(buf,key.getBytes());
		return new String(bt);
		}

		//加密方法
		public static byte[] encrypt(byte[] data,byte[] key)
		throws Exception
		{
		DESKeySpec dks=new DESKeySpec(key);
		SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
		SecretKey secretkey=keyFactory.generateSecret(dks);
		Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
		IvParameterSpec iv = new IvParameterSpec(key);
		cipher.init(Cipher.ENCRYPT_MODE, secretkey,iv);
		return cipher.doFinal(data);
		}


		//解密方法
		public static byte[] decrypt(byte[] data,byte[] key)
		throws Exception
		{
		DESKeySpec dks=new DESKeySpec(key);
		SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
		SecretKey secretkey=keyFactory.generateSecret(dks);
		Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
		IvParameterSpec iv = new IvParameterSpec(key);
		cipher.init(Cipher.DECRYPT_MODE, secretkey,iv);
		return cipher.doFinal(data);
		}
	

3、PHP

		function encode($text,$key){
		return base64_encode(mcrypt_encrypt(MCRYPT_DES, $key,pkcs5Pad($text, mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC)), MCRYPT_MODE_CBC, $key));
		}

		function decrypt($str,$key){
		return pkcs5Unpad(mcrypt_cbc(MCRYPT_DES, $key, base64_decode ($str), MCRYPT_DECRYPT, $key ));
		}

		function pkcs5Pad($text, $blocksize) {
		$pad = $blocksize - (strlen($text) % $blocksize);
		return $text . str_repeat(chr($pad), $pad);
		}

		function pkcs5Unpad($text) {
		$pad = ord($text {strlen($text) - 1});
		if ($pad > strlen($text))
		return false;

		if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
		return false;

		return substr($text, 0, - 1 * $pad);
		}
	

字符串:AAAAAAAA,密钥:z1z1z1z1,加密结果为:OZ9bVaoMPMWQeOjnO7evaQ==

希财网 版权所有 © 2014-2017  湘ICP备10026015号