Java处理url加解密


1.加密
Java代码
  1. MessageDigest md = MessageDigest.getInstance("MD5");   
  2. byte[] byteKeyMd5 = md.digest(encryptKey.getBytes());   
  3.   
  4. byte[] byteKey = new byte[24];   
  5. System.arraycopy(byteKeyMd5, 0, byteKey, 016);    
  6. System.arraycopy(byteKeyMd5, 0, byteKey, 168);    
  7.   
  8. Key deskey = null;   
  9. DESedeKeySpec spec = new DESedeKeySpec(byteKey);   
  10. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");   
  11. deskey = keyfactory.generateSecret(spec);   
  12. Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");   
  13. cipher.init(Cipher.ENCRYPT_MODE, deskey);   
  14. byte[] encryptedBytes = cipher.doFinal(source.getBytes("UTF-8"));   
  15.   
  16. BASE64Encoder encoder = new BASE64Encoder();   
  17. encryptedString = encoder.encode(encryptedBytes);  


2.解密
Java代码
  1. byte[] decodedBytes;   
  2. BASE64Decoder decoder = new BASE64Decoder();   
  3. decodedBytes = decoder.decodeBuffer(cipherText);   
  4.                
  5. MessageDigest md = MessageDigest.getInstance("MD5");   
  6. byte[] byteKeyMd5 = md.digest(decryptKey.getBytes());   
  7.                
  8. byte[] byteKey = new byte[24];   
  9. System.arraycopy(byteKeyMd5, 0, byteKey, 016);    
  10. System.arraycopy(byteKeyMd5, 0, byteKey, 168);    
  11.   
  12. Key deskey = null;   
  13. DESedeKeySpec spec = new DESedeKeySpec(byteKey);   
  14. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");   
  15. deskey = keyfactory.generateSecret(spec);   
  16. Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");   
  17. cipher.init(Cipher.DECRYPT_MODE, deskey);   
  18.                
  19. byte[] plainTextBytes = cipher.doFinal(decodedBytes);   
  20. plainText = new String(plainTextBytes);  

相关内容