久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將此 C# Rijndael 加密轉(zhuǎn)換為 PHP?

How do I convert this C# Rijndael encryption to PHP?(如何將此 C# Rijndael 加密轉(zhuǎn)換為 PHP?)
本文介紹了如何將此 C# Rijndael 加密轉(zhuǎn)換為 PHP?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

已經(jīng)有一些關于 SO 的有用問題:

There are already some helpful questions on SO:

  • Rijndael 256 在 c# 和 php 之間加密/解密?
  • 用 PHP 重寫 Rijndael 256 C# 加密代碼
  • Rijndael/AES 解密 C# 到 PHP 的轉(zhuǎn)換

但是,我的特殊情況仍然有困難.

However I am still having difficulties with my particular case.

我嘗試了各種方法,但最終得到錯誤 IV 參數(shù)必須與塊大小一樣長" 或與生成的哈希不匹配的文本.

I've tried various methods but end up getting the error "The IV parameter must be as long as the blocksize" or text that doesn't match the resulting hash.

我對加密的了解不夠深入,無法弄清楚我做錯了什么.

I don't understand encryption enough to work out what I'm doing wrong.

這是php版本:

$pass = 'hello';
$salt = 'application-salt';

echo Encrypt('hello', 'application-salt');

function Encrypt($pass, $salt)
{
    $derived = PBKDF1($pass, $salt, 100, 16);
    $key = bin2hex(substr($derived, 0, 8));
    $iv = bin2hex(substr($derived, 8, 8));
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $pass, MCRYPT_MODE_CBC, $iv);
}

function PBKDF1($pass, $salt, $count, $dklen)
{
    $t = $pass.$salt;
    $t = sha1($t, true);
    for($i=2; $i <= $count; $i++)
    {
        $t = sha1($t, true);
    }
    $t = substr($t,0,$dklen-1);
    return $t;
}

還有 C# 版本:

Console.WriteLine(Encrypt("hello", "application-salt"));
// output: "Hk4he+qKGsO5BcL2HDtbkA=="

public static string Encrypt(string clearText, string Password)
{
    byte[] clearData = System.Text.Encoding.Unicode.GetBytes(clearText);
    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
        new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });

    MemoryStream ms = new MemoryStream();
    Rijndael alg = Rijndael.Create();
    alg.Key = pdb.GetBytes(32);
    alg.IV = pdb.GetBytes(16);
    CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
    cs.Write(clearData, 0, clearData.Length);
    cs.Close();
    byte[] encryptedData = ms.ToArray();

    return Convert.ToBase64String(encryptedData);
}

我希望能夠在一個新的基于 php 的應用程序中驗證用戶登錄,該應用程序?qū)⑴c現(xiàn)有 C# 應用程序通信到同一個 MySQL 數(shù)據(jù)庫.我打算加密密碼并將生成的哈希值與存儲在數(shù)據(jù)庫中的哈希值進行比較以進行身份??驗證.

I want to be able to validate user logins in a new php-based application which will communicate to the same MySQL database as an existing C# application. I intend to encrypt the password and compare the resulting hash to the one stored in the database to authenticate.

任何指針將不勝感激.

我意識到在 C# 函數(shù)中,正在調(diào)用 PasswordDeriveBytes 并傳遞一個字節(jié)數(shù)組作為參數(shù),我在 PHP 版本中沒有類似的參數(shù).我發(fā)現(xiàn)這源自 Codeproject 示例,并且ASCII 中的字節(jié)數(shù)組拼寫為Ivan Medvedev",我假設他是示例作者.不幸的是,我無法改變這一點.

I realize that in the C# function, the PasswordDeriveBytes is being called and passed a byte array as an argument for which I don't have an analog in the PHP version. I discovered that this originates from a Codeproject example and that the byte array in ASCII spells "Ivan Medvedev" whom I assume to be the example author. Unfortunately I cannot change this.

推薦答案

我認為 PHP 版本實際上可能會在鍵和 IV 中添加 00h 值字節(jié).它們都有一個無效的大小:每個 8 個字節(jié).對于 AES-128,它們需要擴展到 16 個字節(jié).在您的 C# 代碼中,您使用 32 個字節(jié)作為密鑰,因此將使用具有 密鑰大小 256 位的 AES.

I think that the PHP version may actually add 00h valued bytes to the key and IV. They both have an invalid size : 8 bytes for each. They need to be extended to 16 bytes for AES-128. In your C# code you use 32 bytes for the key, which will therefore use AES with a key size of 256 bits.

此外,您沒有在 PasswordDeriveBytes 中指定迭代次數(shù),您應該指定它,因為該類沒有指定默認的迭代次數(shù) - 根據(jù)您的評論,這將是 100,讓我們假設它是.

Futhermore, you don't specify the number of iterations in PasswordDeriveBytes, you should specify it as the class does not specify the default number of iterations - according to your comments, this would be 100, lets assume it is.

