礼品卡卡信息的解密方法

此为C#版,如需PHP版,请访问PHP版礼品卡的解密方法

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

namespace ConsoleAPI
{
    class Program
    {
        static void Main(string[] args)
        {
            //密钥是您的用户ID(即用户名,不是openid)的前8位,不足8位则用数字0补齐
            string key = "12345678";

            //待加密字符串
            string s1 = "1";

            //加密
            string s2 = DesEncrypt(s1, key);

            //解密
            string s3 = DesDecrypt("g6AtgJul6q0=", key);

            //加密、解密输出
            Debug.WriteLine("加密前:" + s1);
            Debug.WriteLine("加密后:" + s2);
            Debug.WriteLine("解密后:" + s3);
        }

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="strText">明文</param>
        /// <param name="encryptKey">密钥</param>
        /// <returns></returns>
        static string DesEncrypt(string strText, string encryptKey)
        {
            string outString = "";
            byte[] byKey = null;
            byte[] IV = Encoding.Default.GetBytes(encryptKey);
            try
            {
                byKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, encryptKey.Length));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                outString = Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception)
            {
                outString = "";
            }
            return outString;
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="strText">密文</param>
        /// <param name="decryptKey">密钥</param>
        /// <returns></returns>
        static string DesDecrypt(string strText, string decryptKey)
        {
            string outString = "";
            byte[] byKey = null;
            byte[] IV = Encoding.Default.GetBytes(decryptKey);
            byte[] inputByteArray = new Byte[strText.Length];
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, decryptKey.Length));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Mode = CipherMode.ECB;
                inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = new System.Text.UTF8Encoding();
                outString = encoding.GetString(ms.ToArray());
            }
            catch (Exception)
            {
                outString = "";
            }
            return outString;
        }
    }
}


登录 后可发表评论

    最新发布
    聚合数据©版权所有