【问题标题】:Encrypt in ColdFusion, Decrypt in C#在 ColdFusion 中加密,在 C# 中解密
【发布时间】:2016-06-29 22:15:42
【问题描述】:

我有一个使用 ColdFusion 9 使用 encrypt() 函数加密的字符串。问题是该函数是使用 UUEncode 算法(默认)调用的。我需要使用 C# 解密这个值。所有样本都使用 Base64 编码。有什么想法吗?

这是 ColdFusion 中使用的内容

 <cfset encryptionKey = generateSecretKey( "AES" ) />

 <cfset pwhash = encrypt( user_password,
                          encryptionKey,
                          "AES",
                          "UU"
                       ) />

我有结果值。我也有一个用户的解密结果值,所以我可以将它与 C# 中的东西进行比较。我需要解密系统中所有用户的密码。

【问题讨论】:

  • 快速谷歌搜索发现fewresultsforUUDecode,你试过了吗?
  • 当然可以,但我不知道它是如何适应的。
  • 你在问题​​中说数据是用 UUEncode 加密的,所以逻辑上你可以用 UUDecode 解密吗?
  • 是的,但它比 UU 还多。 Cold Fusion 在 AES 加密方法中使用 UU 编码,并带有一个密钥。那么所有其他部分呢?我知道 UU 编码/解码是其中的一部分,但不确定它是如何发挥作用的,即要采取哪些其他步骤使其适合使用 ASCII 或其他内容的解密代码。
  • 只要可以将 UU 值解码为字节,the other example 应该可以正常工作。只需将SomeMethodToConvertHexToBytes 替换为您用于将加密值解码为字节的任何方法。

标签: c# encryption coldfusion-9


【解决方案1】:

感谢@Leigh 和他的回答here 我能够使用this C# implementation of UUEncoding 实现解决方案,如下所示:

public class PasswordHelper
{               
    public string DecryptAES(string encryptedString, string Key)
    {

        //First write to memory
        MemoryStream mmsStream = new MemoryStream();
        StreamWriter srwTemp = new StreamWriter(mmsStream);
        //sBuffer is my encodedtext.
        srwTemp.Write(encryptedString);
        srwTemp.Flush();
        mmsStream.Position = 0;

        MemoryStream outstream = new MemoryStream();

        //CallingUUDecode
        Codecs.UUDecode(mmsStream, outstream);         

        //Extract the bytes of each of the values
        byte[] input = outstream.ToArray();
        byte[] key = Convert.FromBase64String(Key);

        string decryptedText = null;

        using (RijndaelManaged algorithm = new RijndaelManaged())
        {

            // initialize settings to match those used by CF
            algorithm.Mode = CipherMode.ECB;
            algorithm.Padding = PaddingMode.PKCS7;
            algorithm.BlockSize = 128;
            algorithm.KeySize = 128;
            algorithm.Key = key;

            ICryptoTransform decryptor = algorithm.CreateDecryptor();

            using (MemoryStream msDecrypt = new MemoryStream(input))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        decryptedText = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
        return decryptedText;
    }

}

public static class Codecs
{
    static readonly byte[] UUEncMap = new byte[]
    {
      0x60, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
      0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
      0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
      0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
      0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
      0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
      0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
    };

