我正在使用 Ruby 尝试加密一个字符串,该字符串将存储在数据库中并由 Flash/Actionscript 应用程序读取/解密。
该应用程序使用这种河豚实现。
我已经尝试过创建兼容字符串的openssl和crypt/blowfish方法。两者都不匹配,也不匹配 Flash 应用程序所期望的。
我从哪里开始让它发挥作用?
irb(main):001:0> require 'openssl'
=> true
irb(main):002:0> require 'crypt/blowfish'
=> true
irb(main):007:0> require 'base64'
=> true
irb(main):003:0> key = "foo"
=> "foo"
irb(main):004:0> plain = "some string"
=> "some string"
irb(main):005:0> blowfish = Crypt::Blowfish.new(key)
=>
irb(main):006:0> enc = blowfish.encrypt_block(plain)
=> "\xF5\xAFB\x12=\xB9\xDB\f"
irb(main):008:0> Base64.encode64(enc)
=> "9a9CEj252ww=\n"
# Now, openssl version
irb(main):009:0> cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').send(:encrypt)
=> #<OpenSSL::Cipher::Cipher:0x00000000f26430>
irb(main):010:0> cipher.key = Digest::SHA256.digest(key)
=> ",&\xB4kh\xFF\xC6\x8F\xF9\x9BE<\x1D0A4\x13B-pd\x83\xBF\xA0\xF9\x8A^\x88bf\xE7\xAE"
irb(main):011:0> enc = cipher.update(plain) << cipher.final
=> "m<\xDB\xC1B\x02p\xB0\xD6\xD0\xA4\xE8XyY\x99"
irb(main):012:0> Base64.encode64(enc)
=> "bTzbwUICcLDW0KToWHlZmQ==\n"
编辑
这是我们在 AS3 中所做的(使用上面提到的河豚代码):
import com.lassieadventurestudio.Blowfish;
import fl.controls.Button;
import fl.controls.TextInput;
import flash.events.MouseEvent;
var $key:String = "foo";
BTN_Submit.label = "Encrypt";
BTN_Submit.addEventListener(MouseEvent.CLICK, onSubmit);
function onSubmit(event:MouseEvent):void
{
trace("INP_Pass.text, ", INP_Pass.text);
var $encryption:String = Blowfish.encrypt(INP_Pass.text, $key);
TXT_Output.text = $encryption;
}
BTN_Decrypt.label = "Decrypt";
BTN_Decrypt.addEventListener(MouseEvent.CLICK, decrypt);
function decrypt(event:MouseEvent):void
{
TXT_Output2.text = Blowfish.decrypt(TXT_Output.text, $key);
}