我现在这边需要跟.NET那边进行数据交互,数据是需要进行加密处理的,但是现在的问题是,我用PHP的加密处理之后和.net加密处理之后的数据不相等,他们那边的代码是没有问题的,这个是可以确认的,那么我这边的加密就有问题了,现在具体的问题不是很清楚,求各位大神指教,下面是我这边的代码:
<code>/** </code>
* 3DES加密处理类
*
* @author fbbin fbbin@gmail.com
* @version 1.0 beta
* @created 2014/01/07
* @logs
*/
class Crypt3Des {
<code>/** * 加密需要的key * @var intval */ protected static $key = ""; /** * 静态构造初始化 * * @return Crypt3Des */ public static function instance($key = '') { self::$key = $key; return new static; } /** * 对字符串进行加密 * * @return string */ public function encrypt($input) { $size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB); $input = $this->paddingPKCS7($input); $key = str_pad(self::$key, 24, '0'); $td = mcrypt_module_open(MCRYPT_3DES, '', 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); return base64_encode($data); } /** * 对字符串进行解密 * * @return string */ public function decrypt($encrypted) { $encrypted = base64_decode($encrypted); $key = str_pad(self::$key, 24, '0'); $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, ''); $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); return $this->pkcs5Unpad($decrypted); } /** * 对字符串按照规则打包 * * @return string */ public function paddingPKCS7($data) { $block_size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB); $padding_char = $block_size - (strlen($data) % $block_size); $data .= str_repeat(chr($padding_char), $padding_char); return $data; } /** * 对字符串按照规则解包 * * @return string */ public function pkcs5Unpad($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); } </code>
}
使用方式:
<code>echo Crypt3Des::instance(base64_encode('FD3BCD75678B4BABB6E8948149CA6C91'))->encrypt('1'); </code>
我这边得出的结果是:mzbVH6DmUXI=, 而正确的值应该是:sztImTSd0o;
求指教,万分感谢。
回复内容:
我现在这边需要跟.NET那边进行数据交互,数据是需要进行加密处理的,但是现在的问题是,我用PHP的加密处理之后和.net加密处理之后的数据不相等,他们那边的代码是没有问题的,这个是可以确认的,那么我这边的加密就有问题了,现在具体的问题不是很清楚,求各位大神指教,下面是我这边的代码:
<code>/** </code>
* 3DES加密处理类
*
* @author fbbin fbbin@gmail.com
* @version 1.0 beta
* @created 2014/01/07
* @logs
*/
class Crypt3Des {
<code>/** * 加密需要的key * @var intval */ protected static $key = ""; /** * 静态构造初始化 * * @return Crypt3Des */ public static function instance($key = '') { self::$key = $key; return new static; } /** * 对字符串进行加密 * * @return string */ public function encrypt($input) { $size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB); $input = $this->paddingPKCS7($input); $key = str_pad(self::$key, 24, '0'); $td = mcrypt_module_open(MCRYPT_3DES, '', 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); return base64_encode($data); } /** * 对字符串进行解密 * * @return string */ public function decrypt($encrypted) { $encrypted = base64_decode($encrypted); $key = str_pad(self::$key, 24, '0'); $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, ''); $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); return $this->pkcs5Unpad($decrypted); } /** * 对字符串按照规则打包 * * @return string */ public function paddingPKCS7($data) { $block_size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB); $padding_char = $block_size - (strlen($data) % $block_size); $data .= str_repeat(chr($padding_char), $padding_char); return $data; } /** * 对字符串按照规则解包 * * @return string */ public function pkcs5Unpad($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); } </code>
}
使用方式:
<code>echo Crypt3Des::instance(base64_encode('FD3BCD75678B4BABB6E8948149CA6C91'))->encrypt('1'); </code>
我这边得出的结果是:mzbVH6DmUXI=, 而正确的值应该是:sztImTSd0o;
求指教,万分感谢。

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

The article discusses PHP Data Objects (PDO), an extension for database access in PHP. It highlights PDO's role in enhancing security through prepared statements and its benefits over MySQLi, including database abstraction and better error handling.

Article discusses creating and securing PHP APIs, detailing steps from endpoint definition to performance optimization using frameworks like Laravel and best security practices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
