设为首页 收藏本站
查看: 529|回复: 0

[经验分享] java php DES 加密解密[转摘]

[复制链接]

尚未签到

发表于 2017-4-2 08:36:23 | 显示全部楼层 |阅读模式
Java代码 DSC0000.gif




  • 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)));   

  •      }   

  • }  

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代码






  • <?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
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代码






  • <?php   

  •     include('DES.php');   

  •   

  •     $des = new Crypt_DES();   

  •   

  •     $des->setKey('abcdefgh');   

  •     $plaintext = 'a';   

  •     $jiami = base64_encode($des->encrypt($plaintext));   

  •     echo "Encode:".$jiami."<br/>";   

  •     echo "Decode:".$des->decrypt(base64_decode($jiami));   

  • ?>  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-358919-1-1.html 上篇帖子: php_encode 转换出乱码的解决 下篇帖子: PHP变量标识符的一些规则
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表