实现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加密后的字符串进行解密
/// </summary>
/// <param name="encryptedString">待解密的字符串</param>
/// <returns>解密后的字符串</returns>
public string DESDeCode(string encryptedString, string sKey)
{
byte[] btKey = Encoding.Default.GetBytes(sKey);
byte[] btIV = Encoding.Default.GetBytes("12345678");
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
using (MemoryStream ms = new MemoryStream())
{
byte[] inData = Convert.FromBase64String(encryptedString);
try
{
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(inData, 0, inData.Length);
cs.FlushFinalBlock();
}
return Encoding.UTF8.GetString(ms.ToArray());
}
catch
{
throw;
}
}
}
#endregion
//测试
private void button1_Click(object sender, EventArgs e)
{
string jiami = textBox1.Text;
textBox2.Text= DESEnCode(jiami, "11111111");
}
private void button2_Click(object sender, EventArgs e)
{
string jiemi = textBox2.Text;
textBox3.Text = DESDeCode(jiemi,"11111111");
}
}
}