我曾尝试使用Blowfish (CBC)技术对PHP到Flash的文本进行加密/解密。经过几个小时的调查和研究,我了解到AS3Crypto可以用于解密河豚(CBC模式)。在一个简单的例子中,我使用Mcrypt (A Library for PHP)来加密文本:
const CYPHER = 'blowfish';
const MODE = 'cbc';
const KEY = '12345';
public function encrypt($plaintext)
{
$td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, self::KEY, $iv);
$crypttext = mcrypt_generic($td, $plaintext);
mcrypt_generic_deinit($td);
return $iv.$crypttext;
}然后,我可以通过使用Base64对输出进行编码来传输输出。例如,如果原始文本为(不带引号) "stackoverflow“,键为"123456",则输出将为(base64):
MUXl8mBS9OsvxTbLAiCrAMp851L8vVD0
到目前为止没有任何问题。现在,当我将这个编码的文本转换为flash时,我可以毫不费力地获取它。您可以尝试转到http://crypto.hurlant.com/demo/CryptoDemo.swf,然后选择“密钥”选项卡,并选择加密为"Blowfish",模式为"CBC",填充为“无”,并勾选"Prepend IV to cipher text“选项。之后,您可以使用密钥成功解密上面的文本,并再次获得"stackoverflow“文本。
所以,到目前为止,我知道可以从Mcrypt转换为AS3Crypt,然后我尝试在闪存中使用AS3Crypto库(你可以从http://code.google.com/p/as3crypto/获得它)。
我制作了一个新的actionscript文件,其中包含以下内容来测试加密是否相同(由于主要问题,我不知道如何解密它):
package
{
import com.hurlant.crypto.Crypto;
import com.hurlant.util.Hex;
import com.hurlant.crypto.hash.HMAC;
import com.hurlant.crypto.hash.IHash;
import com.hurlant.crypto.hash.MD5;
import com.hurlant.crypto.hash.SHA1;
import com.hurlant.crypto.hash.SHA224;
import com.hurlant.crypto.hash.SHA256;
import com.hurlant.crypto.prng.ARC4;
import com.hurlant.crypto.symmetric.AESKey;
import com.hurlant.crypto.symmetric.BlowFishKey;
import com.hurlant.crypto.symmetric.CBCMode;
import com.hurlant.crypto.symmetric.CFB8Mode;
import com.hurlant.crypto.symmetric.CFBMode;
import com.hurlant.crypto.symmetric.CTRMode;
import com.hurlant.crypto.symmetric.DESKey;
import com.hurlant.crypto.symmetric.ECBMode;
import com.hurlant.crypto.symmetric.ICipher;
import com.hurlant.crypto.symmetric.IMode;
import com.hurlant.crypto.symmetric.IPad;
import com.hurlant.crypto.symmetric.ISymmetricKey;
import com.hurlant.crypto.symmetric.IVMode;
import com.hurlant.crypto.symmetric.NullPad;
import com.hurlant.crypto.symmetric.OFBMode;
import com.hurlant.crypto.symmetric.PKCS5;
import com.hurlant.crypto.symmetric.SimpleIVMode;
import com.hurlant.crypto.symmetric.TripleDESKey;
import com.hurlant.crypto.symmetric.XTeaKey;
import flash.utils.ByteArray;
import com.hurlant.crypto.rsa.RSAKey;
import com.hurlant.util.Base64;
public class BlowFish
{
/**
* Encrypts a string.
* @param text The text string to encrypt.
* @param key A cipher key to encrypt the text with.
*/
/**
* Decrypts an encrypted string.
* @param text The text string to decrypt.
* @param key The key used while originally encrypting the text.
*/
static public function encrypt( s :String, k :String ) :String
{
var key :ByteArray = Hex.toArray(k);
var data :ByteArray = Hex.toArray(Hex.fromString(s));
var pad :IPad = new NullPad();
var cipher :ICipher = Crypto.getCipher("blowfish-cbc", key, pad);
pad.setBlockSize(cipher.getBlockSize());
cipher.encrypt(data);
var result :String = Hex.fromArray(data);
var ivmode :IVMode = cipher as IVMode;
var iv :String = Hex.fromArray(ivmode.IV);
return Base64.encodeByteArray(Hex.toArray(Hex.fromArray(ivmode.IV) + Hex.fromArray(data)));
}
}
}我使用了下面的代码来得到结果:
import BlowFish;
var $key:String = "123456";
var $encryption:String = BlowFish.encrypt("stackoverflow", $key);
trace( $encryption );问题是我不能将以下输出匹配在一起。我对actionscript一无所知,所以你肯定会发现里面有很多错误。
我将真的很感激任何解释和解决方案的例子,找出如何成功解密在闪存中使用AS3Crypto的加密文本。
谢谢。
发布于 2012-05-11 16:57:06
希望这能对你有所帮助:
public static function encryptString(encString : String = "") : String
{
var kdata : ByteArray = Hex.toArray(Hex.fromString(k))
var _method : String = "simple-blowfish-ecb";
var pad : IPad = new NullPad;
var crypt : ICipher = Crypto.getCipher(_method, kdata, pad);
var data : ByteArray = Hex.toArray(Hex.fromString(encString));
pad.setBlockSize(crypt.getBlockSize());
crypt.encrypt(data);
encString = Base64.encodeByteArray(data);
return encString;
}https://stackoverflow.com/questions/9750637
复制相似问题