java代码如下:
package com.gzzy.hsso.util;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.codec.binary.Base64;
public class DES{
/**
* 解密数据
*/
public static String decrypt(byte[] bytesrc, String key) throws Exception{
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");//DES/ECB/NoPadding 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);
}
/**
* 加密
* @param message
* @param key
* @return
* @throws Exception
*/
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 void main(String[] args) throws Exception{
String key = "12345678";
String value="cneirong";
byte[] b1 = encrypt(value,key);
String enstr=Base64.encodeBase64String(b1);
System.out.println("CBC加密后的数据:"+enstr);
byte[] db =Base64.decodeBase64(enstr);
String str=decrypt(db, key);
System.out.println("CBC解密后的数据:"+str);
}
}
C#代码代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program{
string _Key = "12345678";//加密密钥长度必须为8位
public string Key
{
get
{
return _Key;
}
set
{
_Key = value;
}
}
static void Main(string[] args)
{
var str = "cneirong";
var s1 = new Program().Encrypt(str);
Console.WriteLine("CBC加密后:" + s1);
var s2 = new Program().Decrypt(s1);
Console.WriteLine("CBC解密后:" + s2);
var s3 = new Program().DESEncrypt(str, "12345678");
Console.WriteLine("ECB加密后:" + s3);
Console.ReadLine();
}
public string DESEncrypt(string encryptStr, string encryptKey)
{
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(encryptStr);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;//C#默认使用的是CBC 注意这里:如果没有这句,则始终加密过后字符串与java不一致 // des.Mode = CipherMode.ECB;
des.Key = Encoding.ASCII.GetBytes(encryptKey.Substring(0, 8));
des.IV = Encoding.ASCII.GetBytes(encryptKey.Substring(0, 8));
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());
}
public string Encrypt(string Text)
{
try
{
if (Text + "" == "")
{
return "";
}
byte[] TextByteArray = Encoding.Default.GetBytes(Text);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(Key);
DES.IV = ASCIIEncoding.ASCII.GetBytes(Key);
MemoryStream TextMemoryStream = new MemoryStream();
CryptoStream TextCryptoStream = new CryptoStream(TextMemoryStream, DES.CreateEncryptor(), CryptoStreamMode.Write);
TextCryptoStream.Write(TextByteArray, 0, TextByteArray.Length);
TextCryptoStream.FlushFinalBlock();
string PasswordText = Convert.ToBase64String(TextMemoryStream.ToArray(), 0, TextMemoryStream.ToArray().Length);
return PasswordText;
}catch{
return Text;
}
}
public string Decrypt(string PasswordText)
{
try
{
if (PasswordText + "" == "")
{
return "";
}
byte[] PasswordTextByteArray = Convert.FromBase64String(PasswordText);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(Key);
DES.IV = ASCIIEncoding.ASCII.GetBytes(Key);
MemoryStream PasswordTextMemoryStream = new MemoryStream();
CryptoStream PasswordTextCryptoStream = new CryptoStream(PasswordTextMemoryStream, DES.CreateDecryptor(), CryptoStreamMode.Write);
PasswordTextCryptoStream.Write(PasswordTextByteArray, 0, PasswordTextByteArray.Length);
PasswordTextCryptoStream.FlushFinalBlock();
string Text = Encoding.Default.GetString(PasswordTextMemoryStream.ToArray());
return Text;
}
catch
{
return PasswordText;
}
}
}
}