解密 - 请问这个转换的php7加密方法,为什么和c#的结果不一样呢?
谢谢大家.
如下,有c#的加解密方法,我自己写了个php7的实现.但是加解密的结果和c#的完全不一样,请问哪里错了?本人菜鸟,多谢各位.
c
public static string Decrypt(string Text, string sKey) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); int num = Text.Length / 2; byte[] buffer = new byte[num]; for (int i = 0; i < num; i++) { int num3 = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10); buffer[i] = (byte)num3; } provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); return Encoding.Default.GetString(stream.ToArray()); } public static string Encrypt(string Text, string sKey) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); byte[] bytes = Encoding.Default.GetBytes(Text); provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); MemoryStream stream = new MemoryStream(); string x = provider.Mode.ToString(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in stream.ToArray()) { builder.AppendFormat("{0:X2}", num); } return builder.ToString(); }
php7
public function encrypt($input, $key) { $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC); $input = $this->pkcs5_pad($input, $size); $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); $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 function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } public function decrypt($sStr, $sKey) { $decrypted = mcrypt_decrypt( MCRYPT_DES, $sKey, base64_decode($sStr), MCRYPT_MODE_CBC ); $dec_s = strlen($decrypted); $padding = ord($decrypted[$dec_s - 1]); $decrypted = substr($decrypted, 0, -$padding); return $decrypted; }
回复内容:
谢谢大家.
如下,有c#的加解密方法,我自己写了个php7的实现.但是加解密的结果和c#的完全不一样,请问哪里错了?本人菜鸟,多谢各位.
c
public static string Decrypt(string Text, string sKey) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); int num = Text.Length / 2; byte[] buffer = new byte[num]; for (int i = 0; i < num; i++) { int num3 = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10); buffer[i] = (byte)num3; } provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); return Encoding.Default.GetString(stream.ToArray()); } public static string Encrypt(string Text, string sKey) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); byte[] bytes = Encoding.Default.GetBytes(Text); provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); MemoryStream stream = new MemoryStream(); string x = provider.Mode.ToString(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in stream.ToArray()) { builder.AppendFormat("{0:X2}", num); } return builder.ToString(); }
php7
public function encrypt($input, $key) { $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC); $input = $this->pkcs5_pad($input, $size); $td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); $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 function pkcs5_pad($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); } public function decrypt($sStr, $sKey) { $decrypted = mcrypt_decrypt( MCRYPT_DES, $sKey, base64_decode($sStr), MCRYPT_MODE_CBC ); $dec_s = strlen($decrypted); $padding = ord($decrypted[$dec_s - 1]); $decrypted = substr($decrypted, 0, -$padding); return $decrypted; }
使用PHP自带的OpenSSL不好了?

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Use most text editors to open XML files; if you need a more intuitive tree display, you can use an XML editor, such as Oxygen XML Editor or XMLSpy; if you process XML data in a program, you need to use a programming language (such as Python) and XML libraries (such as xml.etree.ElementTree) to parse.

There are three ways to convert XML to Word: use Microsoft Word, use an XML converter, or use a programming language.
