java php DES 加密解密

10 篇文章 0 订阅
  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. }  




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($textchr($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 方法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. ?>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值