Java和PHP加解密

PHP代码

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

 

结果:

密文:Rm+8trB4CBQ=
明文:中国

 

Java代码

  1 package com.util;
  2 import java.security.Key;
  3 import java.security.SecureRandom;
  4 
  5 import javax.crypto.Cipher;
  6 import javax.crypto.SecretKeyFactory;
  7 import javax.crypto.spec.DESKeySpec;
  8 
  9 import org.apache.commons.codec.binary.Base64;
 10 
 11 import com.util.DesEncrypt;
 12 
 13 /**
 14  * 加密解密
 15  * @author bian
 16  * 2015 上午11:13:36
 17  */
 18 public class DesEncrypt {
 19     Key key;
 20     public DesEncrypt(String str) {
 21         try{
 22             String key = str;
 23             setKey(key);// 生成密匙
 24         }catch(Exception e){
 25              
 26         }
 27     }
 28 
 29     public DesEncrypt() {
 30         setKey("heimazhifuqw233344");
 31     }
 32 
 33     /**
 34      * 根据参数生成KEY
 35      */
 36     public void setKey(String strKey) {
 37         try {
 38             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
 39             this.key  = keyFactory.generateSecret(new DESKeySpec(strKey.getBytes("UTF8")));
 40         } catch (Exception e) {
 41             throw new RuntimeException(
 42                     "Error initializing SqlMap class. Cause: " + e);
 43         }
 44     }
 45 
 46     
 47     /**
 48      * 加密String明文输入,String密文输出
 49      */
 50     public String encrypt(String strMing) {
 51         byte[] byteMi = null;
 52         byte[] byteMing = null;
 53         String strMi = "";
 54         //BASE64Encoder base64en = new BASE64Encoder();
 55         try {
 56             byteMing = strMing.getBytes("UTF8");
 57             byteMi = this.getEncCode(byteMing);
 58             strMi = new String(Base64.encodeBase64(byteMi), "UTF8");
 59         } catch (Exception e) {
 60             throw new RuntimeException(
 61                     "Error initializing SqlMap class. Cause: " + e);
 62         } finally {
 63             //base64en = null;
 64             byteMing = null;
 65             byteMi = null;
 66         }
 67         return strMi;
 68     }
 69 
 70     /**
 71      * 解密 以String密文输入,String明文输出
 72      * 
 73      * @param strMi
 74      * @return
 75      */
 76     public String decrypt(String strMi) {
 77         ///BASE64Decoder base64De = new BASE64Decoder();
 78         byte[] byteMing = null;
 79         byte[] byteMi = null;
 80         String strMing = "";
 81         try {
 82             byteMi = Base64.decodeBase64(strMi);
 83             byteMing = this.getDesCode(byteMi);
 84             strMing = new String(byteMing, "UTF8");
 85         } catch (Exception e) {
 86             throw new RuntimeException(
 87                     "Error initializing SqlMap class. Cause: " + e);
 88         } finally {
 89             //base64De = null;
 90             byteMing = null;
 91             byteMi = null;
 92         }
 93         return strMing;
 94     }
 95 
 96     /**
 97      * 加密以byte[]明文输入,byte[]密文输出
 98      * 
 99      * @param byteS
100      * @return
101      */
102     private byte[] getEncCode(byte[] byteS) {
103         byte[] byteFina = null;
104         Cipher cipher;
105         try {
106             cipher = Cipher.getInstance("DES");
107             cipher.init(Cipher.ENCRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));
108             byteFina = cipher.doFinal(byteS);
109         } catch (Exception e) {
110             throw new RuntimeException(
111                     "Error initializing SqlMap class. Cause: " + e);
112         } finally {
113             cipher = null;
114         }
115         return byteFina;
116     }
117 
118     /**
119      * 解密以byte[]密文输入,以byte[]明文输出
120      * 
121      * @param byteD
122      * @return
123      */
124     private byte[] getDesCode(byte[] byteD) {
125         Cipher cipher;
126         byte[] byteFina = null;
127         try {
128             cipher = Cipher.getInstance("DES");
129             cipher.init(Cipher.DECRYPT_MODE, key,SecureRandom.getInstance("SHA1PRNG"));
130             byteFina = cipher.doFinal(byteD);
131         } catch (Exception e) {
132             throw new RuntimeException(
133                     "Error initializing SqlMap class. Cause: " + e);
134         } finally {
135             cipher = null;
136         }
137         return byteFina;
138     }
139 
140     
141 
142     public static void main(String args[])  {
143         String key = "123456789012345678901234567890123456";//36位
144         String str = "中国";
145         DesEncrypt des = new DesEncrypt(key);    
146         
147         // DES加密
148         String mStr =   des.encrypt(str);
149         // DES解密
150         String deStr = des.decrypt(mStr);
151         
152         System.out.println("密文:" + mStr);
153         System.out.println("明文:" + deStr);
154     }
155 }

 

结果:

密文:Rm+8trB4CBQ=
明文:中国

 

    原文作者:Good__Good
    原文地址: https://www.cnblogs.com/lhat/p/7200205.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