搜尋
首頁後端開發php教程PHP、Java des加密解密实例_PHP教程

PHP、Java des加密解密实例

   这篇文章主要介绍了PHP、Java des加密解密实例,des加密是对称加密中在互联网应用的比较多的一种加密方式,本文分别给出了PHP和JAVA版本的实现代码,需要的朋友可以参考下

  des加密是对称加密中在互联网应用的比较多的一种加密方式,php 通过mcrypt扩展库来支持des加密,要在Php中使用des加密,需要先安装mcrypt扩展库

  下面是加密解密的实例

   代码如下:

  $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.";//需要加密的内容

  echo ($text) . "\n";

  $crypttext =base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));

  echo $crypttext . "\n";//加密后的内容

  echo mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,base64_decode($crypttext),MCRYPT_MODE_ECB,$iv);//解密后的内容

  在AES加密算法中通常会用到MCRYPT_RIJNDAEL_128、MCRYPT_RIJNDAEL_192、MCRYPT_RIJNDAEL_256三种,后面的128、192、256代表的是秘钥(也就是加密的Key)是多少bit的,比如使用的是MCRYPT_RIJNDAEL_128,那么用这个算法加密时秘钥长度就是128bit的,比如 $key = 'fjjda0&9^$$#+*%$fada',是20个字符,那在实际加密的时候只用到前16个字符加密(16*8=128),不足128bit的php中会用'\0'来补齐。

  有的时候做项目对接的时候,可能你用的是Php加密的,而对方用的是java写的,对接的过程中就发现机加密后的内容对方解密不了,这是因为Php跟java在实现这个算法的时候有差别,要想正确加密解密需要两边都做下处理:

  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.'
';

  echo Security::decrypt($value, $key );

  Java:

  代码如下:

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

  }

  }

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/990991.htmlTechArticlePHP、Java des加密解密实例 这篇文章主要介绍了PHP、Java des加密解密实例,des加密是对称加密中在互联网应用的比较多的一种加密方式,本文分别...
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
絕對會話超時有什麼區別?絕對會話超時有什麼區別?May 03, 2025 am 12:21 AM

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

如果會話在服務器上不起作用,您將採取什麼步驟?如果會話在服務器上不起作用,您將採取什麼步驟?May 03, 2025 am 12:19 AM

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()函數的意義是什麼?session_start()函數的意義是什麼?May 03, 2025 am 12:18 AM

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。

為會話cookie設置httponly標誌的重要性是什麼?為會話cookie設置httponly標誌的重要性是什麼?May 03, 2025 am 12:10 AM

設置httponly標誌對會話cookie至關重要,因為它能有效防止XSS攻擊,保護用戶會話信息。具體來說,1)httponly標誌阻止JavaScript訪問cookie,2)在PHP和Flask中可以通過setcookie和make_response設置該標誌,3)儘管不能防範所有攻擊,但應作為整體安全策略的一部分。

PHP會議在網絡開發中解決了什麼問題?PHP會議在網絡開發中解決了什麼問題?May 03, 2025 am 12:02 AM

phpsessions solvathepromblymaintainingStateAcrossMultipleHttpRequestsbyStoringDataTaNthEserVerAndAssociatingItwithaIniquesestionId.1)他們儲存了AtoredAtaserver side,通常是Infilesordatabases,InseasessessionIdStoreDistordStoredStoredStoredStoredStoredStoredStoreDoreToreTeReTrestaa.2)

可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具