3DESPHP is compatible with. NET, and some garbled characters occur after decryption.

Source: Internet
Author: User
Tags mcrypt string back kohana
Php for 89407737, the result after 3DES is 04cd1b1572ff6e02e70205bb633b6d70. What is the result after I decrypt c? 6 ?? C89407737, the first part of the Garbled text is correct. Several methods have been changed, and the first part is garbled. The following information is correct. C # TripleDESCryptoServiceProviderDESnewTripleDESCryp

Php for 89407737, the result after 3DES is 04cd1b1572ff6e02e70205bb633b6d70. What is the result after I decrypt c? 6 ?? C89407737, the first part of the Garbled text is correct. Several methods have been changed, and the first part is garbled. The following information is correct. C # TripleDESCryptoServiceProvider DES = new TripleDESCryp

Php returns "04cd1b1572ff6e02e70205bb633b6d70" for "89407737" and "3DES ". After I decrypt c #, the result is "? 6 ?? C89407737 ", the first part of the Garbled text is correct. Several methods have been changed, and the first part is garbled. The following information is correct.

C #

TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider ();
DES. Key = Encoding. UTF8.GetBytes (txtKey );
DES. IV = Encoding. UTF8.GetBytes (txtIV );
DES. Mode = CipherMode. CFB;
DES. Padding = PaddingMode. None;
ICryptoTransform tf = DES. CreateDecryptor ();

String hexString = Value;
Byte [] bytes = new byte [hexString. Length/2];
For (int I = 0; I <bytes. Length; I ++)
{
Bytes [I] = byte. Parse (hexString. Substring (I * 2, 2 ),
System. Globalization. NumberStyles. HexNumber );
}

Return UTF8Encoding. UTF8.GetString (tf. TransformFinalBlock (bytes, 0, 16 ));

Php:

