Encrypt in C# and decrypt in Flex

蓝咒 提交于 2019-12-10 10:23:44

问题


I need to decrypt some data in Flex that is encrypted in C# and written to a file. I settled on blowfish for simplicity's sake using the as3crypto As3 library and Bruce Schneier C# library.

AS3 as3crypto link

Bruce Schneier C# blowfish link

I can get a short string to encrypt in C# and decrypt in Flex fine however longer strings just fail to produce results and I do not know what I am missing?

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

Update, some samples

working

encrypt string = "watson?"

C# produces: 1514ea36fecfd5f5

AS3 produces: 1514ea36fecfd5f5

not working

encrypt string = "whats up watson?"

C# produces: 3ea9808a4b9f74aaa8e54fe682947673

AS3 produces: 3ea9808a4b9f74aa20776174736f6e3f

which is very similar but not matching

if I decrypt the AS3 cipher in C# I get :

whats up?`r???

if I decrypt the C# cipher in AS3 I get :

whats up¨åO悔vs


回答1:


The AS3 code seems to be incorrect. Working example code:

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



回答2:


Answer to "how do I decrypt the string afterwards":

var encodedtxt:String = Hex.fromArray(cryptText);
cryptText = Hex.toArray(encodedtxt);
crypt.decrypt(cryptText);



回答3:


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

    }
}

Try out this Class that might solve your problem.



来源:https://stackoverflow.com/questions/9993707/encrypt-in-c-sharp-and-decrypt-in-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!