c# - 如何使用 20 字节 key 进入 AES ECB 模式

标签 c# javascript php

如何用 20 字节 key (不能更改 key )加密文本“debac58d0bc8526339678667bca923e15a7106a0c16c8148bd1468cefad57762ccf53a4a780bc27748c5526339678667bca923e15a7106a0c16c8148bd1468cefad57762ccf53a4a780bc27748c55267bca923e15a7106a0c16c8148bd1468cefad57762ccf53a4a780bc27748c5583a02c41deedee key 解密 fdsaz",纯文本“http://www.hellohello.com/hellohello.asp” .. 任何 javascript、php、C# 都会有帮助

最佳答案

好的,这是 C# 中的解决方案,它只支持任何 Key key 大小最多 32 字节和低至 0 字节的 key 。

Ideone 样本:https://ideone.com/XONRBf

解决方案截图:

answer

C# 源代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String cryptedText = "debac58d0bc8526339678667bca923e15a7106a0c16c8148bd1468cefad57762ccf53a4a780bc27748c5583a02c41dee";
            String key = "qwertyuioplkjhgfdsaz";
            Console.WriteLine("Crypted Text = " + cryptedText);
            Console.WriteLine("Key = " + key);
            Console.WriteLine("Decrypted = " + Decrypt(cryptedText, key));
            Console.ReadKey();
        }

        public static string Decrypt(string toDecrypt, string key)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); // AES-256 key
            PadToMultipleOf(ref keyArray, 8);
            //byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
            byte[] toEncryptArray = ConvertHexStringToByteArray(toDecrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.KeySize = (keyArray.Length * 8);
            rDel.Key = keyArray;          // in bits
            rDel.Mode = CipherMode.ECB; // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
            rDel.Padding = PaddingMode.None;  // better lang support
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        public static void PadToMultipleOf(ref byte[] src, int pad)
        {
            int len = (src.Length + pad - 1) / pad * pad;
            Array.Resize(ref src, len);
        }

        public static byte[] ConvertHexStringToByteArray(string hexString)
        {
            if (hexString.Length % 2 != 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
            }

            byte[] HexAsBytes = new byte[hexString.Length / 2];
            for (int index = 0; index < HexAsBytes.Length; index++)
            {
                string byteValue = hexString.Substring(index * 2, 2);
                HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            }

            return HexAsBytes;
        }
    }
}

关于c# - 如何使用 20 字节 key 进入 AES ECB 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22409692/

相关文章:

c# - 如何在 C# 的数据库中创建具有模块化、可搜索字段的对象?

javascript - Marionette.CompositeView 中 serializeData 和 onRender 的区别

php - 如何通过为空字段添加 NULLS 将 CSV 文件导入 MySQL?

php - 使用 array_filter 和 array_splice 从行加载特定字段,之后想要清空字段

javascript - TinyMCE 如何将 baseURL 附加到 LINK 和图像的基本路径

php - 在数据库中存储可变数量的值

c# - 加载时的 Winforms ListView ItemCheck

c# - 通过 C# .NET 访问 Gmail Drive

c# - 在没有 CancellationToken 的情况下停止任务

javascript - 如何使用 chrome 扩展解除 document.ondblclick 的绑定(bind)