日期:2014-05-18 浏览次数:21092 次
实现java和C#相互加密与解密 并能保持解密出来一致
?
方法一:
java:
??
?
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;
/**
?*
?* @author Administrator
?*/
public class CryptoTools {
?? // DES加密的私钥,必须是8位长的字符串
??? private static final byte[] DESkey = "11111111".getBytes();// 设置密钥
?private static final byte[] DESIV = "12345678".getBytes();// 设置向量
?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("天下")));
?}
}
?
?
?
?
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
?
?
?
?
?
方法二:
C#:
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();
??????? }
?
????????? 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);
??????????? //建立加密对象的密钥和偏移量???
??????????? //原文使用ASCIIEncoding.ASCII方法的GetBytes方法???
??????????? //使得输入密码必须输入英文文本???
??????????? des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
??????????? des.IV = ASCIIEncoding.ASCII.GetBytes("12345678");
??????????? MemoryStream ms = new MemoryStream();
??????????? CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
??????????? cs.Write(inputByteArray, 0, inputByteArray.Length);
??????????? cs.FlushFinalBlock();
??????????? return Convert.ToBase64String(ms.ToArray());
??????? }
??????? #endregion
??????? #region DESDeCode DES解密
??????? /// <summary>
??????? /// 对DES加密后的字符串进