3DES,32位长秘钥加密
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3DES,32位长秘钥加密相关的知识,希望对你有一定的参考价值。
一般3des加密的秘钥是一个24位的字节数组,但是很多遇到32位字符串秘钥,不知道怎么去用,其实只是经过几步转化就可以了。希望这篇文章对大家有帮助或者带来灵感
比如:
秘钥:33333333333333333333333333333333
要加密内容:06111111FFFFFFFF
加密后内容:66322DAA27A95807
java代码
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import com.raying.abs.base.logger.Logger; /** * 3DES加密工具类 * @author QiaoZhenwu */ public class Des3EncryptUtils { /** 密钥 */ private SecretKey securekey; /** * setKey(设置byte[] KEY值) (算法中需要通过key来得到加密用的key) */ public void setKey(byte[] key) { try { DESedeKeySpec dks = new DESedeKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); securekey = keyFactory.generateSecret(dks); } catch (Exception e) { Logger.error(e.getMessage(), e); } } /** * get3DesEncCode(3DesECB加密byte[]明文) */ public byte[] get3DesEncCode(byte[] byteS) { byte[] byteFina = null; Cipher cipher; try { cipher = Cipher.getInstance("DESede/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, securekey); byteFina = cipher.doFinal(byteS); } catch (Exception e) { Logger.error(e.getMessage(), e); } finally { cipher = null; } return byteFina; } /** * get3DesDesCode(3DesECB解密byte[]密文) */ public byte[] get3DesDesCode(byte[] byteD) { Cipher cipher; byte[] byteFina = null; try { cipher = Cipher.getInstance("DESede/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, securekey); byteFina = cipher.doFinal(byteD); } catch (Exception e) { Logger.error(e.getMessage(), e); } finally { cipher = null; } return byteFina; } } /** * 3DES加解密主类,加解密调用内部的方法 * @author QiaoZhenwu * */ public class Des3Utils { /** * dec:(解密). * @param key 密钥 * @param content 密文内容 16位 * @return 返回结果:String */ public static String decryption(String key, String content) { Des3EncryptUtils des = new Des3EncryptUtils(); String enKey = "";//最终解密秘钥 48位 String enContent = "";//解密内容 if(key.length() <= 32){ enKey = (key + key).substring(0, 48); }else if(key.length() >= 48){ enKey = key.substring(0, 48); } if(content.length() == 16){ enContent = content; }else{ if(content.length() > 16){ throw new RuntimeException("the encrypt content length more than 16"); }else if(content.length() < 16){ throw new RuntimeException("the encrypt content length less than 16"); } } des.setKey(enKey.getBytes()); byte[] get3DesDesCode = des.get3DesDesCode(HexUtils.fromString(enContent)); return HexUtils.toString(get3DesDesCode).trim(); } /** * dec:(加密). * @param key 密钥 * @param content 密文内容 16位 * @return 返回结果:String */ public static String encryption(String key, String content) { Des3EncryptUtils des = new Des3EncryptUtils(); String enKey = "";//最终加密秘钥48位 String enContent = "";//加密内容 if(key.length() <= 32){ enKey = (key + key).substring(0, 48); }else if(key.length() >= 48){ enKey = key.substring(0, 48); } if(content.length() == 16){ enContent = content; }else{ if(content.length() > 16){ throw new RuntimeException("the encrypt content length more than 16"); }else if(content.length() < 16){ throw new RuntimeException("the encrypt content length less than 16"); } } des.setKey(enKey.getBytes()); byte[] bye = des.get3DesEncCode(HexUtils.fromString(enContent)); return HexUtils.toString(bye).trim(); } } /** * 十六进制帮助类 * @author QiaoZhenwu */ public class HexUtils { /** 转换数据 */ private static final char[] HEXDIGITS = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘ }; /** * toString(控制长度的将byte[]的转换为相应的十六进制String表示) */ public static String toString(byte[] ba, int offset, int length) { char[] buf = new char[length * 2]; int j = 0; int k; for (int i = offset; i < offset + length; i++) { k = ba[i]; buf[j++] = HEXDIGITS[(k >>> 4) & 0x0F]; buf[j++] = HEXDIGITS[k & 0x0F]; } return new String(buf); } /** * toString(将byte[]的转换为相应的十六进制String表示) */ public static String toString(byte[] ba) { return toString(ba, 0, ba.length); } /** * fromString(将十六进制形式的字符串转换为byte[]) */ public static byte[] fromString(String hex) { int len = hex.length(); byte[] buf = new byte[(len + 1) / 2]; int i = 0; int j = 0; if ((len % 2) == 1) { buf[j++] = (byte) fromDigit(hex.charAt(i++)); } while (i < len) { buf[j++] = (byte) ((fromDigit(hex.charAt(i++)) << 4) | fromDigit(hex.charAt(i++))); } return buf; } /** * fromDigit(将十六进制的char转换为十进制的int值) */ public static int fromDigit(char ch) { if (ch >= ‘0‘ && ch <= ‘9‘) { return ch - ‘0‘; } if (ch >= ‘A‘ && ch <= ‘F‘) { return ch - ‘A‘ + 10; } if (ch >= ‘a‘ && ch <= ‘f‘) { return ch - ‘a‘ + 10; } throw new IllegalArgumentException("invalid hex digit ‘" + ch + "‘"); } }
本文出自 “my dream fly on the sky” 博客,谢绝转载!
以上是关于3DES,32位长秘钥加密的主要内容,如果未能解决你的问题,请参考以下文章