Java实现的3des加密解密工具类示例
程序员文章站
2024-04-02 09:07:58
本文实例讲述了java实现的3des加密解密工具类。分享给大家供大家参考,具体如下:
package com.gcloud.common;
import org...
本文实例讲述了java实现的3des加密解密工具类。分享给大家供大家参考,具体如下:
package com.gcloud.common; import org.apache.poi.poifs.property.child; import org.bouncycastle.jce.provider.bouncycastleprovider; import javax.crypto.*; import javax.crypto.spec.ivparameterspec; import javax.crypto.spec.secretkeyspec; import java.io.bufferedreader; import java.io.file; import java.io.ioexception; import java.io.inputstreamreader; import java.net.url; import java.security.*; import java.security.spec.algorithmparameterspec; /** * 三重数据加密算法工具类 * created by charlin on 2017/9/11. */ public class v3desutil { //密钥存放位置 public static string filename = "d:/3des.key"; // 1为加密,0为解密 private int isencrypt = -1; // 加密/解密密钥,长度为16byte或者24byte。 private string keystr; // 要加密/解密信息(解密时需为十六进制显示的字符串) private string message; public v3desutil() { } public v3desutil(int isencrypt, string keystr, string message) { this.isencrypt = isencrypt; this.keystr = keystr; this.message = message; } //numstr = 12345678 public string v3deschiper(string numstr) { string result = null; try { security.addprovider(new bouncycastleprovider()); url url = getclass().getresource(filename); file myfile = new file(filename); if (!myfile.exists()) { return "can't find " + filename; } try { bufferedreader in = new bufferedreader(new inputstreamreader(url.openstream())); while ((keystr = in.readline()) == null) { return "读取密钥失败!"; } in.close(); } catch (ioexception e) { e.printstacktrace(); } secretkey key = new secretkeyspec(keystr.getbytes(), "desede"); result = null; byte[] textbyte = null; byte[] messagebyte = null; cipher cipher = cipher.getinstance("desede/cbc/pkcs5padding", "bc"); algorithmparameterspec spec = new ivparameterspec(numstr.getbytes()); if (isencrypt == 1) { messagebyte = message.getbytes(); cipher.init(cipher.encrypt_mode, key, spec); } else if (isencrypt == 0) { messagebyte = decodehex(message); cipher.init(cipher.decrypt_mode, key, spec); } else { return "加解密设置错误,请确认输入:1为加密;0为解密"; } textbyte = cipher.dofinal(messagebyte); if (isencrypt == 1) { result = encodehex(textbyte); } else if (isencrypt == 0) { result = new string(textbyte); } } catch (exception e) { e.printstacktrace(); } return result; } public static final string encodehex(byte bytes[]) { stringbuffer buf = new stringbuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { if ((bytes[i] & 0xff) < 16) buf.append("0"); buf.append(long.tostring(bytes[i] & 0xff, 16)); } return buf.tostring(); } public static final byte[] decodehex(string hex) { char chars[] = hex.tochararray(); byte bytes[] = new byte[chars.length / 2]; int bytecount = 0; for (int i = 0; i < chars.length; i += 2) { int newbyte = 0; newbyte |= hexchartobyte(chars[i]); newbyte <<= 4; newbyte |= hexchartobyte(chars[i + 1]); bytes[bytecount] = (byte) newbyte; bytecount++; } return bytes; } private static final byte hexchartobyte(char ch) { switch (ch) { case 48: // '0' return 0; case 49: // '1' return 1; case 50: // '2' return 2; case 51: // '3' return 3; case 52: // '4' return 4; case 53: // '5' return 5; case 54: // '6' return 6; case 55: // '7' return 7; case 56: // '8' return 8; case 57: // '9' return 9; case 97: // 'a' return 10; case 98: // 'b' return 11; case 99: // 'c' return 12; case 100: // 'd' return 13; case 101: // 'e' return 14; case 102: // 'f' return 15; case 58: // ':' case 59: // ';' case 60: // '<' case 61: // '=' case 62: // '>' case 63: // '?' case 64: // '@' case 65: // 'a' case 66: // 'b' case 67: // 'c' case 68: // 'd' case 69: // 'e' case 70: // 'f' case 71: // 'g' case 72: // 'h' case 73: // 'i' case 74: // 'j' case 75: // 'k' case 76: // 'l' case 77: // 'm' case 78: // 'n' case 79: // 'o' case 80: // 'p' case 81: // 'q' case 82: // 'r' case 83: // 's' case 84: // 't' case 85: // 'u' case 86: // 'v' case 87: // 'w' case 88: // 'x' case 89: // 'y' case 90: // 'z' case 91: // '[' case 92: // '\\' case 93: // ']' case 94: // '^' case 95: // '_' case 96: // '`' default: return 0; } } public static string getfilename() { return filename; } public int getisencrypt() { return isencrypt; } public void setisencrypt(int isencrypt) { this.isencrypt = isencrypt; } public string getkeystr() { return keystr; } public void setkeystr(string keystr) { this.keystr = keystr; } public string getmessage() { return message; } public void setmessage(string message) { this.message = message; } public static void main(string[] args) { string key = "yycg12345678901234567890"; string oldstring = "test" + "#" + "test" + "#" + system.currenttimemillis(); v3desutil tempdesen = new v3desutil(1, oldstring, key); string strtemp = tempdesen.v3deschiper("12345678"); system.out.println("strtemp=== " + strtemp); v3desutil tempde = new v3desutil(0, strtemp, key); string strtempde = tempde.v3deschiper("12345678"); system.out.println("strtempde===" + strtempde); } }
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含aes、des、rc4等):
md5在线加密工具:
http://tools.jb51.net/password/createmd5password
在线散列/哈希算法加密工具:
在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
在线sha1/sha224/sha256/sha384/sha512加密工具:
更多关于java相关内容感兴趣的读者可查看本站专题:《java数学运算技巧总结》、《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。