在 PHP 中加密 AES,在 C# 中解密

发布于 2024-11-28 17:08:58 字数 2218 浏览 0 评论 0原文

我尝试在 PHP 中使用此函数加密数据,并使用 C# 中的其他函数解密。但我没有得到相同的字符串。

//php function
    public function onCrypt($text)
    {   
    $key=md5('DFDFDFDFDFDFDFDFDFDFDFDF',true);

    $crypttext = urldecode(trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND))));

    $text_crp =base64_encode($crypttext);


    return $text_crp;
            }

//c# 函数

//public static void DecryptFile 参数: strKey:解密时选择的密钥。 PathPlainTextFile: 加密文件的路径 PathPlainTextFile:解密后的原始文件。

public static void DecryptFile(string strKey, string pathPlainTextFile, string pathCypheredTextFile)     
  {  

//crypt key with md5 function           
System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create();
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
byte[] md5val = alg.ComputeHash(enc.GetBytes(strKey));


StreamReader fsPlainTextFile = File.OpenText(pathPlainTextFile);    
FileInfo t = new FileInfo(pathCypheredTextFile);
StreamWriter Tex =t.CreateText();
string input = null;
while ((input = fsPlainTextFile.ReadLine()) != null)
{


        byte[] cipheredData = Convert.FromBase64String(input);

           RijndaelManaged rijndaeld = new RijndaelManaged();

            // define the used mode
            rijndaeld.Mode = CipherMode.ECB;

            // create the cipher AES - Rijndael
            ICryptoTransform decryptor = rijndaeld.CreateDecryptor(md5val,null);

             // Write the ciphered data in  MemoryStream
            MemoryStream ms= new MemoryStream(cipheredData);
            CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

                 // Insert  the ciphered data in a byte array
               byte[] plainTextData = new byte[cipheredData.Length];

            int decryptedByteCount = cs.Read(plainTextData, 0, plainTextData.Length);

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


   // Insert  the ciphered data in string encoded on Base64         
Tex.WriteLine (Encoding.UTF8.GetString(plainTextData, 0, decryptedByteCount));





}

Tex.Close();    


    }

I tried to encrypt data using this function in PHP and decrypt it with the other function in C#. But I don't get the same string.

//php function
    public function onCrypt($text)
    {   
    $key=md5('DFDFDFDFDFDFDFDFDFDFDFDF',true);

    $crypttext = urldecode(trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND))));

    $text_crp =base64_encode($crypttext);


    return $text_crp;
            }

//c# function

//public static void DecryptFile
Parameters :
strKey : the key choosed in decryption .
PathPlainTextFile : path of the crypted file
PathPlainTextFile : the original file decrypted.

public static void DecryptFile(string strKey, string pathPlainTextFile, string pathCypheredTextFile)     
  {  

//crypt key with md5 function           
System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create();
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
byte[] md5val = alg.ComputeHash(enc.GetBytes(strKey));


StreamReader fsPlainTextFile = File.OpenText(pathPlainTextFile);    
FileInfo t = new FileInfo(pathCypheredTextFile);
StreamWriter Tex =t.CreateText();
string input = null;
while ((input = fsPlainTextFile.ReadLine()) != null)
{


        byte[] cipheredData = Convert.FromBase64String(input);

           RijndaelManaged rijndaeld = new RijndaelManaged();

            // define the used mode
            rijndaeld.Mode = CipherMode.ECB;

            // create the cipher AES - Rijndael
            ICryptoTransform decryptor = rijndaeld.CreateDecryptor(md5val,null);

             // Write the ciphered data in  MemoryStream
            MemoryStream ms= new MemoryStream(cipheredData);
            CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

                 // Insert  the ciphered data in a byte array
               byte[] plainTextData = new byte[cipheredData.Length];

            int decryptedByteCount = cs.Read(plainTextData, 0, plainTextData.Length);

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


   // Insert  the ciphered data in string encoded on Base64         
Tex.WriteLine (Encoding.UTF8.GetString(plainTextData, 0, decryptedByteCount));





}

Tex.Close();    


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮光沉寂 2024-12-05 17:08:58

ECB 模式并不安全。您应该使用 CTR 模式或 CBC 模式。最好还明确指定要在两端使用的填充。

ECB mode is not secure. You should use either CTR mode or CBC mode. It is also best to explicitly specify the padding you are going to use at both ends.

不离久伴 2024-12-05 17:08:58

乍一看,您没有向 C# 解密器提供 IV:

ICryptoTransform decryptor = rijndaeld.CreateDecryptor(md5val, null);

我不熟悉 php,但看起来您在加密内容时创建了 IV。您需要使用相同的 IV 来在 C# 代码中解密它(即使您通过 php 进行解密,您也需要相同的 IV 来解密它)。

At a quick glance, you're not supplying an IV to the C# decryptor:

ICryptoTransform decryptor = rijndaeld.CreateDecryptor(md5val, null);

I'm not familiar with php, but it looks like you created an IV when you encrypted the content. You'll need to have that same IV to decrypt it in the C# code (you'd need the same IV to decrypt it even if you were doing the decryption through php).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文