·您当前的位置:首页 > 技术教程 > JavaScript >

[JS]java加密封装和php加密封装

时间:2014-09-23 17:15酷播
[JS]java加密封装和php加密封装

java代码

  1. import java.io.IOException;   
  2. import java.security.SecureRandom;   
  3. import javax.crypto.Cipher;   
  4. import javax.crypto.SecretKey;   
  5. import javax.crypto.SecretKeyFactory;   
  6. import javax.crypto.spec.DESKeySpec;   
  7. import sun.misc.BASE64Decoder;   
  8. import sun.misc.BASE64Encoder;   
  9. public class DES {   
  10.    
  11.     private byte[] desKey;   
  12.    
  13.     public DES(String desKey) {   
  14.         this.desKey = desKey.getBytes();   
  15.     }   
  16.    
  17.     public byte[] desEncrypt(byte[] plainText) throws Exception {   
  18.         SecureRandom sr = new SecureRandom();   
  19.         byte rawKeyData[] = desKey;   
  20.         DESKeySpec dks = new DESKeySpec(rawKeyData);   
  21.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");   
  22.         SecretKey key = keyFactory.generateSecret(dks);   
  23.         Cipher cipher = Cipher.getInstance("DES");   
  24.         cipher.init(Cipher.ENCRYPT_MODE, key, sr);   
  25.         byte data[] = plainText;   
  26.         byte encryptedData[] = cipher.doFinal(data);   
  27.         return encryptedData;   
  28.     }   
  29.    
  30.     public byte[] desDecrypt(byte[] encryptText) throws Exception {   
  31.         SecureRandom sr = new SecureRandom();   
  32.         byte rawKeyData[] = desKey;   
  33.         DESKeySpec dks = new DESKeySpec(rawKeyData);   
  34.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");   
  35.         SecretKey key = keyFactory.generateSecret(dks);   
  36.         Cipher cipher = Cipher.getInstance("DES");   
  37.         cipher.init(Cipher.DECRYPT_MODE, key, sr);   
  38.         byte encryptedData[] = encryptText;   
  39.         byte decryptedData[] = cipher.doFinal(encryptedData);   
  40.         return decryptedData;   
  41.     }   
  42.    
  43.     public String encrypt(String input) throws Exception {   
  44.         return base64Encode(desEncrypt(input.getBytes()));   
  45.     }   
  46.    
  47.     public String decrypt(String input) throws Exception {   
  48.         byte[] result = base64Decode(input);   
  49.         return new String(desDecrypt(result));   
  50.     }   
  51.    
  52.     public static String base64Encode(byte[] s) {   
  53.         if (s == null)   
  54.             return null;   
  55.         BASE64Encoder b = new sun.misc.BASE64Encoder();   
  56.         return b.encode(s);   
  57.     }   
  58.    
  59.     public static byte[] base64Decode(String s) throws IOException {   
  60.         if (s == null)   
  61.             return null;   
  62.         BASE64Decoder decoder = new BASE64Decoder();   
  63.         byte[] b = decoder.decodeBuffer(s);   
  64.         return b;   
  65.     }   
  66.    
  67.     public static void main(String[] args) throws Exception {   
  68.         String key = "abcdefgh";   
  69.         String input = "a";   
  70.         DES crypt = new DES(key);   
  71.         System.out.println("Encode:" + crypt.encrypt(input));   
  72.         System.out.println("Decode:" + crypt.decrypt(crypt.encrypt(input)));   
  73.     }   
  74.  
  75.  
  76. }   

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器