/* * DES3.java * Copyright (c) 2015 众信在线 * All rights reserved. * --------------------------- * 2015年3月30日 Created */ package com.mango.p2p.utils; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; /** * * DES3加解密 * * @author chaizhilei * @version 1.0 2015年3月30日 * */ public class DES3 { // 密钥 private final static String secretKey = "jinrong-online.com-app-c"; // 向量 private final static String iv = "01234567"; // 加解密统一使用的编码方式 private final static String encoding = "utf-8"; /** * 3DES加密 * * @param plainText * 普通文本 * @return * @throws Exception */ public static String encode(String plainText) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes()); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, deskey, ips); byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding)); return Base64.encodeToString(encryptData, Base64.DEFAULT); } /** * 3DES解密 * * @param encryptText * 加密文本 * @return * @throws Exception */ public static String decode(String encryptText) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes()); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, deskey, ips); byte[] decryptData = cipher.doFinal(Base64.decode(encryptText, Base64.DEFAULT)); return new String(decryptData, encoding); } public static void main(String[] args) { try { String jiami = encode("ABC"); System.out.println("加密后的结果" + jiami); String jiemi = decode(jiami); System.out.println("解密结果" + jiemi); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

wang0wang LV2
2020年5月4日
killking LV1
2020年1月21日
ncd12320 LV8
2020年1月12日
luohaipeng LV23
2019年12月6日
低调人 LV38
2019年7月31日
jiajia91323 LV23
2019年4月19日
ttttttt22 LV3
2019年1月7日
shanshantoyou LV6
2018年11月23日
271530419 LV5
2018年10月10日
disoaa LV3
2018年9月17日