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

使用 Rijndael 進行跨平臺(php 到 C# .NET)加密/解密

Cross platform (php to C# .NET) encryption/decryption with Rijndael(使用 Rijndael 進行跨平臺(php 到 C# .NET)加密/解密)
本文介紹了使用 Rijndael 進行跨平臺(php 到 C# .NET)加密/解密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我目前在解密由 php mcrypt 加密的消息時遇到了一些問題.php代碼如下:

I'm currently having a bit of problem with decrypting a message encrypted by php mcrypt. The php code is as following:

<?php
  //$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
  $iv = "45287112549354892144548565456541";
  $key = "anjueolkdiwpoida";
  $text = "This is my encrypted message";
  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
  $crypttext = urlencode($crypttext);
  $crypttext64=base64_encode($crypttext);
  print($crypttext64) . "
<br/>";
?>

然后將加密的消息發送到 ASP.NET 平臺 (C#).但是,我在保留解密順序(base64 解碼到 urldecode)時遇到了問題.我在 ASP.NET 中的代碼如下(iv 和 key 與 php 中的相同):

The the encrypted message is then sent to a ASP.NET platform (C#). However, I'm having problem retaining the order of decryption (base64 decode to urldecode). The code I had in ASP.NET is as the following (iv and key is the same as in php):

public string Decode(string str)
{
    byte[] decbuff = Convert.FromBase64String(str);
    return System.Text.Encoding.UTF8.GetString(decbuff);
}

static public String DecryptRJ256(string cypher, string KeyString, string IVString)
{

    string sRet = "";
    RijndaelManaged rj = new RijndaelManaged();
    UTF8Encoding encoding = new UTF8Encoding();


    try
    {
        //byte[] message = Convert.FromBase64String(cypher);
        byte[] message = encoding.GetBytes(cypher);

        byte[] Key = encoding.GetBytes(KeyString);
        byte[] IV = encoding.GetBytes(IVString);

        rj.Padding = PaddingMode.Zeros;
        rj.Mode = CipherMode.CBC;
        rj.KeySize = 256;
        rj.BlockSize = 256;
        rj.Key = Key;
        rj.IV = IV;
        MemoryStream ms = new MemoryStream(message);

        using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
        {
            using (StreamReader sr = new StreamReader(cs))
            {
                sRet = sr.ReadToEnd();
            }
        }

    }
    finally
    {
        rj.Clear();
    }

    return sRet;


}

string temp = DecryptRJ256(Server.UrlDecode(Decode(cypher)), keyString, ivString);

我遇到的問題是,在收到來自 php 的加密消息后,我將其轉換為 byte[],然后再轉換回 UTF8 編碼字符串,以便我可以對其進行 urldecode.然后我將結果提供給函數,在那里我將字符串轉換回 byte[] 并通過解密過程運行它.但是,我無法得到想要的結果……有什么想法嗎?

The problem I'm having is that after I recieved the encrypted message from php, I converted it into byte[] and then converted back to UTF8 encoded string so I can urldecode it. then I feed the result into the function where I converted the string back to byte[] and ran it through the decryption process. However, I can't get the desired result...any ideas?

提前致謝.

推薦答案

在這里我可以看到兩邊的問題.

Here I can see problems on both sides.

請記住,編碼時得到的不是字符串,而是字節數組.所以在 PHP 中你不需要對 cyphertext 進行 urlencode.

Please keep in mind that what you get when encoding is not string, but rather an array of bytes. So in PHP you don't need to urlencode cyphertext.

base64 編碼就是你所需要的.當您打開 base64_encode help 時,您會看到

base64 encoding is all you need. When you open base64_encode help you see

base64_encode 使用 base64 對給定數據進行編碼.這種編碼是旨在使二進制數據在傳輸過程中幸存

base64_encode Encodes the given data with base64. This encoding is designed to make binary data survive transport

還有一件事 - 要在 .net 中以正確的長度解碼您的消息,您必須手動附加填充字符.RijndaelManaged 的??默認填充模式是 PKCS7,讓我們堅持下去.您必須將源字符串擴展到字符代碼等于填充字節數的偶數塊.

One more thing - to have your message decoded in .net with a correct length, you have to manually append it with padding characters. Default padding mode for RijndaelManaged is PKCS7, lets' stick with it. You have to extend your source string to even blocks with characters code equal to number of padding bytes.

<?php
  $iv = "45287112549354892144548565456541";
  $key = "anjueolkdiwpoida";
  $text = "This is my encrypted message";

  // to append string with trailing characters as for PKCS7 padding scheme
  $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
  $padding = $block - (strlen($text) % $block);
  $text .= str_repeat(chr($padding), $padding);

  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);

  // this is not needed here            
  //$crypttext = urlencode($crypttext);

  $crypttext64=base64_encode($crypttext);
  print($crypttext64) . "
<br/>";
?>

在 C# 方面,您可以從 base64byte[]stringbyte[] 的轉換.您只需要進行從 base64byte[] 的第一次轉換.請記住,base64 保存的是二進制數據而不是字符串的加密文本.另請注意,RijndaelManaged 是 IDisposable,所以我將它包裝在 using() 構造中.如 MSDN 所述,調用 Close() 是必要的,但還不夠.

At C# side you have casting from base64 to byte[] to string to byte[]. You have to do the first conversion from base64 to byte[] only. Remember, base64 is holding the cyphered text that is binary data, not string. Also please note that RijndaelManaged is IDisposable, so I have wrapped it in using() construct. Calling Close() is necessary but not enough as stated in MSDN.

public byte[] Decode(string str)
{
    var decbuff = Convert.FromBase64String(str);
    return decbuff;
}

static public String DecryptRJ256(byte[] cypher, string KeyString, string IVString)
{
    var sRet = "";

    var encoding = new UTF8Encoding();
    var Key = encoding.GetBytes(KeyString);
    var IV = encoding.GetBytes(IVString);

    using (var rj = new RijndaelManaged())
    {
        try
        {
            rj.Padding = PaddingMode.PKCS7;
            rj.Mode = CipherMode.CBC;
            rj.KeySize = 256;
            rj.BlockSize = 256;
            rj.Key = Key;
            rj.IV = IV;
            var ms = new MemoryStream(cypher);

            using (var cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
            {
                using (var sr = new StreamReader(cs))
                {
                    sRet = sr.ReadLine();
                }
            }
        }
        finally
        {
            rj.Clear();
        }
    }

    return sRet;
}

因此,以下 C# 代碼將返回初始字符串:

As a result, following code in C# will return you the initial string:

var iv = "45287112549354892144548565456541";
var key = "anjueolkdiwpoida";
var cypher = "u+rIlHB/2rrT/u/qFInnlEkg2unhizsNzGVb9O54sP8=";

var temp = DecryptRJ256(Decode(cypher), key, iv);

這篇關于使用 Rijndael 進行跨平臺(php 到 C# .NET)加密/解密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 中的數組自動填充選擇框)
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 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 国际精品鲁一鲁一区二区小说 | 国产九九九九 | 免费啪啪 | 日韩欧美三区 | 精品久久久久久久久久久久 | 91亚洲精选| 黄网站涩免费蜜桃网站 | 成人黄色在线视频 | 成人亚洲性情网站www在线观看 | 欧美日在线 | 天天操夜夜操 | 精品成人69xx.xyz | 国产精品视频久久久 | 99精品99| 欧美aaa| 国产黄色在线观看 | 秋霞a级毛片在线看 | 亚洲精品福利视频 | 日日摸夜夜爽人人添av | 国产精品免费一区二区三区四区 | av中文在线播放 | 日韩欧美亚洲 | 一级视频黄色 | www.久久精品视频 | 欧美日韩亚洲成人 | 欧美黑人体内she精在线观看 | 精品三区 | 成人一区二 | 嫩草视频在线 | 视频一区二区三区四区五区 | 日韩在线一区二区三区 | 夜夜干夜夜操 | 美女天堂 | 一级黄色绿像片 | 97偷拍视频 | 精品一区二区三区在线观看国产 | 国产精品久久久久久久粉嫩 | 精品一区二区三区不卡 | 国产免费一区二区三区免费视频 | 亚洲精品久久久久avwww潮水 | 国产精品一区二区av |