    static readonly byte[] UUDecMap = new byte[]
    {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
      0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
      0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
      0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
      0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
      0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    public static void UUDecode(System.IO.Stream input, System.IO.Stream output)
    {
        if (input == null)
            throw new ArgumentNullException("input");

        if (output == null)
            throw new ArgumentNullException("output");

        long len = input.Length;
        if (len == 0)
            return;

        long didx = 0;
        int nextByte = input.ReadByte();
        while (nextByte >= 0)
        {
            // get line length (in number of encoded octets)
            int line_len = UUDecMap[nextByte];

            // ascii printable to 0-63 and 4-byte to 3-byte conversion
            long end = didx + line_len;
            byte A, B, C, D;
            if (end > 2)
            {
                while (didx < end - 2)
                {
                    A = UUDecMap[input.ReadByte()];
                    B = UUDecMap[input.ReadByte()];
                    C = UUDecMap[input.ReadByte()];
                    D = UUDecMap[input.ReadByte()];

                    output.WriteByte((byte)(((A << 2) & 255) | ((B >> 4) & 3)));
                    output.WriteByte((byte)(((B << 4) & 255) | ((C >> 2) & 15)));
                    output.WriteByte((byte)(((C << 6) & 255) | (D & 63)));
                    didx += 3;
                }
            }

            if (didx < end)
            {
                A = UUDecMap[input.ReadByte()];
                B = UUDecMap[input.ReadByte()];
                output.WriteByte((byte)(((A << 2) & 255) | ((B >> 4) & 3)));
                didx++;
            }

            if (didx < end)
            {
                B = UUDecMap[input.ReadByte()];
                C = UUDecMap[input.ReadByte()];
                output.WriteByte((byte)(((B << 4) & 255) | ((C >> 2) & 15)));
                didx++;
            }

            // skip padding
            do
            {
                nextByte = input.ReadByte();
            }
            while (nextByte >= 0 && nextByte != '\n' && nextByte != '\r');

            // skip end of line
            do
            {
                nextByte = input.ReadByte();
            }
            while (nextByte >= 0 && (nextByte == '\n' || nextByte == '\r'));
        }
    }

    public static void UUEncode(System.IO.Stream input, System.IO.Stream output)
    {
        if (input == null)
            throw new ArgumentNullException("input");

        if (output == null)
            throw new ArgumentNullException("output");

        long len = input.Length;
        if (len == 0)
            return;

        int sidx = 0;
        int line_len = 45;
        byte[] nl = Encoding.ASCII.GetBytes(Environment.NewLine);

        byte A, B, C;
        // split into lines, adding line-length and line terminator
        while (sidx + line_len < len)
        {
            // line length
            output.WriteByte(UUEncMap[line_len]);

            // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
            for (int end = sidx + line_len; sidx < end; sidx += 3)
            {
                A = (byte)input.ReadByte();
                B = (byte)input.ReadByte();
                C = (byte)input.ReadByte();

                output.WriteByte(UUEncMap[(A >> 2) & 63]);
                output.WriteByte(UUEncMap[(B >> 4) & 15 | (A << 4) & 63]);
                output.WriteByte(UUEncMap[(C >> 6) & 3 | (B << 2) & 63]);
                output.WriteByte(UUEncMap[C & 63]);
            }

            // line terminator
            for (int idx = 0; idx < nl.Length; idx++)
                output.WriteByte(nl[idx]);
        }

        // line length
        output.WriteByte(UUEncMap[len - sidx]);

        // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
        while (sidx + 2 < len)
        {
            A = (byte)input.ReadByte();
            B = (byte)input.ReadByte();
            C = (byte)input.ReadByte();

            output.WriteByte(UUEncMap[(A >> 2) & 63]);
            output.WriteByte(UUEncMap[(B >> 4) & 15 | (A << 4) & 63]);
            output.WriteByte(UUEncMap[(C >> 6) & 3 | (B << 2) & 63]);
            output.WriteByte(UUEncMap[C & 63]);
            sidx += 3;
        }

        if (sidx < len - 1)
        {
            A = (byte)input.ReadByte();
            B = (byte)input.ReadByte();

            output.WriteByte(UUEncMap[(A >> 2) & 63]);
            output.WriteByte(UUEncMap[(B >> 4) & 15 | (A << 4) & 63]);
            output.WriteByte(UUEncMap[(B << 2) & 63]);
            output.WriteByte(UUEncMap[0]);
        }
        else if (sidx < len)
        {
            A = (byte)input.ReadByte();

            output.WriteByte(UUEncMap[(A >> 2) & 63]);
            output.WriteByte(UUEncMap[(A << 4) & 63]);
            output.WriteByte(UUEncMap[0]);
            output.WriteByte(UUEncMap[0]);
        }

        // line terminator
        for (int idx = 0; idx < nl.Length; idx++)
            output.WriteByte(nl[idx]);
    }
}   

【讨论】:

  • geekswithblogs 的 KDE 编解码器部分有一些翻译错误。在文章的评论中注明。
猜你喜欢
  • 2021-06-10
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
相关资源
最近更新 更多