C# 与php 7.1 资料传输以DES加解密

Phanix
·
·
IPFS
·

原本在php 7.0 及之前可以直接用mcrypt,但是在7.1 之后就被拿掉了,除非用PCEL 去装,或者使用其他的library,要不然应该就只能使用openssl 的加解密。

原本应该是很容易的事情,但是因为C# 跟php 两边的写法实在差很大,其中php 用openssl 做加解密真的方便很多,麻烦的主要是在C# 这边…

php 使用openssl 完成DES 加解密

$password = '12345678'; //DES 8 digit password
var_dump($password);

$method = "des-cbc";
var_dump($method);

$plain = "this is a book";
$iv = $password;

$cipher = openssl_encrypt($plain, $method, $password, 0, $iv);
var_dump($cipher);

var_dump(openssl_decrypt($cipher, $method, $password, 0, $iv));

C#实作DES加解密

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

namespace MySecure
{
    public static class MyEncryption
    {

        ///
        /// DES Encryption
        ///
        /// <param name="plain">Plain text for encryption.
        /// <param name="encryptKey">keyfor encryption, length = 8
        /// <returns>Return encrypted string. If fail, return null.</returns>
        public static string EncryptDES(string encryptString, string encryptKey = "12345678")
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] key = Encoding.ASCII.GetBytes(encryptKey);
                byte[] iv = Encoding.ASCII.GetBytes(encryptKey);
                byte[] dataByteArray = Encoding.UTF8.GetBytes(encryptString);

                des.Key = key;
                des.IV = iv;
                string encrypt = "";
                using (MemoryStream ms = new MemoryStream())
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(dataByteArray, 0, dataByteArray.Length);
                    cs.FlushFinalBlock();
                    encrypt = Convert.ToBase64String(ms.ToArray());
                }
                return encrypt;
            }
            catch
            {
                return null;
            }
        }

        ///
        /// DES Decryption
        ///
        /// <param name="cipher">Cipher text for decryption.
        /// <param name="decryptKey">key for decrypt, length = 8
        /// <returns>Return decripted string. If fail, return null.</returns>
        public static string DecryptDES(string cipher, string decryptKey = "12345678")
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] key = Encoding.ASCII.GetBytes(decryptKey);
                byte[] iv = Encoding.ASCII.GetBytes(decryptKey);
                des.Key = key;
                des.IV = iv;

                byte[] dataByteArray = Convert.FromBase64String(cipher);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(dataByteArray, 0, dataByteArray.Length);
                        cs.FlushFinalBlock();
                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
            catch
            {
                return null;
            }
        }
    }
}

呼叫使用

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MySecure;

namespace TestConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string plain = "this is a book";
            Console.WriteLine(plain);
            string cipher = MyEncryption.EncryptDES(plain, "12345678");
            Console.WriteLine(cipher);
            Console.WriteLine(MyEncryption.DecryptDES(cipher, "12345678"));
            Console.ReadLine();
        }
    }
}

Original link: Phanix's Blog

CC BY-NC-ND 2.0 授权

喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!