您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 江门分类信息网,免费分类信息发布

如何使用java 3DES加解密

2024/4/26 8:26:05发布70次查看
这次给大家带来如何使用java 3des加解密,使用java 3des加解密的注意事项有哪些,下面就是实战案例,一起来看一下。
<pre name="code" class="java">java写的加密解密算法及调用范例 1、.java算法范例 package common.jutility; import javax.crypto.*; import javax.crypto.spec.secretkeyspec; import com.sun.org.apache.xerces.internal.impl.dv.util.base64; public class encryptutils { /// <summary> /// 3des解码 /// </summary> /// <param name="value">待解密字符串</param> /// <param name="key">原始密钥字符串</param> /// <returns></returns> public static string decrypt3des(string value, string key) throws exception { byte[] b = decryptmode(getkeybytes(key), base64.decode(value)); return new string(b); } /// <summary> /// 3des加密 /// </summary> /// <param name="value">待加密字符串</param> /// <param name="strkey">原始密钥字符串</param> /// <returns></returns> public static string encrypt3des(string value, string key) throws exception { string str = byte2base64(encryptmode(getkeybytes(key), value.getbytes())); return str; } //计算24位长的密码byte值,首先对原始密钥做md5算hash值,再用前8位数据对应补全后8位 public static byte[] getkeybytes(string strkey) throws exception { if (null == strkey || strkey.length() < 1) throw new exception("key is null or empty!"); java.security.messagedigest alg = java.security.messagedigest.getinstance("md5"); alg.update(strkey.getbytes()); byte[] bkey = alg.digest(); system.out.println("md5key.length=" + bkey.length); system.out.println("md5key=" + byte2hex(bkey)); int start = bkey.length; byte[] bkey24 = new byte[24]; for (int i = 0; i < start; i++) { bkey24[i] = bkey[i]; } for (int i = start; i < 24; i++) {//为了与.net16位key兼容 bkey24[i] = bkey[i - start]; } system.out.println("byte24key.length=" + bkey24.length); system.out.println("byte24key=" + byte2hex(bkey24)); return bkey24; } private static final string algorithm = "desede"; //定义 加密算法,可用 des,desede,blowfish //keybyte为加密密钥,长度为24字节 //src为被加密的数据缓冲区(源) public static byte[] encryptmode(byte[] keybyte, byte[] src) { try { //生成密钥 secretkey deskey = new secretkeyspec(keybyte, algorithm); //加密 cipher c1 = cipher.getinstance(algorithm); c1.init(cipher.encrypt_mode, deskey); return c1.dofinal(src); } catch (java.security.nosuchalgorithmexception e1) { e1.printstacktrace(); } catch (javax.crypto.nosuchpaddingexception e2) { e2.printstacktrace(); } catch (java.lang.exception e3) { e3.printstacktrace(); } return null; } //keybyte为加密密钥,长度为24字节 //src为加密后的缓冲区 public static byte[] decryptmode(byte[] keybyte, byte[] src) { try { //生成密钥 secretkey deskey = new secretkeyspec(keybyte, algorithm); //解密 cipher c1 = cipher.getinstance(algorithm); c1.init(cipher.decrypt_mode, deskey); return c1.dofinal(src); } catch (java.security.nosuchalgorithmexception e1) { e1.printstacktrace(); } catch (javax.crypto.nosuchpaddingexception e2) { e2.printstacktrace(); } catch (java.lang.exception e3) { e3.printstacktrace(); } return null; } //转换成base64编码 public static string byte2base64(byte[] b) { return base64.encode(b); } //转换成十六进制字符串 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(); } } 2、java 算法调用范例 package test.com; import com.sun.org.apache.xerces.internal.impl.dv.util.base64; import common.jutility.encryptutils;//用原始密钥经md5转换和补位后的新密钥来做3des加密解密 public class test3des { public static void main(string[] args) { string key = "abcd1234"; string password = "password"; system.out.println("key=" + key + ",password=" + password); system.out.println(); system.out.println("----------示例开始:使用java写的算法加密解密-----------"); try { string encrypt = ""; string decrypt = ""; byte[] bkey = encryptutils.getkeybytes(key); encrypt = encryptutils.byte2base64(encryptutils.encryptmode(bkey, password.getbytes())); system.out.println("用预转换密钥算加密结果=" + encrypt); system.out.println("加密后base64表示=" + encryptutils.byte2hex(base64.decode(encrypt))); system.out.println("调用原始密钥算加密结果=" + encryptutils.encrypt3des(password, key)); try { decrypt = new string(encryptutils.decryptmode(bkey, base64.decode(encrypt))); system.out.println("用预转换密钥算解密结果=" + decrypt); system.out.println("调用原始密钥算解密结果=" + encryptutils.decrypt3des(encrypt, key)); } catch (exception ex) { system.out.println("exception:" + ex.getmessage()); } } catch (exception ex) { system.out.println("exception:" + ex.getmessage()); } system.out.println("----------示例结束:使用java写的算法加密解密-----------"); } } 3、(7)的运算结果 key=abcd1234,password=password ----------示例开始:使用java写的算法加密解密----------- md5key.length=16 md5key=e1:9d:5c:d5:af:03:78:da:05:f6:3f:89:1c:74:67:af byte24key.length=24 byte24key=e1:9d:5c:d5:af:03:78:da:05:f6:3f:89:1c:74:67:af:e1:9d:5c:d5:af:03:78:da 用预转换密钥算加密结果=ftwpzxfh4wpzt4orq8uslq== 加密后base64表示=7e:dc:0f:cf:11:47:e1:6a:73:4f:83:ab:ab:cb:92:2d md5key.length=16 md5key=e1:9d:5c:d5:af:03:78:da:05:f6:3f:89:1c:74:67:af byte24key.length=24 byte24key=e1:9d:5c:d5:af:03:78:da:05:f6:3f:89:1c:74:67:af:e1:9d:5c:d5:af:03:78:da 调用原始密钥算加密结果=ftwpzxfh4wpzt4orq8uslq== 用预转换密钥算解密结果=password md5key.length=16 md5key=e1:9d:5c:d5:af:03:78:da:05:f6:3f:89:1c:74:67:af byte24key.length=24 byte24key=e1:9d:5c:d5:af:03:78:da:05:f6:3f:89:1c:74:67:af:e1:9d:5c:d5:af:03:78:da 调用原始密钥算解密结果=password ----------示例结束:使用java写的算法加密解密-----------
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
移动端web开发中click,touch,tap事件使用详解
rem相对单位使用案例分享
以上就是如何使用java 3des加解密的详细内容。
江门分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录