java php DES 加密解密[转摘]

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. }  
import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DES {

 private byte[] desKey;

 public DES(String desKey) {
  this.desKey = desKey.getBytes();
 }

 public byte[] desEncrypt(byte[] plainText) throws Exception {
  SecureRandom sr = new SecureRandom();
  byte rawKeyData[] = desKey;
  DESKeySpec dks = new DESKeySpec(rawKeyData);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  SecretKey key = keyFactory.generateSecret(dks);
  Cipher cipher = Cipher.getInstance("DES");
  cipher.init(Cipher.ENCRYPT_MODE, key, sr);
  byte data[] = plainText;
  byte encryptedData[] = cipher.doFinal(data);
  return encryptedData;
 }

 public byte[] desDecrypt(byte[] encryptText) throws Exception {
  SecureRandom sr = new SecureRandom();
  byte rawKeyData[] = desKey;
  DESKeySpec dks = new DESKeySpec(rawKeyData);
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  SecretKey key = keyFactory.generateSecret(dks);
  Cipher cipher = Cipher.getInstance("DES");
  cipher.init(Cipher.DECRYPT_MODE, key, sr);
  byte encryptedData[] = encryptText;
  byte decryptedData[] = cipher.doFinal(encryptedData);
  return decryptedData;
 }

 public String encrypt(String input) throws Exception {
  return base64Encode(desEncrypt(input.getBytes()));
 }

 public String decrypt(String input) throws Exception {
  byte[] result = base64Decode(input);
  return new String(desDecrypt(result));
 }

 public static String base64Encode(byte[] s) {
  if (s == null)
   return null;
  BASE64Encoder b = new sun.misc.BASE64Encoder();
  return b.encode(s);
 }

 public static byte[] base64Decode(String s) throws IOException {
  if (s == null)
   return null;
  BASE64Decoder decoder = new BASE64Decoder();
  byte[] b = decoder.decodeBuffer(s);
  return b;
 }

 public static void main(String[] args) throws Exception {
  String key = "abcdefgh";
  String input = "a";
  DES crypt = new DES(key);
  System.out.println("Encode:" + crypt.encrypt(input));
  System.out.println("Decode:" + crypt.decrypt(crypt.encrypt(input)));
 }
}





php 方法一

Php代码 复制代码
  1. <?php   
  2. class DES1 {       
  3.     var $key;          
  4.     function     DES1($key) {           
  5.         $this->key = $key;          
  6.      }          
  7.     function encrypt($input) {         
  8.         $size = mcrypt_get_block_size('des', 'ecb');           
  9.         $input = $this->pkcs5_pad($input, $size);           
  10.         $key = $this->key;          
  11.         $td = mcrypt_module_open('des', '', 'ecb', '');        
  12.         $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);       
  13.          @mcrypt_generic_init($td, $key, $iv);          
  14.         $data = mcrypt_generic($td, $input);           
  15.          mcrypt_generic_deinit($td);       
  16.          mcrypt_module_close($td);          
  17.         $data = base64_encode($data);          
  18.         return $data;      
  19.      }          
  20.     function decrypt($encrypted) {         
  21.         $encrypted = base64_decode($encrypted);        
  22.         $key =$this->key;           
  23.         $td = mcrypt_module_open('des','','ecb','');   
  24.         //使用MCRYPT_DES算法,cbc模式                 
  25.         $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);             
  26.         $ks = mcrypt_enc_get_key_size($td);                
  27.          @mcrypt_generic_init($td, $key, $iv);          
  28.         //初始处理                 
  29.         $decrypted = mdecrypt_generic($td, $encrypted);          
  30.         //解密               
  31.          mcrypt_generic_deinit($td);          
  32.         //结束             
  33.          mcrypt_module_close($td);                  
  34.         $y=$this->pkcs5_unpad($decrypted);           
  35.         return $y;     
  36.      }          
  37.     function pkcs5_pad ($text, $blocksize) {           
  38.         $pad = $blocksize - (strlen($text) % $blocksize);          
  39.         return $text . str_repeat(chr($pad), $pad);   
  40.      }      
  41.     function pkcs5_unpad($text) {          
  42.         $pad = ord($text{strlen($text)-1});        
  43.         if ($pad > strlen($text))               
  44.             return false;          
  45.         if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)                
  46.             return false;          
  47.         return substr($text, 0, -1 * $pad);   
  48.      }   
  49. }   
  50.         $key = "abcdefgh";   
  51.         $input = "a";   
  52.         $crypt = new DES1($key);   
  53.         echo "Encode:".$crypt->encrypt($input)."<br/>";   
  54.         echo "Decode:".$crypt->decrypt($crypt->encrypt($input));   
  55. ?>   
<?php
class DES1 { 
 var $key;  
 function  DES1($key) {  
  $this->key = $key;  
 }  
 function encrypt($input) {  
  $size = mcrypt_get_block_size('des', 'ecb');     
  $input = $this->pkcs5_pad($input, $size);     
  $key = $this->key;     
  $td = mcrypt_module_open('des', '', 'ecb', '');     
  $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);    
  @mcrypt_generic_init($td, $key, $iv);     
  $data = mcrypt_generic($td, $input);     
  mcrypt_generic_deinit($td);    
  mcrypt_module_close($td);     
  $data = base64_encode($data);     
  return $data; 
 }  
 function decrypt($encrypted) {  
  $encrypted = base64_decode($encrypted);     
  $key =$this->key;     
  $td = mcrypt_module_open('des','','ecb',''); 
  //使用MCRYPT_DES算法,cbc模式           
  $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);        
  $ks = mcrypt_enc_get_key_size($td);          
  @mcrypt_generic_init($td, $key, $iv);       
  //初始处理            
  $decrypted = mdecrypt_generic($td, $encrypted);       
  //解密            
  mcrypt_generic_deinit($td);       
  //结束          
  mcrypt_module_close($td);               
  $y=$this->pkcs5_unpad($decrypted);        
  return $y; 
 }  
 function pkcs5_pad ($text, $blocksize) {     
  $pad = $blocksize - (strlen($text) % $blocksize);     
  return $text . str_repeat(chr($pad), $pad); 
 }  
 function pkcs5_unpad($text) {  
  $pad = ord($text{strlen($text)-1});  
  if ($pad > strlen($text))    
   return false;  
  if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)    
   return false;     
  return substr($text, 0, -1 * $pad); 
 }
} 
  $key = "abcdefgh";
  $input = "a";
  $crypt = new DES1($key);
  echo "Encode:".$crypt->encrypt($input)."<br/>";
  echo "Decode:".$crypt->decrypt($crypt->encrypt($input));
?>


php 方法2
使用phpseclib中的DES

Php代码 复制代码
  1. <?php   
  2.     include('DES.php');   
  3.   
  4.     $des = new Crypt_DES();   
  5.   
  6.     $des->setKey('abcdefgh');   
  7.     $plaintext = 'a';   
  8.     $jiami = base64_encode($des->encrypt($plaintext));   
  9.     echo "Encode:".$jiami."<br/>";   
  10.     echo "Decode:".$des->decrypt(base64_decode($jiami));   
  11. ?>  

 

你可能感兴趣的:(java,PHP,算法,Security,sun)