1、java的加密解密bytealgorithm加密string解密importimport java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.spec.InvalidKeySpecException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.Ille
2、galBlockSizeException;import javax.crypto.KeyGenerator;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;public class DESEncryptTest private static final String DES_ALGORITHM = DES; /* * DES加密 * param pla
3、inData * param secretKey * return * throws Exception */ public String encryption(String plainData, String secretKey) throws Exception Cipher cipher = null; try cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, generateKey(secretKey); catch (NoSuchAlgorithmException e) e.pr
4、intStackTrace(); catch (NoSuchPaddingException e) e.printStackTrace(); catch(InvalidKeyException e) try / 为了防止解密时报javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher异常, / 不能把加密后的字节数组直接转换成字符串 byte buf = cipher.doFinal(plainData.getBytes(); ret
5、urn Base64Utils.encode(buf); catch (IllegalBlockSizeException e) e.printStackTrace(); throw new Exception(IllegalBlockSizeException, e); catch (BadPaddingException e) e.printStackTrace(); throw new Exception(BadPaddingException, e); /* * DES解密 * param secretData * param secretKey * return * throws E
6、xception */ public String decryption(String secretData, String secretKey) throws Exception Cipher cipher = null; try cipher = Cipher.getInstance(DES_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey); catch (NoSuchAlgorithmException e) e.printStackTrace(); throw new Exception(NoSuch
7、AlgorithmException, e); catch (NoSuchPaddingException e) e.printStackTrace(); throw new Exception(NoSuchPaddingException, e); catch(InvalidKeyException e) e.printStackTrace(); throw new Exception(InvalidKeyException, e); try byte buf = cipher.doFinal(Base64Utils.decode(secretData.toCharArray(); retu
8、rn new String(buf); catch (IllegalBlockSizeException e) e.printStackTrace(); throw new Exception(IllegalBlockSizeException, e); catch (BadPaddingException e) e.printStackTrace(); throw new Exception(BadPaddingException, e); /* * 获得秘密密钥 * * param secretKey * return * throws NoSuchAlgorithmException *
9、/ private SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException SecureRandom secureRandom = new SecureRandom(secretKey.getBytes(); / 为我们选择的DES算法生成一个KeyGenerator对象 KeyGenerator kg = null; try kg = KeyGenerator.getInstance(DES_ALGORITHM); catch (NoSuchAlgorithmException e) kg.init(se
10、cureRandom); /kg.init(56, secureRandom); / 生成密钥 return kg.generateKey(); public static void main(String a) throws Exception String input = cy11Xlbrmzyh:604:301:1353064296; String key = 37d5aed075525d4fa0fe635231cba447; DESEncryptTest des = new DESEncryptTest(); String result = des.encryption(input,
11、key); System.out.println(result); System.out.println(des.decryption(result, key); static class Base64Utils static private char alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=.toCharArray(); static private byte codes = new byte256; static for (int i = 0; i 256; i+) codesi
12、 = -1; for (int i = A; i = Z; i+) codesi = (byte) (i - A); for (int i = a; i = z; i+) codesi = (byte) (26 + i - a); for (int i = 0; i = 9; i+) codesi = (byte) (52 + i - 0); codes+ = 62; codes/ = 63; /* * 将原始数据编码为base64编码 */ static public String encode(byte data) char out = new char(data.length + 2)
13、/ 3) * 4; for (int i = 0, index = 0; i data.length; i += 3, index += 4) boolean quad = false; boolean trip = false; int val = (0xFF & (int) datai); val = 8; if (i + 1) data.length) val |= (0xFF & (int) datai + 1); trip = true; val = 8; if (i + 2) = 6; outindex + 2 = alphabet(trip ? (val & 0x3F) : 64
14、); val = 6; outindex + 1 = alphabetval & 0x3F; val = 6; outindex + 0 = alphabetval & 0x3F; return new String(out); /* * 将base64编码的数据解码成原始数据 */ static public byte decode(char data) int len = (data.length + 3) / 4) * 3; if (data.length 0 & datadata.length - 1 = =) -len; if (data.length 1 & datadata.le
15、ngth - 2 = =) -len; byte out = new bytelen; int shift = 0; int accum = 0; int index = 0; for (int ix = 0; ix = 0) accum = 8) shift -= 8; outindex+ = (byte) (accum shift) & 0xff); if (index != out.length) throw new Error(miscalculated data length!); return out; KKKKkkkkKKKKKKKKKKKKKKK/*在java中调用sun公司提
16、供的3DES加密解密算法时,需要使用到$JAVA_HOME/jre/lib/目录下如下的4个jar包:jce.jarsecurity/US_export_policy.jarsecurity/local_policy.jarext/sunjce_provider.jar Java运行时会自动加载这些包,因此对于带main函数的应用程序不需要设置到CLASSPATH环境变量中。对于WEB应用,不需要把这些包加到WEB-INF/lib目录下。 以下是java中调用sun公司提供的3DES加密解密算法的样本代码: */package com.aaa.aaa;import java.security.
17、*;import javax.crypto.*;import javax.crypto.spec.SecretKeySpec;public class ThreeDES private static final String Algorithm = DESede; / 定义 加密算法,可用/ DES,DESede,Blowfishprivate static final byte keyBase = 0x11, 0x22, 0x4F, 0x58,(byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB,(byte) 0
18、xDD, 0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40,0x36, (byte) 0xE2 ; / 24个byte的密钥/ keybyte为加密密钥,长度为24字节/ src为被加密的数据缓冲区(源)/* * 加密 */public static byte encryptMode(byte src) try / 生成密钥SecretKey deskey = new SecretKeySpec(keyBase, Algorithm);/ 加密Cipher c1 = Cipher.getInstance(Algorithm);c1.in
19、it(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为加密后的缓冲区publ
20、ic static byte decryptMode(byte src) try / 生成密钥SecretKey deskey = new SecretKeySpec(keyBase, 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.cryp
21、to.NoSuchPaddingException e2) e2.printStackTrace(); catch (java.lang.Exception e3) e3.printStackTrace();return null;/ 转换成十六进制字符串public static String byte2hex(byte b) String hs = ;String stmp = ;for (int n = 0; n b.length; n+) stmp = (java.lang.Integer.toHexString(bn & 0XFF);if (stmp.length() = 1)hs
22、= hs + 0 + stmp;elsehs = hs + stmp;if (n b.length - 1)hs = hs + :;return hs.toUpperCase();public static void main(String args) / 添加新安全算法,如果用JCE就要把它添加进去Security.addProvider(new com.sun.crypto.provider.SunJCE();final byte keyBytes = 0x11, 0x22, 0x4F, 0x58, (byte) 0x88, 0x10,0x40, 0x38, 0x28, 0x25, 0x7
23、9, 0x51, (byte) 0xCB, (byte) 0xDD,0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40, 0x36,(byte) 0xE2 ;/ 24字节的密钥String szSrc = This is a 3DES test. 测试;System.out.println(加密前的字符串: + szSrc);byte encoded = encryptMode(szSrc.getBytes();System.out.println(加密后的字符串: + new String(encoded);byte srcBytes
24、= decryptMode(encoded);System.out.println(解密后的字符串: + (new String(srcBytes);dddddddddddddddddddddpackage com.snailteam.adserver.until;import java.io.UnsupportedEncodingException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;im
25、port javax.crypto.*;import javax.crypto.spec.SecretKeySpec;/* * author :xiaofancn * version :2011-11-11 上午11:02:22 * */ public class Tool3DES private static final String Algorithm = DESede; / 定义 加密算法,可用 private static final int Keysize = 168; /* * 将byte转化成16进制字符串 * param buf * return */ public stati
26、c String parseByte2HexStr(byte buf) StringBuffer sb = new StringBuffer(); for (int i = 0; i buf.length; i+) String hex = Integer.toHexString(bufi & 0xFF); if (hex.length() = 1) hex = 0 + hex; sb.append(hex.toUpperCase(); return sb.toString(); /* * 将16进制字符串转化成byte * param buf * return */ public static byte parseHexStr2Byte(String hexStr) if (hexStr.length() 1) return null; byte result = new bytehexStr.length() / 2; for (int i = 0; i hexStr.length() / 2; i+) int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2