【知识积累】DES算法之C#加密Java解密
一、前言
在項(xiàng)目需要添加安全模塊,客戶端調(diào)用服務(wù)端發(fā)布的service必須要經(jīng)過(guò)驗(yàn)證,加密算法采用DES,客戶端采用C#進(jìn)行加密,服務(wù)端使用Java進(jìn)行解密。廢話不多說(shuō),直接上代碼。
二、客戶端
客戶端采用C#進(jìn)行開(kāi)發(fā),C#進(jìn)行DES加密解密代碼清單如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO;namespace DESHelper {public class DESHelper{private static string m_encryptkey = "ENCRYPTT";private static string m_str = "IAMACOEDR";public static string DESEncrypt(){string str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + m_str;DESCryptoServiceProvider des = new DESCryptoServiceProvider();byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(str);//建立加密對(duì)象的密鑰和偏移量 //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 //使得輸入密碼必須輸入英文文本 des.Key = ASCIIEncoding.ASCII.GetBytes(m_encryptkey);des.IV = ASCIIEncoding.ASCII.GetBytes(m_encryptkey);MemoryStream ms = new MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();StringBuilder ret = new StringBuilder();foreach (byte b in ms.ToArray()){ret.AppendFormat("{0:X2}", b);}ret.ToString();return ret.ToString();}public static string DESDecrypt(string pToDecrypt, string sKey){DESCryptoServiceProvider des = new DESCryptoServiceProvider();byte[] inputByteArray = new byte[pToDecrypt.Length / 2];for (int x = 0; x < pToDecrypt.Length / 2; x++){int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));inputByteArray[x] = (byte)i;}des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);MemoryStream ms = new MemoryStream();CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);cs.Write(inputByteArray, 0, inputByteArray.Length);cs.FlushFinalBlock();StringBuilder ret = new StringBuilder();return System.Text.Encoding.Default.GetString(ms.ToArray());}static void Main(string[] args){string str = DESEncrypt();Console.WriteLine(str);Console.WriteLine(DESDecrypt(str, m_encryptkey));}} } View Code運(yùn)行結(jié)果:
34DB26C86E933FB8F9C294A563BEF742D85451292A3C40C6FC8DF5A99C56EDCC
2016-03-10 12:43:05IAMACOEDR
三、服務(wù)器
服務(wù)器采用Java進(jìn)行開(kāi)發(fā),Java進(jìn)行DES加密解密代碼清單如下:
import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; import java.security.Key; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.Cipher;public class Des { //解密數(shù)據(jù) public static String decrypt(String message,String key) throws Exception { byte[] bytesrc =convertHexString(message); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte); } // 加密數(shù)據(jù)public static byte[] encrypt(String message, String key) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); return cipher.doFinal(message.getBytes("UTF-8")); } public static byte[] convertHexString(String ss) { byte digest[] = new byte[ss.length() / 2]; for(int i = 0; i < digest.length; i++) { String byteString = ss.substring(2 * i, 2 * i + 2); int byteValue = Integer.parseInt(byteString, 16); digest[i] = (byte)byteValue; } return digest; } public static String toHexString(byte b[]) { StringBuffer hexString = new StringBuffer(); for (int i = 0; i < b.length; i++) { String plainText = Integer.toHexString(0xff & b[i]); if (plainText.length() < 2) plainText = "0" + plainText; hexString.append(plainText); } return hexString.toString(); } public static void main(String[] args) throws Exception { String key = "ENCRYPTT"; String value = "IAMACODER"; String formatString = java.net.URLEncoder.encode(value, "utf-8"); System.out.println("加密數(shù)據(jù):"+ formatString); String encryptedString = toHexString(encrypt(formatString, key)); System.out.println("加密后的數(shù)據(jù)為:" + encryptedString); String decryptedString = java.net.URLDecoder.decode(decrypt(encryptedString, key), "utf-8") ; System.out.println("解密后的數(shù)據(jù):" + decryptedString); } } View Code運(yùn)行結(jié)果:
加密數(shù)據(jù):IAMACODER
加密后的數(shù)據(jù)為:a8a3f8641ec18ddeff808105c493510e
解密后的數(shù)據(jù):IAMACODER
四、測(cè)試C#加密&Java解密
將C#端加密的字符串傳入Java端(替換decrypt中的encryptedString即可)直接進(jìn)行解密,可以得到如下結(jié)果:
加密數(shù)據(jù):IAMACODER
加密后的數(shù)據(jù)為:a8a3f8641ec18ddeff808105c493510e
解密后的數(shù)據(jù):2016-03-10 12:43:05IAMACOEDR
五、總結(jié)
這是一個(gè)小的功能模塊,有此需求的園友可以直接拿去使用,謝謝各位園友觀看~
轉(zhuǎn)載于:https://www.cnblogs.com/leesf456/p/5261201.html
總結(jié)
以上是生活随笔為你收集整理的【知识积累】DES算法之C#加密Java解密的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 天运旅游旅行社怎么样
- 下一篇: 陈卓璇晒月光的人光影舞台