从 Action Script 到 C# 的 Rijndael 加密

发布于 2024-08-29 09:57:37 字数 304 浏览 5 评论 0原文

我正在尝试在 Action Script 和 C# 之间共享加密

中解密以下消息daf6e25b61ab1a281

我的任务是在 C# f1ca22a365ba54c005c3eb599d84b19c354d26dcf475ab4be775b991ac97884791017b12471000def05bb77bfe9c3a97d44ef78c9449f12

它使用 Rijndael 加密,ECB 模式(电子密码本),密钥: Pas5pr@se ,128 位密钥大小和块大小。

我的问题是我似乎无法做到这一点,有人可以帮助我吗?

I am trying to share encryption between Action Script and C#

My task is to decrypt the following message within C#

f1ca22a365ba54c005c3eb599d84b19c354d26dcf475ab4be775b991ac97884791017b12471000def05bb77bfe9c3a97d44ef78c9449f12daf6e25b61ab1a281

It uses Rijndael encyption , ECB mode (electronic code book), Key: Pas5pr@se , 128 bit key size and block size.

The problem I have is I can't seem to do it, anyone help me on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城空念 2024-09-05 09:57:37

这是 Rijndael 加密的一种实现,我的一个网站目前正在使用它。看看这是否有效:

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

namespace CMS.Core.Domain
{
    /// <summary>
    /// Summary description for EncryptionManager
    /// </summary>
    public static class EncryptionManager
    {
        public static string EncryptRijndael(string value, string encryptionKey) {
            try {
                var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
                var rijndael = new RijndaelManaged {
                    BlockSize = 128,
                    IV = key,
                    KeySize = 128,
                    Key = key
                };

                var transform = rijndael.CreateEncryptor();
                using (var ms = new MemoryStream()) {
                    using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) {
                        byte[] buffer = Encoding.UTF8.GetBytes(value);

                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    ms.Close();
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
            catch {
                return string.Empty;
            }
        }

        public static string DecryptRijndael(string value, string encryptionKey)
        {
            try
            {
                var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
                var rijndael = new RijndaelManaged
                                               {
                                                   BlockSize = 128,
                                                   IV = key,
                                                   KeySize = 128,
                                                   Key = key
                                               };

                var buffer = Convert.FromBase64String(value);
                var transform = rijndael.CreateDecryptor();
                string decrypted;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                    {
                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                        decrypted = Encoding.UTF8.GetString(ms.ToArray());
                        cs.Close();
                    }
                    ms.Close();
                }

                return decrypted;
            }
            catch
            {
                return null;
            }
        }
    }
}

更新

我刚刚在您的输入中注意到的一件事是您的加密密钥只有 9 个字符,而我上面的代码需要 16 个字符的密钥。我不确定这是否是 Rijndael 加密算法的硬性要求,但上述代码不适用于不完全是 16 个字符的加密密钥。

This is an implementation of Rijndael Encryption which one of my websites is currently using. See if this does the trick:

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

namespace CMS.Core.Domain
{
    /// <summary>
    /// Summary description for EncryptionManager
    /// </summary>
    public static class EncryptionManager
    {
        public static string EncryptRijndael(string value, string encryptionKey) {
            try {
                var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
                var rijndael = new RijndaelManaged {
                    BlockSize = 128,
                    IV = key,
                    KeySize = 128,
                    Key = key
                };

                var transform = rijndael.CreateEncryptor();
                using (var ms = new MemoryStream()) {
                    using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) {
                        byte[] buffer = Encoding.UTF8.GetBytes(value);

                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    ms.Close();
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
            catch {
                return string.Empty;
            }
        }

        public static string DecryptRijndael(string value, string encryptionKey)
        {
            try
            {
                var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
                var rijndael = new RijndaelManaged
                                               {
                                                   BlockSize = 128,
                                                   IV = key,
                                                   KeySize = 128,
                                                   Key = key
                                               };

                var buffer = Convert.FromBase64String(value);
                var transform = rijndael.CreateDecryptor();
                string decrypted;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                    {
                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                        decrypted = Encoding.UTF8.GetString(ms.ToArray());
                        cs.Close();
                    }
                    ms.Close();
                }

                return decrypted;
            }
            catch
            {
                return null;
            }
        }
    }
}

Update

One thing I just noticed with your input is that your encryption key is only 9 characters and my code above requires a 16 character key. I am not sure if this is a hard requirement of the Rijndael encryption algorithm, but the above code will not work with an encryption key that is not exactly 16 characters.

一曲琵琶半遮面シ 2024-09-05 09:57:37

我遇到了这个链接,它解决了 AES Rijndael 加密的 C# 和 ActionScript

http://ryoushin.com/cmerighi/en-us/42,2007-03-02/AES-Rijndael_with_ActionScript_and_ASP_Net.aspx

I came across this link which addresses both C# and ActionScript for the AES Rijndael encryption

http://ryoushin.com/cmerighi/en-us/42,2007-03-02/AES-Rijndael_with_ActionScript_and_ASP_Net.aspx

蓝眼睛不忧郁 2024-09-05 09:57:37

您可以为 Rijndael 尝试 此包装,因为它可能是 IV 或密码填充的问题(我很想知道它是否不起作用)

You could try this wrapper for Rijndael as it may be an issue with the IV or the passphrase padding (I'd be interested to know if it doesn't work)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文