欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

des加密解密JAVA与.NET互通实例

程序员文章站 2023-12-21 13:53:40
java版本 复制代码 代码如下:import javax.crypto.cipher; import javax.crypto.secretkey; import ja...

java版本

复制代码 代码如下:

import javax.crypto.cipher;
import javax.crypto.secretkey;
import javax.crypto.secretkeyfactory;
import javax.crypto.spec.deskeyspec;
import javax.crypto.spec.ivparameterspec;


public class des {
private byte[] deskey;


//解密数据
public static string decrypt(string message,string key) throws exception {

byte[] bytesrc =converthexstring(message);
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.decrypt_mode, secretkey, iv);

byte[] retbyte = cipher.dofinal(bytesrc);
return new string(retbyte);
}

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 byte[] converthexstring(string ss)
{
byte digest[] = new byte[ss.length() / 2];
for(int i = 0; i < digest.length; i++)
{
string bytestring = ss.substring(2 * i, 2 * i + 2);
int bytevalue = integer.parseint(bytestring, 16);
digest[i] = (byte)bytevalue;
}

return digest;
}


public static void main(string[] args) throws exception {
string key = "12345678";
string value="test1234 ";
string jiami=java.net.urlencoder.encode(value, "utf-8").tolowercase();

system.out.println("加密数据:"+jiami);
string a=tohexstring(encrypt(jiami, key)).touppercase();


system.out.println("加密后的数据为:"+a);
string b=java.net.urldecoder.decode(decrypt(a,key), "utf-8") ;
system.out.println("解密后的数据:"+b);

}


public static string tohexstring(byte b[]) {
stringbuffer hexstring = new stringbuffer();
for (int i = 0; i < b.length; i++) {
string plaintext = integer.tohexstring(0xff & b[i]);
if (plaintext.length() < 2)
plaintext = "0" + plaintext;
hexstring.append(plaintext);
}

return hexstring.tostring();
}

}

.net版本

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
using system.security.cryptography;
using system.io;
using system.text;
public class testdes{
//cookies加密密钥
public static string des_key = "12345678";

#region desencode des加密
public static string desencode(string ptoencrypt, string skey)
{
ptoencrypt = httpcontext.current.server.urlencode(ptoencrypt);
descryptoserviceprovider des = new descryptoserviceprovider();
byte[] inputbytearray = encoding.getencoding("utf-8").getbytes(ptoencrypt);


des.key = asciiencoding.ascii.getbytes(skey);
des.iv = asciiencoding.ascii.getbytes(skey);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createencryptor(), cryptostreammode.write);

cs.write(inputbytearray, 0, inputbytearray.length);
cs.flushfinalblock();

stringbuilder ret = new stringbuilder();
foreach (byte b in ms.toarray())
{
ret.appendformat("{0:x2}", b);
}
ret.tostring();
return ret.tostring();
}
#endregion

#region desdecode des解密
public static string desdecode(string ptodecrypt, string skey)
{
// httpcontext.current.response.write(ptodecrypt + "<br>" + skey);
// httpcontext.current.response.end();
descryptoserviceprovider des = new descryptoserviceprovider();

byte[] inputbytearray = new byte[ptodecrypt.length / 2];
for (int x = 0; x < ptodecrypt.length / 2; x++)
{
int i = (convert.toint32(ptodecrypt.substring(x * 2, 2), 16));
inputbytearray[x] = (byte)i;
}

des.key = asciiencoding.ascii.getbytes(skey);
des.iv = asciiencoding.ascii.getbytes(skey);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createdecryptor(), cryptostreammode.write);
cs.write(inputbytearray, 0, inputbytearray.length);
cs.flushfinalblock();

stringbuilder ret = new stringbuilder();

return httpcontext.current.server.urldecode(system.text.encoding.default.getstring(ms.toarray()));
}
#endregion

public testdes()
{
//
// todo: 在此处添加构造函数逻辑
//
}
}

上一篇:

下一篇: