コード例 #1
0
ファイル: PayInterface.cs プロジェクト: zsurge/UBPayApp
        /// <summary>
        /// AES解密(无向量)
        /// </summary>
        /// <param name="encryptedBytes">被加密的明文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static string AESDecrypt(String Data, String Key)
        {
            MYLIB mylib = new MYLIB();

            Byte[] encryptedBytes = mylib.AscToBcd(Data, Data.Length);
            Byte[] bKey           = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);

            MemoryStream mStream = new MemoryStream(encryptedBytes);
            //mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
            //mStream.Seek( 0, SeekOrigin.Begin );
            RijndaelManaged aes = new RijndaelManaged();

            aes.Mode    = CipherMode.ECB;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            aes.Key     = bKey;
            //aes.IV = _iV;
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);

            try
            {
                byte[] tmp = new byte[encryptedBytes.Length + 32];
                int    len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
                byte[] ret = new byte[len];
                Array.Copy(tmp, 0, ret, 0, len);
                return(Encoding.UTF8.GetString(ret));
            }
            finally
            {
                cryptoStream.Close();
                mStream.Close();
                aes.Clear();
            }
        }
コード例 #2
0
ファイル: PayInterface.cs プロジェクト: zsurge/UBPayApp
        public static string AESDecrypt(String Data, String Key, String Vector)
        {
            MYLIB mylib = new MYLIB();

            // 256-AES key
            byte[] keyArray = new byte[Key.Length / 2];
            mylib.GetHexFromString(Key, keyArray);

            //"112233" -> 0x11, 0x22, 0x33
            byte[] toEncryptArray = new byte[Data.Length / 2];
            mylib.GetHexFromString(Data, toEncryptArray);

            RijndaelManaged rDel = new RijndaelManaged();

            rDel.IV      = Encoding.UTF8.GetBytes(Vector.Substring(0, 16));
            rDel.Key     = keyArray;
            rDel.Mode    = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();

            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0,
                                                                toEncryptArray.Length);

            return(Encoding.UTF8.GetString(resultArray));
        }
コード例 #3
0
ファイル: PayInterface.cs プロジェクト: zsurge/UBPayApp
        public static string AESEncrypt02(String Data, String Key)
        {
            MYLIB mylib = new MYLIB();

            // 256-AES key
            //byte[] keyArray = new byte[Key.Length / 2];
            //mylib.GetHexFromString(Key, keyArray);

            byte[] keyArray = HexStringToBytes(Key);

            //byte[] toEncryptArray = Encoding.UTF8.GetBytes(Data);
            byte[] toEncryptArray = UTF8Encoding.ASCII.GetBytes(Data);

            RijndaelManaged rDel = new RijndaelManaged();

            //rDel.IV = Encoding.UTF8.GetBytes(AES_IV.Substring(0, 16));
            rDel.Key     = keyArray;
            rDel.Mode    = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();

            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0,
                                                                toEncryptArray.Length);

            //0x11, 0x22, 0x33 -> "112233"
            return(BytesToHexString(resultArray));
        }