首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java处置url加解密

2012-09-17 
java处理url加解密1.加密MessageDigest md MessageDigest.getInstance(MD5)byte[] byteKeyMd5 md.d

java处理url加解密
1.加密

MessageDigest md = MessageDigest.getInstance("MD5");byte[] byteKeyMd5 = md.digest(encryptKey.getBytes());byte[] byteKey = new byte[24];System.arraycopy(byteKeyMd5, 0, byteKey, 0, 16); System.arraycopy(byteKeyMd5, 0, byteKey, 16, 8); Key deskey = null;DESedeKeySpec spec = new DESedeKeySpec(byteKey);SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");deskey = keyfactory.generateSecret(spec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, deskey);byte[] encryptedBytes = cipher.doFinal(source.getBytes("UTF-8"));BASE64Encoder encoder = new BASE64Encoder();encryptedString = encoder.encode(encryptedBytes);


2.解密
byte[] decodedBytes;BASE64Decoder decoder = new BASE64Decoder();decodedBytes = decoder.decodeBuffer(cipherText);MessageDigest md = MessageDigest.getInstance("MD5");byte[] byteKeyMd5 = md.digest(decryptKey.getBytes());byte[] byteKey = new byte[24];System.arraycopy(byteKeyMd5, 0, byteKey, 0, 16); System.arraycopy(byteKeyMd5, 0, byteKey, 16, 8); Key deskey = null;DESedeKeySpec spec = new DESedeKeySpec(byteKey);SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");deskey = keyfactory.generateSecret(spec);Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");cipher.init(Cipher.DECRYPT_MODE, deskey);byte[] plainTextBytes = cipher.doFinal(decodedBytes);plainText = new String(plainTextBytes);

热点排行