我如何与AES加密在C#这样我就可以在PHP解密呢? [英] How can I encrypt with AES in C# so I can decrypt it in PHP?

查看:502
本文介绍了我如何与AES加密在C#这样我就可以在PHP解密呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了几个答案在PHP中加密和解密在C#中,但至今一直未能逆转这一过程...



背景是我想:



在C#中:
AES加密文件的内容。
(通过POST可能通过HTTP)上传数据到服务器



在PHP中:
接收并保存文件



和在PHP(日后):
解密文件



我特别想对它进行加密使用SSL外/ TLS(虽然我可能要做到这一点为好),因为我需要知道文件保持加密(和解密的!)当存储在服务器上。



要在C#中加密我使用:

  Rijndael算法RijndaelAlg = Rijndael.Create(); 
RijndaelAlg.KeySize = 128;
RijndaelAlg.Mode = CipherMode.CBC;
的CryptoStream cStream =新的CryptoStream(fstream的,RijndaelAlg.CreateEncryptor(密钥,IV),
CryptoStreamMode.Read);

和在PHP解密:

  mcrypt_cbc(MCRYPT_RIJNDAEL_128,$键,$缓冲区,MCRYPT_DECRYPT,$ IV); 


解决方案

我知道这是问前一段时间,但我认为我会发布我为别人解决方案。我在PHP和C#中,可以让你加密/解密两种方式写了一个简单的代码示例。我有几个问题与得到双方的设置工作了。在填充的差异会让它解密的一种方式而不是其他。



https://github.com/dchymko/.NET--PHP-encryption



希望帮助一些人。


I've found a few answers to Encrypt in PHP, and Decrypt in C#, but as yet have been unable to reverse the process...

The background is I want to:

In C#: AES encrypt a file's contents. Upload the data (likely via http via POST) to a server.

In PHP: Receive and save the file.

And in PHP (at a later date): Decrypt the file.

I specifically want to encrypt it outside of using SSL/TLS (though I might have to do this as well), as I need to know the file remains encrypted (and decryptable!) when stored on the server.

To encrypt in C# I'm using:

Rijndael RijndaelAlg = Rijndael.Create();
RijndaelAlg.KeySize = 128;
RijndaelAlg.Mode = CipherMode.CBC;
CryptoStream cStream = new CryptoStream(fStream, RijndaelAlg.CreateEncryptor(Key, IV),
                                        CryptoStreamMode.Read);

and to decrypt in PHP:

 mcrypt_cbc(MCRYPT_RIJNDAEL_128, $key, $buffer, MCRYPT_DECRYPT, $iv);

解决方案

I know this was asked a while ago but I thought I'd post my solution for others. I wrote up a quick code example in PHP and C# that lets you encrypt/decrypt both ways. I had a few issues with getting the settings on both sides to work out. A difference in padding would let it decrypt one way but not the other

https://github.com/dchymko/.NET--PHP-encryption

hope that helps some people.

这篇关于我如何与AES加密在C#这样我就可以在PHP解密呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