Encryption 在php中加密ASP(Rijndael)和Descrypt
我有一个ASP的代码,但我不知道在php加密和比较密码加密 我的图书馆是:Encryption 在php中加密ASP(Rijndael)和Descrypt,encryption,rijndael,Encryption,Rijndael,我有一个ASP的代码,但我不知道在php加密和比较密码加密 我的图书馆是: using System.Security.Cryptography; // Encrypt a string into a string using a password // Uses Encrypt(byte[], byte[], byte[]) public string Encrypt(string clearText, string Password) {
using System.Security.Cryptography;
// Encrypt a string into a string using a password
// Uses Encrypt(byte[], byte[], byte[])
public string Encrypt(string clearText, string Password)
{
// First we need to turn the input string into a byte array.
byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
// Then, we need to turn the password into Key and IV
// We are using salt to make it harder to guess our key using a dictionary attack -
// trying to guess a password by enumerating all possible words.
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
// Now get the key/IV and do the encryption using the function that accepts byte arrays.
// Using PasswordDeriveBytes object we are first getting 32 bytes for the Key
// (the default Rijndael key length is 256bit = 32bytes) and then 16 bytes for the IV.
// IV should always be the block size, which is by default 16 bytes (128 bit) for Rijndael.
// If you are using DES/TripleDES/RC2 the block size is 8 bytes and so should be the IV size.
// You can also read KeySize/BlockSize properties off the algorithm to find out the sizes.
byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
// Now we need to turn the resulting byte array into a string.
// A common mistake would be to use an Encoding class for that. It does not work
// because not all byte values can be represented by characters.
// We are going to be using Base64 encoding that is designed exactly for what we are
// trying to do.
return Convert.ToBase64String(encryptedData);
}
php代码是:
function fnEncrypt($Word, $key){
$iv = mcrypt_create_iv( mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND );
if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $Word . md5($Word), MCRYPT_MODE_CBC, $iv));
return $iv_base64 . $encrypted;
}
或者我尝试在php中使用以下代码:
function Encrypt($pass, $salt){
$derived = PBKDF1($pass, $salt, 100, 32);
$key = bin2hex(substr($derived, 0, 32));
$iv = bin2hex(substr($derived, 32, 16));
return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $pass, MCRYPT_MODE_CBC, $iv);
}
function PBKDF1($pass, $salt, $count, $dklen)
{
$t = sha1($pass.$salt);
for($i=1; $i <= $count; $i++)
{
$t = sha1($t);
}
$t = substr($t,0,$dklen-1);
return $t;
}
函数加密($pass,$salt){
$derived=PBKDF1($pass,$salt,100,32);
$key=bin2hex(substr($derived,0,32));
$iv=bin2hex(substr($derived,32,16));
返回mcrypt_encrypt(mcrypt_RIJNDAEL_128,$key,$pass,mcrypt_MODE_CBC,$iv);
}
函数PBKDF1($pass、$salt、$count、$dklen)
{
$t=sha1($pass.$salt);
对于($i=1;$i什么不起作用?您希望发生什么?加密代码不一样,每次刷新页面时,加密代码都不一样。我有ASP的加密代码,但生成函数的加密代码与ASP.net和PHP中发生的行为不同?sa到底是什么我?我注意到这行$iv=mcrypt_create_iv(mcrypt_get_iv_size(mcrypt_RIJNDAEL_128,mcrypt_MODE_CBC),mcrypt_RAND);
您是否确保使用相同的iv进行加密和解密?如果它们不同,解密将失败。