在.Net中加密并在AS3中解密 [英] Encrypt in .Net and decrypt in AS3
问题描述
我需要加密ASP.net中的某些文件,并在使用Action Script 3构建的Flash应用程序中对其进行解密.
I need to encrypt some files in ASP.net and decrypt them in a flash application built with Action Script 3.
AS3开发人员发现了一个库调用AS3crypto,对于AS3来说似乎是一个不错的库.这个想法是使用相同的密钥进行加密和解密.对称加密?
AS3 developer found a lib call AS3crypto which seems like a good one for AS3. The idea is encrypt and decrypt using same key. Symmetrical Encryption?
但是我很难找到等效的.Net,它将使用相同的算法进行加密.
But I am struggling to find .Net equivalent that would use same algorithm for encryption.
我尝试了4guysfromrolla博客中的RC4示例,该示例对我来说太慢了. 我已经在此示例上尝试过AES(
I have tried RC4 example from 4guysfromrolla blog which works too slow for me. I have tried AES on this example (http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(v=vs.100).aspx) which works great on .Net but I can't seem to decrypt using AS3crypto to get the same file back. AS3crypto doesn't seem to like to have IV for decryption. I can only supply one key.
到目前为止,我迷路了.如何在.Net中加密文件,然后在AS3中解密该文件以恢复相同文件?
So far I am lost. How can I encrypt a file in .Net and decrypt it back in AS3 to get the same file back?
推荐答案
注意:密钥和IV均使用16个字符的长度,例如:密钥:1234567890123456和IV:9876543210654321
Notice: use 16 char length for both Key and IV, ex: Key: 1234567890123456 and IV: 9876543210654321
这是C#代码
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;
}
这是使用AS3Crypto库的AS3代码
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;
}
这篇关于在.Net中加密并在AS3中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!