一个java的DES加解密类转换成C#.docx
《一个java的DES加解密类转换成C#.docx》由会员分享,可在线阅读,更多相关《一个java的DES加解密类转换成C#.docx(16页珍藏版)》请在冰豆网上搜索。
![一个java的DES加解密类转换成C#.docx](https://file1.bdocx.com/fileroot1/2023-3/18/6ba40bcd-1e1d-4b4a-9e6b-82626e0fb4d3/6ba40bcd-1e1d-4b4a-9e6b-82626e0fb4d31.gif)
一个java的DES加解密类转换成C#
一个java的DES加解密类转换成C#
一个java的des加密解密代码如下:
//packagecom.visionsky.util;
importjava.security.*;
//importjava.util.regex.Pattern;
//importjava.util.Hashtable;
importjavax.crypto.*;
importjavax.crypto.spec.*;
importsun.misc.*;
/**
*des加密解密
*/
publicclassDESPlus{
privatestaticStringstrDefaultKey="PLFP";//默认密钥
privatestaticfinalbyte[]iv={0x12,0x34,0x56,0x78,(byte)0x90,(byte)0xab,(byte)0xcd,(byte)0xef};//des向量
privatestaticBASE64Encoderenc=newBASE64Encoder();//将byte[]转换成String
privatestaticBASE64Decoderdec=newBASE64Decoder();//将String转换成byte[]
/**
*加密字节数组
*
*@paramarrB
* 需加密的字节数组
*@paramkey
* 密钥
*@return加密后的字节数组
*@throwsException
*/
publicstaticbyte[]encrypt(byte[]arrB,Stringkey)throwsException{
DESKeySpecdesKeySpec=newDESKeySpec(key.getBytes());
SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");
SecretKeysecretKey=keyFactory.generateSecret(desKeySpec);
IvParameterSpecivp=newIvParameterSpec(DESPlus.iv);
CipherencryptCipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivp);
returnencryptCipher.doFinal(arrB);
}
/**
*加密字符串
*
*@paramxml
* 需加密的字符串
*@paramkey
* 密钥
*@return加密后的字符串
*@throwsException
*/
publicstaticStringencrypt(Stringxml,Stringkey)throwsException{
//returnDESPlus.enc.encode(encrypt(xml.getBytes(),key));
returnnewString(encrypt(xml.getBytes(),key));
}
/**
*使用默认公钥加密字符串
*@paramxml需加密的字符串
*@return加密后的字符串
*@throwsException
*/
publicstaticStringencrypt(Stringxml)throwsException{
returnencrypt(xml,strDefaultKey);
}
/**
*解密字节数组
*
*@paramarrB
* 需解密的字节数组
*@paramkey
* 密钥
*@return解密后的字节数组
*@throwsException
*/
publicstaticbyte[]decrypt(byte[]arrB,Stringkey)throwsException{
DESKeySpecdesKeySpec=newDESKeySpec(key.getBytes());
SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");
SecretKeysecretKey=keyFactory.generateSecret(desKeySpec);
IvParameterSpecivp=newIvParameterSpec(DESPlus.iv);
CipherdecryptCipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE,secretKey,ivp);
returndecryptCipher.doFinal(arrB);
}
/**
*解密字符串
*
*@paramxml
* 需解密的字符串
*@paramkey
* 密钥
*@return解密后的字符串
*@throwsException
*/
publicstaticStringdecrypt(Stringxml,Stringkey)throwsException{
returnnewString(decrypt(DESPlus.dec.decodeBuffer(xml),key));
}
/**
*使用默认公钥解密字符串
*@paramxml需解密的字符串
*@return解密后的字符串
*@throwsException
*/
publicstaticStringdecrypt(Stringxml)throwsException{
returndecrypt(xml,strDefaultKey);
}
/**
*从指定字符串生成密钥,密钥所需的字节数组长度为8位不足8位时后面补0,超出8位只取前8位
*
*@paramarrBTmp
* 构成该字符串的字节数组
*@return生成的密钥
*@throwsjava.lang.Exception
*/
privateKeygetKey(byte[]arrBTmp)throwsException{
//创建一个空的8位字节数组(默认值为0)
byte[]arrB=newbyte[8];
//将原始字节数组转换为8位
for(inti=0;i arrB[i]=arrBTmp[i];
}
//生成密钥
Keykey=newjavax.crypto.spec.SecretKeySpec(arrB,"DES");
returnkey;
}
/**
*获取默认密钥
*@return
*/
publicstaticStringgetDesKey(){
returnDESPlus.strDefaultKey;
}
publicstaticvoidmain(String[]args){
try{
//测试密匙
Stringkey="BOC_PLFP";
//004交易案例
Stringxml="
xmlversion=\"1.0\"encoding=\"UTF-8\"?
>00417850COM0111091412222226601610000000123客户信息不完整,请补充资料。
";
System.out.println("密钥:
"+key);
System.out.println("加密前的字符串:
"+xml);
System.out.println("加密前的字符串长度:
"+xml.getBytes().length);
//加密
xml=DESPlus.encrypt(xml,key);
System.out.println("加密后的字符串:
"+xml);
System.out.println("加密后的字符串长度:
"+xml.getBytes().length);
//解密
xml=DESPlus.decrypt(xml,key);
System.out.println("解密后的字符串:
"+xml);
System.out.println("解密后的字符串长度:
"+xml.getBytes().length);
}catch(Exceptione){
e.printStackTrace();
}
}
}
//packagecom.visionsky.util;
importjava.security.*;
//importjava.util.regex.Pattern;
//importjava.util.Hashtable;
importjavax.crypto.*;
importjavax.crypto.spec.*;
importsun.misc.*;
/**
*des加密解密
*/
publicclassDESPlus{
privatestaticStringstrDefaultKey="PLFP";//默认密钥
privatestaticfinalbyte[]iv={0x12,0x34,0x56,0x78,(byte)0x90,(byte)0xab,(byte)0xcd,(byte)0xef};//des向量
privatestaticBASE64Encoderenc=newBASE64Encoder();//将byte[]转换成String
privatestaticBASE64Decoderdec=newBASE64Decoder();//将String转换成byte[]
/**
*加密字节数组
*
*@paramarrB
* 需加密的字节数组
*@paramkey
* 密钥
*@return加密后的字节数组
*@throwsException
*/
publicstaticbyte[]encrypt(byte[]arrB,Stringkey)throwsException{
DESKeySpecdesKeySpec=newDESKeySpec(key.getBytes());
SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");
SecretKeysecretKey=keyFactory.generateSecret(desKeySpec);
IvParameterSpecivp=newIvParameterSpec(DESPlus.iv);
CipherencryptCipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivp);
returnencryptCipher.doFinal(arrB);
}
/**
*加密字符串
*
*@paramxml
* 需加密的字符串
*@paramkey
* 密钥
*@return加密后的字符串
*@throwsException
*/
publicstaticStringencrypt(Stringxml,Stringkey)throwsException{
//returnDESPlus.enc.encode(encrypt(xml.getBytes(),key));
returnnewString(encrypt(xml.getBytes(),key));
}
/**
*使用默认公钥加密字符串
*@paramxml需加密的字符串
*@return加密后的字符串
*@throwsException
*/
publicstaticStringencrypt(Stringxml)throwsException{
returnencrypt(xml,strDefaultKey);
}
/**
*解密字节数组
*
*@paramarrB
* 需解密的字节数组
*@paramkey
* 密钥
*@return解密后的字节数组
*@throwsException
*/
publicstaticbyte[]decrypt(byte[]arrB,Stringkey)throwsException{
DESKeySpecdesKeySpec=newDESKeySpec(key.getBytes());
SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");
SecretKeysecretKey=keyFactory.generateSecret(desKeySpec);
IvParameterSpecivp=newIvParameterSpec(DESPlus.iv);
CipherdecryptCipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
decryptCipher.init(Cipher.DECRYPT_MODE,secretKey,ivp);
returndecryptCipher.doFinal(arrB);
}
/**
*解密字符串
*
*@paramxml
* 需解密的字符串
*@paramkey
* 密钥
*@return解密后的字符串
*@throwsException
*/
publicstaticStringdecrypt(Stringxml,Stringkey)throwsException{
returnnewString(decrypt(DESPlus.dec.decodeBuffer(xml),key));
}
/**
*使用默认公钥解密字符串
*@paramxml需解密的字符串
*@return解密后的字符串
*@throwsException
*/
publicstaticStringdecrypt(Stringxml)throwsException{
returndecrypt(xml,strDefaultKey);
}
/**
*从指定字符串生成密钥,密钥所需的字节数组长度为8位不足8位时后面补0,超出8位只取前8位
*
*@paramarrBTmp
* 构成该字符串的字节数组
*@return生成的密钥
*@throwsjava.lang.Exception
*/
privateKeygetKey(byte[]arrBTmp)throwsException{
//创建一个空的8位字节数组(默认值为0)
byte[]arrB=newbyte[8];
//将原始字节数组转换为8位
for(inti=0;i arrB[i]=arrBTmp[i];
}
//生成密钥
Keykey=newjavax.crypto.spec.SecretKeySpec(arrB,"DES");
returnkey;
}
/**
*获取默认密钥
*@return
*/
publicstaticStringgetDesKey(){
returnDESPlus.strDefaultKey;
}
publicstaticvoidmain(String[]args){
try{
//测试密匙
Stringkey="BOC_PLFP";
//004交易案例
Stringxml="
xmlversion=\"1.0\"encoding=\"UTF-8\"?
>00417850COM0111091412222226601610000000123客户信息不完整,请补充资料。
";
System.out.println("密钥:
"+key);
System.out.println("加密前的字符串:
"+xml);
System.out.println("加密前的字符串长度:
"+xml.getBytes().length);
//加密
xml=DESPlus.encrypt(xml,key);
System.out.println("加密后的字符串:
"+xml);
System.out.println("加密后的字符串长度:
"+xml.getBytes().length);
//解密
xml=DESPlus.decrypt(xml,key);
System.out.println("解密后的字符串:
"+xml);
System.out.println("解密后的字符串长度:
"+xml.getBytes().length);
}catch(Exceptione){
e.printStackTrace();
}
}
}
上面运行结果为:
密钥:
BOC_PLFP
加密前的字符串:
xmlversion="1.0"encoding="UTF-8"?
>00417850COM0111091412222226601610000000123客户信息不完整,请补充资料。
加密前的字符串长度:
262
加密后的字符串:
bE5N44gjyfO3SdUs6/OhVg4I4l725S2vWcKBRxYOAd/eAnyuADKXeNNgVXJMj3aJJzndntv364rh
YW2bF33lmEABMU43HfS8DcXX7+QrcIjp3mrk7uJdiNHu4T4oHMeqetFZqU5oh2XY1sbBPPdGEgMf
/OguRVaTblzl/ylkFc6C9BNNSD0IwL0Ks7Mi73+V76P+aFdPgXQc7u4Vkq8Cd6+HgHErbHbJI729
JPJKM5L2YAAW4Q06oi4yMoEASDjYf7Aa1X/FWqclsZImSDB0okGOiuj857l94BM1zYl2RtWdXa9o
0beiL4CbEvKSC3U3PydAI0+mZbtE0sVkyP0sXTke7ifrwiMG
加密后的字符串长度:
360
解密后的字符串:
xmlversion="1.0"encoding="UTF-8"?
>00417850COM0111091412222226601610000000123客户信息不完整,请补充资料。
解密后的字符串长度:
262,修改里面的des向量iv,字符编码UTF8为Default等,最终改写为C#代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;