/**
* The Encrypt library provides two-way encryption of text and binary strings
* Using the [Mcrypt] (http://php.net/mcrypt) extension, which consists of three
* Parts: the key, the cipher, and the mode.
*
* The Key
*: A secret passphrase that is used for encoding and decoding
*
* The Cipher
*: A [cipher] (http://php.net/mcrypt.ciphers) determines how the encryption
* Is mathematically calculated. By default, the "rijndael-128" cipher
* Is used. This is commonly known as "AES-128" and is an industry standard.
*
* The Mode
*: The [mode] (http://php.net/mcrypt.constants) determines how the encrypted
* Data is written in binary form. By default, the "nofb" mode is used,
* Which produces short output with high entropy.
*
* @ Package Kohana
* @ Category Security
* @ Author Kohana Team
* @ Copyright (c) 2007-2012 Kohana Team
* @ License http://kohanaframework.org/license
*/
Class Encrypt {

/**
* @ Var string default instance name
*/
Public static $ default = 'default ';

/**
* @ Var array Encrypt class instances
*/
Public static $ instances = array ();

/**
* @ Var string OS-dependent RAND type to use
*/
Protected static $ _ rand;

/**
* Returns a singleton instance of Encrypt. An encryption key must be
* Provided in your "encrypt" configuration file.
*
* $ Encrypt = Encrypt: instance ();
*
* @ Param string $ name configuration group name
* @ Return Encrypt
*/
Public static function instance ($ name = NULL)
{
If ($ name = NULL)
{
// Use the default instance name
$ Name = Encrypt: $ default;
}

If (! Isset (Encrypt: $ instances [$ name])
{
// Load the configuration data
$ Config = Kohana: $ config-> load ('enabled')-> $ name;

If (! Isset ($ config ['key'])
{
// No default encryption key is provided!
Throw new Kohana_Exception ('no encryption key is defined in the encryption configuration group: group ',
Array (': group' => $ name ));
}

If (! Isset ($ config ['Mode'])
{
// Add the default mode
$ Config ['Mode'] = MCRYPT_MODE_NOFB;
}

If (! Isset ($ config ['cer er'])
{
// Add the default cipher
$ Config ['cipher '] = MCRYPT_RIJNDAEL_128;
}

// Create a new instance
Encrypt: $ instances [$ name] = new Encrypt ($ config ['key'], $ config ['Mode'], $ config ['cipher']);
}

Return Encrypt: $ instances [$ name];
}

/**
* Creates a new mcrypt wrapper.
*
* @ Param string $ key encryption key
* @ Param string $ mode mcrypt mode
* @ Param string $ cipher mcrypt cipher
*/
Public function _ construct ($ key, $ mode, $ cipher)
{
// Find the max length of the key, based on cipher and mode
$ Size = mcrypt_get_key_size ($ cipher, $ mode );

If (isset ($ key [$ size])
{
// Shorten the key to the maximum size
$ Key = substr ($ key, 0, $ size );
}

// Store the key, mode, and cipher
$ This-> _ key = $ key;
$ This-> _ mode = $ mode;
$ This-> _ cipher = $ cipher;

// Store the IV size
$ This-> _ iv_size = mcrypt_get_iv_size ($ this-> _ cipher, $ this-> _ mode );
}

/**
* Encrypts a string and returns an encrypted string that can be decoded.
*
* $ Data = $ encrypt-> encode ($ data );
*
* The encrypted binary data is encoded using [base64] (http://php.net/base64_encode)
* To convert it to a string. This string can be stored in a database,
* Displayed, and passed using most other means without resume uption.
*
* @ Param string $ data to be encrypted
* @ Return string
*/
Public function encode ($ data)
{
// Set the rand type if it has not already been set
If (Encrypt: $ _ rand = NULL)
{
If (Kohana: $ is_windows)
{
// Windows only supports the system random number generator
Encrypt: $ _ rand = MCRYPT_RAND;
}
Else
{
If (defined ('mcrypt _ DEV_URANDOM '))
{
// Use/dev/urandom
Encrypt: $ _ rand = MCRYPT_DEV_URANDOM;
}
Elseif (defined ('mcrypt _ DEV_RANDOM '))
{
// Use/dev/random
Encrypt: $ _ rand = MCRYPT_DEV_RANDOM;
}
Else
{
// Use the system random number generator
Encrypt: $ _ rand = MCRYPT_RAND;
}
}
}

If (Encrypt ::$ _ rand === MCRYPT_RAND)
{
// The system random number generator must always be seeded each
// Time it is used, or it will not produce true random results
Mt_srand ();
}

// Create a random initialization vector of the proper size for the current cipher
$ Iv = mcrypt_create_iv ($ this-> _ iv_size, Encrypt ::$ _ rand );

// Encrypt the data using the configured options and generated iv
$ Data = mcrypt_encrypt ($ this-> _ cipher, $ this-> _ key, $ data, $ this-> _ mode, $ iv );

// Use base64 encoding to convert to a string
// Return base64_encode ($ iv. $ data );
Return str2hex ($ iv. $ data );
}

/**
* Decrypts an encoded string back to its original value.
*
* $ Data = $ encrypt-> decode ($ data );
*
* @ Param string $ data encoded string to be decrypted
* @ Return FALSE if decryption fails
* @ Return string
*/
Public function decode ($ data)
{
$ Data = base64_encode (hex2str ($ data ));
// Convert the data back to binary
$ Data = base64_decode ($ data, TRUE );

If (! $ Data)
{
// Invalid base64 data
Return FALSE;
}

// Extract the initialization vector from the data
$ Iv = substr ($ data, 0, $ this-> _ iv_size );

If ($ this-> _ iv_size! = Strlen ($ iv ))
{
// The iv is not the expected size
Return FALSE;
}

// Remove the iv from the data
$ Data = substr ($ data, $ this-> _ iv_size );

// Return the decrypted data, trimming the \ 0 padding bytes from the end of the data
Return rtrim (mcrypt_decrypt ($ this-> _ cipher, $ this-> _ key, $ data, $ this-> _ mode, $ iv), "\ 0 ");
}

}
/**
* Hexadecimal encoding to string
*
* @ Param string hexadecimal Encoding
*
* @ Return string
*/
Function hex2str ($ s)
{
$ R = "";
For ($ I = 0; $ I {
$ R. = chr (hexdec ('0x '. $ s {$ I}. $ s {$ I + 1 }));
}
Return $ r;
}

/**
* String to hexadecimal Encoding
*
* @ Param string
*
* @ Return string hexadecimal Encoding
*/
Function str2Hex ($ s)
{
$ R = "";
$ Hexes = array ("0", "1", "2", "3", "4", "5", "6", "7 ", "8", "9", "a", "B", "c", "d", "e", "f ");
For ($ I = 0; $ I {
$ R. = ($ hexes [(ord ($ s {$ I})> 4)]. $ hexes [(ord ($ s {$ I}) & 0xf)]);
}
Return $ r;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.