溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP和Java des的加密解密怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2023-05-04 10:49:31 來源:億速云 閱讀:67 作者:zzz 欄目:編程語言

本篇內(nèi)容介紹了“PHP和Java des的加密解密怎么實(shí)現(xiàn)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

des加密是對(duì)稱加密中在互聯(lián)網(wǎng)應(yīng)用的比較多的一種加密方式,php 通過mcrypt擴(kuò)展庫(kù)來支持des加密,要在Php中使用des加密,需要先安裝mcrypt擴(kuò)展庫(kù)

下面是加密解密的實(shí)例

復(fù)制代碼 代碼如下:

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = "This is a very secret key";//密鑰
$text = "Meet me at 11 o'clock behind the monument.";//需要加密的內(nèi)容
echo ($text) . "\n";  
$crypttext =base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));
echo $crypttext . "\n";//加密后的內(nèi)容  
echo mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,base64_decode($crypttext),MCRYPT_MODE_ECB,$iv);//解密后的內(nèi)容

在AES加密算法中通常會(huì)用到MCRYPT_RIJNDAEL_128、MCRYPT_RIJNDAEL_192、MCRYPT_RIJNDAEL_256三種,后面的128、192、256代表的是秘鑰(也就是加密的Key)是多少bit的,比如使用的是MCRYPT_RIJNDAEL_128,那么用這個(gè)算法加密時(shí)秘鑰長(zhǎng)度就是128bit的,比如 $key = 'fjjda0&9^$$#+*%$fada',是20個(gè)字符,那在實(shí)際加密的時(shí)候只用到前16個(gè)字符加密(16*8=128),不足128bit的php中會(huì)用'\0'來補(bǔ)齊。

有的時(shí)候做項(xiàng)目對(duì)接的時(shí)候,可能你用的是Php加密的,而對(duì)方用的是java寫的,對(duì)接的過程中就發(fā)現(xiàn)機(jī)加密后的內(nèi)容對(duì)方解密不了,這是因?yàn)镻hp跟java在實(shí)現(xiàn)這個(gè)算法的時(shí)候有差別,要想正確加密解密需要兩邊都做下處理:

PHP:

復(fù)制代碼 代碼如下:

<?php
class Security {
   public static function encrypt($input, $key) {
       $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
       $input = Security::pkcs5_pad($input, $size);
       $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_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;
   }
 
   private static function pkcs5_pad ($text, $blocksize) {
       $pad = $blocksize - (strlen($text) % $blocksize);
       return $text . str_repeat(chr($pad), $pad);
   }
 
   public static function decrypt($sStr, $sKey) {
       $decrypted= mcrypt_decrypt(
       MCRYPT_RIJNDAEL_128,
       $sKey,
       base64_decode($sStr),
       MCRYPT_MODE_ECB
   );
 
       $dec_s = strlen($decrypted);
       $padding = ord($decrypted[$dec_s-1]);
       $decrypted = substr($decrypted, 0, -$padding);
       return $decrypted;
   }    
}
 
$key = "1234567891234567";
$data = "example";  
$value = Security::encrypt($data , $key );
echo $value.'<br/>';
echo Security::decrypt($value, $key );

Java:

復(fù)制代碼 代碼如下:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
import org.apache.commons.codec.binary.Base64;
 
public class Security {
   public static String encrypt(String input, String key){
       byte[] crypted = null;
       try{
           SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
           Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
           cipher.init(Cipher.ENCRYPT_MODE, skey);
           crypted = cipher.doFinal(input.getBytes());
       }catch(Exception e){
       System.out.println(e.toString());
   }
   return new String(Base64.encodeBase64(crypted));
}
 
   public static String decrypt(String input, String key){
       byte[] output = null;
       try{
           SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
           Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
           cipher.init(Cipher.DECRYPT_MODE, skey);
           output = cipher.doFinal(Base64.decodeBase64(input));
           }catch(Exception e){
           System.out.println(e.toString());
       }
       return new String(output);
   }
 
   public static void main(String[] args) {
       String key = "1234567891234567";
       String data = "example";
       
       System.out.println(Security.encrypt(data, key));
       
       System.out.println(Security.decrypt(Security.encrypt(data, key), key));
       
           
   }    
}

“PHP和Java des的加密解密怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI