简体   繁体   English

用PHP加密字符串,用C#解密?

[英]Encrypt a string in PHP, Decrypt it in C#?

In the action.php page, I have a string that I want to store in mysql but I want to encrypt it before inserting it to the db 在action.php页面中,我有一个要存储在mysql中的字符串,但是我想在将其插入数据库之前对其进行加密

 $data = "Hello World";
 $data = Encrypt_This_String($data,"ABCD"); // abcd is a key
 // now I insert this string into MySQL database

In the other side, I need to get back this string from my C#.NET application, so I need to do something like this : 另一方面,我需要从C#.NET应用程序中获取此字符串,因此我需要执行以下操作:

 string myStr = getMyString(); // where getMyString() is a function that get the encrypted string from db

 myStr = Decrypt_String(myStr,"ABCD");

I need that myStr hold the value "Hello World" 我需要myStr保持值“ Hello World”

Could someone please give me an encryption algorithm that is based on a key and can be used in both PHP and C# ? 有人可以给我一种基于密钥的加密算法,该算法可以在PHP和C#中使用吗?

Use mcrypt_encrypt to encrypt the data from PHP, with eg AES in CBC mode as the cipher; 使用mcrypt_encrypt加密来自PHP的数据,例如以CBC模式下的AES作为密码; there is example code on the documentation page. 文档页面上有示例代码。

Then decrypt from C# using AesManaged or AesCryptoServiceProvider , both pages on MSDN have example code. 然后使用AesManagedAesCryptoServiceProvider从C#解密,MSDN上的两个页面都有示例代码。

You can use Rijndael (AES 128) : 您可以使用Rijndael(AES 128):

PHP- AES128 Example PHP- AES128示例

C# AES128 Example C#AES128示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM