【问题标题】:Blowfish decryption / Action Script 3 (AS3Crypto)河豚解密 / 动作脚本 3 (AS3Crypto)
【发布时间】:2012-04-02 18:50:57
【问题描述】:

我尝试使用 Blowfish (CBC) 技术将文本从 PHP 加密/解密到 Flash。 经过几个小时的调查研究,我知道AS3Crypto可以用来解密Blowfish(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”,填充为“无”,并勾选“将 IV 添加到密文”选项。之后,就可以成功解密上面的文本了,使用密钥,再次得到“stackoverflow”文本。

所以,到目前为止,我知道它可以从 Mcrypt 转换为 AS3Crypt,然后我尝试在 flash 中使用 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 成功解密闪存中的加密文本。

谢谢。

【问题讨论】:

  • 你应该对所有的二进制数据进行编码,不仅仅是纯文本,还有密钥。密钥应该是与河豚兼容大小的八位字节字符串,而不是一些可变长度的通用文本字符串,其中的编码是未知的。
  • 您似乎一次使用纯文本,另一次使用十六进制。
  • @owlstead 到目前为止,我对问题的 PHP 方面没有任何问题,在这方面,我只是将输出作为 base64,所以我怎样才能让这个 base64 编码的文本在 flash 中解码?
  • 一个快速的谷歌出现了这个:help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/… ?但我更喜欢加密而不是闪存......
  • @owlstead 我知道如何在 flash 中编码/解码 base64,但我只是使用 base64 作为在传输时保护真实数据的一种方式。

标签: php flash encryption blowfish as3crypto


【解决方案1】:

希望这有帮助:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多