目前正在编写我自己的 AMF TcpSocketServer。到目前为止一切正常,我可以发送和接收对象,并且我使用了一些序列化/反序列化代码。现在我开始研究加密代码,我对这些东西不太熟悉。
- 我使用 bytes ,DES-CBC 是加密这些东西的好方法吗?还是有其他更高效/更安全的方式来发送我的数据?请注意,性能是必须的:)。
- 当我调用:指定解密器的ReadAmf3Object时,我得到一个:当我读出未指定 Amf3TypeCode 的第一个字节时,我的 ReadAmf3Object 函数抛出的 InvalidOperationException(我相信它们的范围从 0 到 16(Bool,String,Int,DateTime, ETC) )。我的类型代码从 97 到 254 不等?有谁知道出了什么问题?我认为这与加密部分有关。由于解串器在没有加密的情况下工作正常。我正在使用正确的填充/模式/键?
我使用:http ://code.google.com/p/as3crypto/作为 as3 加密/解密库。我写了一个异步 tcp 服务器,滥用了线程池;)
无论如何这里有一些代码:
C#密码器初始化代码
System.Security.Cryptography.DESCryptoServiceProvider crypter = new DESCryptoServiceProvider();
crypter.Padding = PaddingMode.Zeros;
crypter.Mode = CipherMode.CBC;
crypter.Key = Encoding.ASCII.GetBytes("TESTTEST");
AS3
private static var _KEY:ByteArray = Hex.toArray(Hex.fromString("TESTTEST"));
private static var _TYPE:String = "des-cbc";
public static function encrypt(array:ByteArray):ByteArray
{
var pad:IPad = new NullPad;
var mode:ICipher = Crypto.getCipher(_TYPE, _KEY, pad);
pad.setBlockSize(mode.getBlockSize());
mode.encrypt(array);
return array;
}
public static function decrypt(array:ByteArray):ByteArray
{
var pad:IPad = new NullPad;
var mode:ICipher = Crypto.getCipher(_TYPE, _KEY, pad);
pad.setBlockSize(mode.getBlockSize());
mode.decrypt(array);
return array;
}
C#读取/反序列化/解密代码
public override object Read(int length)
{
object d;
using (MemoryStream stream = new MemoryStream())
{
stream.Write(this._readBuffer, 0, length);
stream.Position = 0;
if (this.Decrypter != null)
{
using (CryptoStream c = new CryptoStream(stream, this.Decrypter, CryptoStreamMode.Read))
using (AmfReader reader = new AmfReader(c))
{
d = reader.ReadAmf3Object();
}
}
else
{
using (AmfReader reader = new AmfReader(stream))
{
d = reader.ReadAmf3Object();
}
}
}
return d;
}