通过这个方法可以实现java和C#相互加密与解密 并能保持解密出来一致
方法一:通过.NET的key和VI来生成对应于java的key
java:
Java代码
- <span style="color: #888888;">import java.security.Key;
- import java.security.spec.AlgorithmParameterSpec;
- import javax.crypto.Cipher;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.DESKeySpec;
- import javax.crypto.spec.IvParameterSpec;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- public class CryptoTools {
- private static final byte[] DESkey = { (byte) 0x15, (byte) 0xE7,
- (byte) 0xA1, (byte) 0x22, (byte) 0x96, (byte) 0x8B, (byte) 0x24,
- (byte) 0xFA };// 设置密钥,略去
- private static final byte[] DESIV = { (byte) 0xCE, (byte) 0x35, (byte) 0x5,
- (byte) 0xD, (byte) 0x98, (byte) 0x91, (byte) 0x8, (byte) 0xA };// 设置向量,略去
- static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现
- private static Key key = null;
- public CryptoTools() throws Exception {
- DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
- iv = new IvParameterSpec(DESIV);// 设置向量
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
- key = keyFactory.generateSecret(keySpec);// 得到密钥对象
- }
- public String encode(String data) throws Exception {
- Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
- enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
- byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
- BASE64Encoder base64Encoder = new BASE64Encoder();
- return base64Encoder.encode(pasByte);
- }
- public String decode(String data) throws Exception {
- Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
- deCipher.init(Cipher.DECRYPT_MODE, key, iv);
- BASE64Decoder base64Decoder = new BASE64Decoder();
- byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
- return new String(pasByte, "UTF-8");
- }
- public static void main(String[] args) throws Exception {
- CryptoTools tools = new CryptoTools();
- System.out.println("加密:" + tools.encode("天下"));
- System.out.println("解密:" + tools.decode(tools.encode("天下")));
- }
- }</span>
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
方法二:通过java的key来生成对应于.NET的key和VI
C#:
C#代码
- <span style="color: #888888;">using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Configuration;
- using System.Web;
- using System.Security.Cryptography;
- using System.IO;
- namespace DES
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string jiami = textBox1.Text;
- textBox2.Text= DESEnCode(jiami, "11111111");
- }
- public static string DES_Key = "11111111";
- #region DESEnCode DES加密
- public static string DESEnCode(string pToEncrypt, string sKey)
- {
- // string pToEncrypt1 = HttpContext.Current.Server.UrlEncode(pToEncrypt);
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
- //建立加密对象的密钥和偏移量
- //原文使用ASCIIEnc