1

我需要加密 ASP.net 中的一些文件并在使用 Action Script 3 构建的 flash 应用程序中解密它们。

AS3 开发人员发现了一个名为 AS3crypto 的库,这对于 AS3 来说似乎是一个不错的库。这个想法是使用相同的密钥加密和解密。对称加密?

但我正在努力寻找可以使用相同加密算法的 .Net 等价物。

我已经尝试了来自 4guysfromrolla 博客的 RC4 示例,它对我来说太慢了。我已经在这个例子中尝试过 AES(http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged (v=vs.100).aspx ),它在.Net 上效果很好,但我可以似乎没有使用 AS3crypto 解密以取回相同的文件。AS3crypto 似乎不喜欢使用 IV 进行解密。我只能提供一把钥匙。

到目前为止,我迷路了。如何在 .Net 中加密文件并在 AS3 中解密以获取相同的文件?

4

1 回答 1

1

Notice: use 16 char length for both Key and IV, ex: Key: 1234567890123456 and IV: 9876543210654321

Here is C# code

    public byte[] Encrypt(byte[] someData, string KEY, string IV)
    {
        //preparing
        byte[] keyBytes = Encoding.UTF8.GetBytes(KEY); 
        byte[] ivBytes = Encoding.UTF8.GetBytes(IV); 


        //here goes encryption
        RijndaelManaged rijndaelManaged = new RijndaelManaged();
        rijndaelManaged.Key = keyBytes;
        rijndaelManaged.IV = ivBytes;
        rijndaelManaged.BlockSize = 128;
        rijndaelManaged.Mode = CipherMode.CBC;
        ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(rijndaelManaged.Key, rijndaelManaged.IV);

        byte[] result = null;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
            {
                cryptoStream.Write(someData, 0, someData.Length);
                cryptoStream.FlushFinalBlock();
                result = memoryStream.ToArray();
            }
        }

        return result;


    }

And here is AS3 code using AS3Crypto library

    private function decrypt(input:ByteArray, decrKey:String, decrIV:String):ByteArray
    {

        var key:ByteArray = Hex.toArray(Hex.fromString(decrKey));
        var pad:IPad = new NullPad();
        var aes:ICipher = Crypto.getCipher("aes-cbc", key, pad);
        var ivmode:IVMode = aes as IVMode;
        ivmode.IV = Hex.toArray(Hex.fromString(decrIV));
        aes.decrypt(input);

        return input;
    }
于 2013-11-10T16:32:28.360 回答