哦,您使用了不正確的加密方法.MCRYPT_RIJNDAEL_256 指定使用 blocksize 256 位的 Rijndael 算法,而不是 256 位的 密鑰.據(jù)推測,密鑰的位大小只是密鑰的字節(jié)數(shù)乘以 8.

Oh, and you use the incorrect encryption method. MCRYPT_RIJNDAEL_256 specifies the Rijndael algorithm using a blocksize of 256 bits, not keys of 256 bits. Presumably, the bitsize of the keys is simply the number of bytes of the key times 8.

你能用這個替換你的加密功能然后再試一次嗎?

Could you replace your Encrypt function with this and try again?

function Encrypt($pass, $salt)
{
     $derived = PBKDF1($pass, $salt, 100, 48);
     $key = bin2hex(substr($derived, 0, 32));
     $iv = bin2hex(substr($derived, 32, 16));
     return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $pass, MCRYPT_MODE_CBC, $iv);
}

最后,請在進行加密或解密之前檢查生成的IV和密鑰是否與PHP中的IV和密鑰匹配.你確定那個 PHP PBKDF1 函數(shù)是正確的?

Finally, please check if the generated IV and key match with the ones in PHP before performing encryption or decryption. Are you sure that that PHP PBKDF1 function is correct?

更新:這里是關于 M$ PBKDF1 例程的更多信息在 PasswordDeriveBytes 中(包括您可以嘗試轉(zhuǎn)換的 Java 代碼):

UPDATE: Here is some more information on the M$ PBKDF1 routines in PasswordDeriveBytes (including Java code which you may try and convert):

哈,我明白你的意思.

有趣的是,使用.NET:調(diào)用48時結果不同或調(diào)用 32 然后調(diào)用 16:

Interestingly, using .NET: the results are different when calling 48 or calling 32 followed by 16:

.NET GetBytes( 32 +16 ):04DD9D139DCB9DE889946D3662B319682159FF9C9B47FA15ED205C7CAF890922655D8DD89AE1CAAC60A8041FCD7E8DA4

.NET GetBytes( 32 +16 ): 04DD9D139DCB9DE889946D3662B319682159FF9C9B47FA15ED205C7CAF890922655D8DD89AE1CAAC60A8041FCD7E8DA4

.NET GetBytes(32)04DD9D139DCB9DE889946D3662B319682159FF9C9B47FA15ED205C7CAF890922其次是 GetBytes( 16 ) 89946D3662B3196860A8041FCD7E8DA4

.NET GetBytes( 32 ) 04DD9D139DCB9DE889946D3662B319682159FF9C9B47FA15ED205C7CAF890922 Followed by GetBytes( 16 ) 89946D3662B3196860A8041FCD7E8DA4

真正的 Microsoft 代碼,他們無法更改它,因為它可能會破壞該領域的應用程序.請注意,當使用 16 字節(jié)和 8 字節(jié)或直接使用 24 字節(jié)設計調(diào)用它時,它們也會返回不同的結果.您最好升級到 PBKDF2,并將 PBKDF1 限制為最大 20 個字節(jié),如標準中所定義.

True Microsoft code, and they cannot change it because it could break applications in the field. Note that they also would return different results when calling it with 16 and then 8 bytes or directly by 24 bytes by design. You'd better upgrade to PBKDF2, and keep PBKDF1 limited to 20 bytes max, as defined in the standards.

這篇關于如何將此 C# Rijndael 加密轉(zhuǎn)換為 PHP?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數(shù)組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產(chǎn)生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 亚洲精品91 | 中文字幕亚洲欧美日韩在线不卡 | 亚洲激情在线观看 | 黄色大片视频 | 亚洲精品一区中文字幕乱码 | 成人免费视频 | 精品精品 | 欧美一级三级在线观看 | 天堂一区二区三区四区 | 亚洲黄色av | 成人免费大片黄在线播放 | 国产精品视频一二三区 | 国产情品 | 欧美区在线 | 91偷拍精品一区二区三区 | 亚洲欧美中文日韩在线v日本 | 一级电影免费看 | 综合婷婷 | 羞羞视频免费观看 | 国内精品久久影院 | 97精品国产97久久久久久免费 | 久久久久久久香蕉 | 狠狠操操 | 91高清视频在线观看 | 日本不卡一区 | 国产成人免费视频网站高清观看视频 | 一本一道久久a久久精品蜜桃 | 国产激情在线看 | 天天色天天射天天干 | 一级看片免费视频囗交动图 | 欧美激情久久久 | 亚洲视频免费在线观看 | 久久精品—区二区三区 | 久久av一区二区三区 | 欧美精品导航 | 久久精品 | 国产日韩免费视频 | 亚洲成人在线网 | 中文字幕久久久 | 99视频网| 自拍偷拍亚洲视频 |