我需要在Flex中解密一些用C#加密并写入文件的数据。 为了简单起见,我使用as3crypto As3库和Bruce Schneier C#库来确定河豚。
Bruce Schneier C# blowfish link
我可以获得一个短字符串来加密C#并在Flex中解密 但是较长的琴弦不能产生结果,我不知道我错过了什么?
C#:
string reportstring = "watson?";
BlowFish b = new BlowFish("04B915BA43FEB5B6");
string cipherText = b.Encrypt_ECB(reportstring);
String plainText = b.Decrypt_ECB(cipherText);
AS3:
var txt:String = "watson?";
var key:ByteArray = Hex.toArray("04B915BA43FEB5B6");
var blowfish:BlowFishKey = new BlowFishKey(key);
var dataBytes:ByteArray = new ByteArray();
dataBytes=Hex.toArray(Hex.fromString(txt));
blowfish.encrypt(dataBytes);
blowfish.decrypt(dataBytes);
更新,部分样本
工作
加密string =“watson?”
C#产生:1514ea36fecfd5f5
AS3 产生:1514ea36fecfd5f5
不工作
加密字符串=“wats up watson?”
C#产生:3ea9808a4b9f74aaa8e54fe682947673
AS3 产生:3ea9808a4b9f74aa20776174736f6e3f
非常相似但不匹配
如果我用C#解密AS3密码,我得到:
怎么了?`r ???
如果我在AS3中解密C#密码,我得到:
什么是悔vs
答案 0 :(得分:2)
AS3代码似乎不正确。工作示例代码:
import com.hurlant.util.Hex;
import com.hurlant.util.Base64;
import com.hurlant.crypto.Crypto;
import flash.utils.ByteArray;
import com.hurlant.crypto.symmetric.IPad;
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.crypto.symmetric.NullPad;
import com.hurlant.crypto.symmetric.BlowFishKey;
function encrypt($text:String, $cryptKey:ByteArray):String
{
var iPad:IPad = new NullPad();
var crypt = Crypto.getCipher('blowfish-ecb',$cryptKey,iPad);
var cryptText:ByteArray = new ByteArray();
cryptText.writeUTFBytes( $text );
crypt.encrypt( cryptText );
trace( Hex.fromArray( cryptText ) );
return null;
}
var txt:String = "whats up watson?";
var key:ByteArray = Hex.toArray("04B915BA43FEB5B6");
encrypt(txt, key);
答案 1 :(得分:1)
回答“我之后如何解密字符串”:
var encodedtxt:String = Hex.fromArray(cryptText);
cryptText = Hex.toArray(encodedtxt);
crypt.decrypt(cryptText);
答案 2 :(得分:0)
package
{
import com.hurlant.crypto.Crypto;
import com.hurlant.crypto.prng.Random;
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.util.Base64;
import com.hurlant.util.Hex;
import flash.utils.ByteArray;
import mx.utils.Base64Decoder;
import mx.utils.Base64Encoder;
public class EncryptionManager
{
public function EncryptionManager()
{
}
public function enCrypt(data:String, keyStr:String):String
{
var key:ByteArray;
var fileBytes:ByteArray = Hex.toArray(Hex.fromString(data));
key = Hex.toArray(Hex.fromString(keyStr));
var aes:ICipher = Crypto.getCipher("blowfish-ecb", key, Crypto.getPad("pkcs5"));
aes.encrypt(fileBytes);
var enc:Base64Encoder = new Base64Encoder();
enc.encodeBytes(fileBytes);
var result:String = enc.flush();
return result;
}
public function deCrypt(data:String, keyStr:String):String
{
var key:ByteArray;
var dec:Base64Decoder = new Base64Decoder();
dec.decode(data);
var fileBytes:ByteArray = dec.toByteArray();
key = Hex.toArray(Hex.fromString(keyStr));
var aes:ICipher = Crypto.getCipher("blowfish-ecb", key, Crypto.getPad("pkcs5"));
aes.decrypt(fileBytes);
return fileBytes.toString();
}
}
}
尝试这个可以解决问题的课程。