douhuan1648 2014-01-28 14:32
浏览 113
已采纳

使用AES 256 C#php加密和解密非常长的字符串

I got an encrypting function written in PHP which encrypts my data, and a decrypt function in C# which decrypts it and prints on screen(I'm developing a game in Unity engine). So the problem is, if the data string is long it won't decrypt the last part of it... I'm using AES 256 encryption with key

php function:

$username = "Name"
$id = 1;
$email = "email@example.com"

$data = $username . "
" . $id . "
" . $email;

$key = "my 256 bit key"; //32 bytes
function aes256Encrypt($key, $data) {
    if(32 !== strlen($key)) $key = hash('SHA256', $key, true);
    $padding = 16 - (strlen($data) % 16);
    $data .= str_repeat(chr($padding), $padding);
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16));
}

echo base64_encode(aes256Encrypt($key, $data));

This is my C# full code which prints the decrypted string on game screen:

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

public class session : MonoBehaviour {

    private string sessionURL = "http://localhost/xampp/game/session.php";

    void Start () 
    {
        StartCoroutine(GetSession());
    }

    IEnumerator GetSession()
    {
        gameObject.guiText.text = "Loading Session";
        WWW ses_get = new WWW(sessionURL);
        yield return ses_get;

        string key = "my 256 bit key";
        string base64_ciphered_text = ses_get.text;
        String sestext = Decrypt(base64_ciphered_text, key);

        if (ses_get.error != null)
        {
            print("There was an error getting the session: " + ses_get.error);
        }
        else
        {
            guiText.richText = true;
            guiText.text = sestext;
        }
    }


    public String Decrypt(String text, String key)
    {
        //decode cipher text from base64
        byte[] cipher = Convert.FromBase64String(text);
        //get key bytes
        byte[] btkey = Encoding.ASCII.GetBytes(key);

        //init AES 256
        RijndaelManaged aes256 = new RijndaelManaged();
        aes256.Mode = CipherMode.ECB;
        aes256.Padding = PaddingMode.Zeros;

        //decrypt
        ICryptoTransform decryptor = aes256.CreateDecryptor(btkey, null);
        MemoryStream ms = new MemoryStream(cipher);
        CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

        byte[] plain = new byte[cipher.Length];
        int decryptcount = cs.Read(plain, 0, plain.Length);

        ms.Close();
        cs.Close();

        //return plaintext in String
        return Encoding.UTF8.GetString(plain, 0, decryptcount);
    }

}

Anyone got an idea?

One example of what I mean: $data: http://puu.sh/6BkU4.png output on screen: http://puu.sh/6BkTF.jpg

  • 写回答

1条回答 默认 最新

  • dongmeng4742 2014-01-28 14:46
    关注

    You're using CBC-mode in PHP and ECB in C# so after the first block things will go wrong. You need to use the same mode in both cases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!