ฉันต้องการส่งข้อมูลที่เข้ารหัสจากแอปพลิเคชันใน C# ไปยังบริการเว็บภายนอกที่เขียนด้วย PHP (เป็นเว็บแอปพลิเคชันบุคคลที่สามที่ฉันไม่สามารถควบคุมได้) ฉันได้รับข้อมูลโค้ดต่อไปนี้เกี่ยวกับตรรกะการเข้ารหัสที่ใช้ในแอปพลิเคชัน PHP:
\n $data = \'data to be sent\';\n $base64data = base64_encode($data); \n $ciphering = "AES-128-CBC"; \n $iv_length = openssl_cipher_iv_length($ciphering); \n $options = 0; \n $encryption_iv = \'1234567891234567\'; \n $encryption_key = md5(\'123456789\'); \n $encryptedData= base64_encode(openssl_encrypt($base64data, $ciphering, \n $encryption_key, $options, $encryption_iv));\n
\ nฉันได้เขียนการใช้งานต่อไปนี้ใน C# แต่ผลลัพธ์ของการเข้ารหัสแตกต่างจากที่สร้างจากโค้ด PHP
\n string data = "data to be sent";\n string base64data = Convert.ToBase64String(Encoding.UTF8.GetBytes(data));\n\n // Encryption key is MD5 hash of value: \'123456789\'\n MD5 hasher = MD5.Create();\n byte[] HashValue = hasher.ComputeHash(Encoding.UTF8.GetBytes("123456789"));\n string encryptionKey = "";\n foreach (byte b in HashValue)\n {\n encryptionKey += b.ToString("x2");\n }\n byte[] keyBytes = Encoding.UTF8.GetBytes(encryptionKey);\n\n byte[] iv = Encoding.UTF8.GetBytes("1234567891234567");\n\n RijndaelManaged aes = new RijndaelManaged();\n aes.Mode = CipherMode.CBC;\n aes.KeySize = 128;\n aes.Key = keyBytes;\n aes.IV = iv;\n ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);\n MemoryStream msEncrypt = new MemoryStream();\n CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);\n StreamWriter swEncrypt = new StreamWriter(csEncrypt);\n\n swEncrypt.Write(base64data);\n\n swEncrypt.Close();\n csEncrypt.Close();\n aes.Clear();\n aes.Clear();\n byte[] encryptedData = msEncrypt.ToArray();\n\n Console.WriteLine($"Encrypted data: {Convert.ToBase64String(encryptedData)}"); \n
\nโปรดทราบว่าฉันยังใช้คลาส AesManaged ใน .NET และคลาสที่เกี่ยวข้องในไลบรารี BouncyCastle.Crypto เพื่อทำการเข้ารหัสใน C# (ในกรณีที่ฉันอาจพลาดบางสิ่งไป ในการดำเนินการของฉัน) ในทุกกรณี ผลลัพธ์ที่สร้างขึ้นใน C# (แน่นอนว่าเหมือนกันไม่ว่าจะใช้ไลบรารีใดใน .NET) จะแตกต่างจากผลลัพธ์ที่สร้างจากโค้ด PHP ใครสามารถให้ข้อมูลเกี่ยวกับสิ่งที่อาจเป็นปัญหาได้บ้าง
\nOur community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.