Java实现的3des加密解密工具类示例

 更新时间:2017年10月25日 09:23:12   作者:CharlinGod  
这篇文章主要介绍了Java实现的3des加密解密工具类,结合完整实例形式分析了3des加密解密的具体步骤与相关操作技巧,需要的朋友可以参考下

本文实例讲述了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等):
http://tools.jb51.net/password/txt_encode

MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多关于java相关内容感兴趣的读者可查看本站专题:《Java数学运算技巧总结》、《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • Java JDK与cglib动态代理有什么区别

    Java JDK与cglib动态代理有什么区别

    这篇文章主要介绍了Java JDK动态代理和cglib动态代理的区别文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-03-03
  • 使用MockMvc进行controller层单元测试 事务自动回滚的完整案例

    使用MockMvc进行controller层单元测试 事务自动回滚的完整案例

    这篇文章主要介绍了使用MockMvc进行controller层单元测试 事务自动回滚的完整案例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • javaweb 国际化:DateFormat,NumberFormat,MessageFormat,ResourceBundle的使用

    javaweb 国际化:DateFormat,NumberFormat,MessageFormat,ResourceBu

    本文主要介绍javaWEB国际化的知识,这里整理了详细的资料及实现代码,有兴趣的小伙伴可以参考下
    2016-09-09
  • java 设计模式从风控链理解责任链模式

    java 设计模式从风控链理解责任链模式

    这篇文章主要为大家介绍了java 设计模式从风控链理解责任链模式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Spring配置类源码分析详解

    Spring配置类源码分析详解

    这篇文章主要介绍了Spring配置类解析源码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-09-09
  • MyBatis复杂Sql查询实现示例介绍

    MyBatis复杂Sql查询实现示例介绍

    在利用mybatis做查询的时候,一般返回结果用resulttype,这种情况必须是查询的结果在对应 的pojo类中有对应的,一般都是单表查询,但是对于一些复杂的情况,比如需要用到多表查询的时候,resultType不再适用,此时一般用resultMap来表示返回的结果
    2022-12-12
  • SpringBoot中间件ORM框架实现案例详解(Mybatis)

    SpringBoot中间件ORM框架实现案例详解(Mybatis)

    这篇文章主要介绍了SpringBoot中间件ORM框架实现案例详解(Mybatis),本篇文章提炼出mybatis最经典、最精简、最核心的代码设计,来实现一个mini-mybatis,从而熟悉并掌握ORM框架的涉及实现,需要的朋友可以参考下
    2023-07-07
  • springboot读取nacos配置文件的实现

    springboot读取nacos配置文件的实现

    SpringBoot注册服务到Nacos上,由Nacos来做服务的管理,本文主要介绍了springboot读取nacos配置文件的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • 了解JAVA并发工具常用设计套路

    了解JAVA并发工具常用设计套路

    这篇文章主要介绍了了解JAVA并发工具常用设计套路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
    2019-06-06
  • idea中acitviti使用acitBPM插件出现乱码问题及解决方法

    idea中acitviti使用acitBPM插件出现乱码问题及解决方法

    这篇文章主要介绍了idea中acitviti使用acitBPM插件出现乱码问题及解决方法,通过将File Encodings内容设置为UTF-8,本文通过图文展示,需要的朋友可以参考下
    2021-06-06

最新评论