MCipherEncryptor.cs

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

namespace YFCCBPay
{
    public class MCipherEncryptor
    {
        static byte[] temp = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        public static String doEncrypt(String srcString, String encryptKey)
        {
            encryptKey = encryptKey.Substring(0, 8);

            Encoding utf16Encoding = Encoding.GetEncoding("UTF-16");
            byte[] srcBytes = utf16Encoding.GetBytes(srcString);

            Encoding ISO88591Encoding = Encoding.GetEncoding("ISO-8859-1");
            byte[] keyBytes = ISO88591Encoding.GetBytes(encryptKey);

            byte[] cipherBytes = wrapBytes(srcBytes, keyBytes);
            String basedString = Convert.ToBase64String(cipherBytes);
            return System.Web.HttpUtility.UrlEncode(basedString.Replace("+", ","), ISO88591Encoding);
        }

        private static byte[] wrapBytes(byte[] byteEncrypt, byte[] key)
        {            
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.PKCS7;
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, temp), CryptoStreamMode.Write);
            cryptoStream.Write(byteEncrypt, 0, byteEncrypt.Length);
            cryptoStream.FlushFinalBlock();
            return memoryStream.ToArray();
        }
    }
}

 

你可能感兴趣的:(.NET)