Encryption in C # and Decryption in Flex

I need to decrypt some data in Flex that is encrypted in C # and written to a file. I decided to use blowfish for simplicity, using the As3 crypto As3 library and the Bruce Schneier C # library.

AS3 link as3crypto

Bruce Schneier c # blowfish link

I can get a short string to encrypt in C # and decrypt in Flex just fine however longer strings just don't work and I don't know what is missing?

FROM#:

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 examples

works

encrypt string = "watson?"

C # produces: 1514ea36fecfd5f5

AS3 produces: 1514ea36fecfd5f5

does not work

encrypt string = "whats up watson?"

C # produces: 3ea9808a4b9f74aaa8e54fe682947673

AS3 produces: 3ea9808a4b9f74aa20776174736f6e3f

which is very similar but does not match

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

whats up? `r ???

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

whats up¨åO 悔 vs

+3


source to share


3 answers


AS3 code seems to be wrong. Working code example:



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


source


Answer to "how to decrypt the string after":



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

      

+1


source


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 this class which might solve your problem.

0


source







All Articles