3DES,Java与C#的加解密?有经验的兄弟帮忙!急需 3DES,Java与C#的加解密?有经验的兄弟帮忙,只要有2边都能通用的就好了,祈求源码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java加密,然后C#解密?那你要重写java的解密过程,或者简单一点,你把java的解密过程写个单独页面让C#传值进去解密打印出来,你再HTTP请求得到解密后的字符串 给你个JAVA的 DES 加密和解密 你再去C# 求一个 C#的 DES加密和解密 不就OK 了?import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class Des { Key key; /** * 根据参数生成KEY * @param strKey */ public void getKey(String strKey) { try { KeyGenerator _generator = KeyGenerator.getInstance("DES"); _generator.init(new SecureRandom(strKey.getBytes())); this.key = _generator.generateKey(); _generator = null; } catch (Exception e) { e.printStackTrace(); } } /** * 加密String明文输入,String密文输出 * @param strMing String明文 * @return String密文 */ public String getEncString(String strMing) { byte[] byteMi = null; byte[] byteMing = null; String strMi = ""; BASE64Encoder base64en = new BASE64Encoder(); try { byteMing = strMing.getBytes("UTF8"); byteMi = this.getEncCode(byteMing); strMi = base64en.encode(byteMi); } catch (Exception e) { e.printStackTrace(); } finally { base64en = null; byteMing = null; byteMi = null; } return strMi; } /** * 解密 以String密文输入,String明文输出 * @param strMi String密文 * @return String明文 */ public String getDesString(String strMi) { BASE64Decoder base64De = new BASE64Decoder(); byte[] byteMing = null; byte[] byteMi = null; String strMing = ""; try { byteMi = base64De.decodeBuffer(strMi); byteMing = this.getDesCode(byteMi); strMing = new String(byteMing, "UTF8"); } catch (Exception e) { e.printStackTrace(); } finally { base64De = null; byteMing = null; byteMi = null; } return strMing; } /** * 加密以byte[]明文输入,byte[]密文输出 * @param byteS * @return */ private byte[] getEncCode(byte[] byteS) { byte[] byteFina = null; Cipher cipher; try { cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); byteFina = cipher.doFinal(byteS); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } /** * 解密以byte[]密文输入,以byte[]明文输出 * @param byteD * @return */ private byte[] getDesCode(byte[] byteD) { Cipher cipher; byte[] byteFina = null; try { cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byteFina = cipher.doFinal(byteD); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } //转化为16进制字符串 public static String byte2hex(byte[] b){ String hs=""; String stmp=""; for(int n=0;n<b.length;n++){ stmp=(java.lang.Integer.toHexString(b[n]&0XFF)); if(stmp.length()==1)hs=hs+"0"+stmp; else hs=hs+stmp; if(n<b.length-1) hs=hs+":"; } return hs.toUpperCase(); } public static void main(String args[]) { Des des=new Des();//实例化一个对像 des.getKey("aadd");//生成密匙 String strEnc = des.getEncString("明文");//加密字符串,返回String的密文 System.out.println("加密文:"+strEnc); String strDes = des.getDesString(strEnc);//把String 类型的密文解密 System.out.println("解密文:"+strDes); }} 参考下http://www.cnblogs.com/flyingchen/archive/2009/10/15/1583649.html我也没做过的 Google了一种可行。一种让Java和.Net兼容的方式,在.Net中指定模式为ECB,填充为PKCS7,然后在Java中采用其默认的模式(DESede/ECB/PKCS5Padding)即可,注意双方约定key的size为24个字节。建议双方对key以base64编码字符串进行告知,因为java和.net中byte字节的范围不相同(前者-128~127,后者0~255),避免不必要的处理。 Hibernate 关联 空对象 java程序员只能写写网站程序吗? java压缩成.zip格式问题 大家帮忙看看这是什么错误,急!先谢谢各位了!ssh开发网上购物商城进行添加商品信息是报错 关于hibnate插件的问题! 求JMS教程 如何WEB页面中显示XML的数据?(XML是传过来的字符串形式) 谁有jb8 for weblogic 的注册机????help me!!!! 从JSP页面的文本框里输入中文,后台JAVA程序中显示乱码 公司突然停电,之后SVN连接报错怎么解决 我这样的SSH结构能正确关闭session么? Struts标签和Jstl标签
那你要重写java的解密过程,或者简单一点,你把java的解密过程写个单独页面让C#传值进去解密打印出来,你再HTTP请求得到解密后的字符串
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;public class Des {
Key key; /**
* 根据参数生成KEY
* @param strKey
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 加密String明文输入,String密文输出
* @param strMing String明文
* @return String密文
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
} /**
* 解密 以String密文输入,String明文输出
* @param strMi String密文
* @return String明文
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
} /**
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
} /**
* 解密以byte[]密文输入,以byte[]明文输出
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
//转化为16进制字符串
public static String byte2hex(byte[] b){
String hs="";
String stmp="";
for(int n=0;n<b.length;n++){
stmp=(java.lang.Integer.toHexString(b[n]&0XFF));
if(stmp.length()==1)hs=hs+"0"+stmp;
else hs=hs+stmp;
if(n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
}
public static void main(String args[]) {
Des des=new Des();//实例化一个对像
des.getKey("aadd");//生成密匙 String strEnc = des.getEncString("明文");//加密字符串,返回String的密文
System.out.println("加密文:"+strEnc); String strDes = des.getDesString(strEnc);//把String 类型的密文解密
System.out.println("解密文:"+strDes);
}
}
http://www.cnblogs.com/flyingchen/archive/2009/10/15/1583649.html
我也没做过的
一种让Java和.Net兼容的方式,在.Net中指定模式为ECB,填充为PKCS7,然后在Java中采用其默认的模式(DESede/ECB/PKCS5Padding)即可,注意双方约定key的size为24个字节。建议双方对key以base64编码字符串进行告知,因为java和.net中byte字节的范围不相同(前者-128~127,后者0~255),避免不必要的处理。