0

这是一个基本的安全应用程序,我只想加密和解密一些数据现在我的问题是在文本框中显示网络文本并取回该文本然后将其转换为字节数组并且在解密时我得到了错误

Error: ECB mode cipher length must be a multiple of blocksize 16
    at com.hurlant.crypto.symmetric::ECBMode/decrypt()

这是security.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            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 com.security.AesKeyGen;
            import com.security.EncryptionKeyGen;

            import mx.utils.Base64Decoder;
            public var mytest:String;
            private var kdata:ByteArray = new ByteArray();
            private var cleartextBytes:ByteArray = new ByteArray();
            private function encryptedSave():void
            {
                //create or retrieve the current shared object
                var so:SharedObject = SharedObject.getLocal("encryptedStore");

                //generate a random key

                var ec:EncryptionKeyGen = new EncryptionKeyGen();
                kdata=//some plaine text converted to byte array or some byte array
                trace(kdata);
                //store our data to encrypt into a ByteArray

                cleartextBytes.writeUTFBytes(plainText.text);

                var aes:ICipher = Crypto.getCipher("aes-ecb", kdata, Crypto.getPad("pkcs5"));

                aes.encrypt(cleartextBytes);
                trace("after Encription-------------"+cleartextBytes.length+cleartextBytes);

            }

            private function encryptedLoad():void
            {cleartextBytes=new ByteArray();
                cleartextBytes.writeUTFBytes(cyperText.text);
                                 trace(cleartextBytes +"-------------"+cleartextBytes.length);
                var aes:ICipher = Crypto.getCipher("aes-ecb", kdata, Crypto.getPad("pkcs5"));

                aes.decrypt(cleartextBytes);

            }
        ]]>
    </fx:Script>

    <mx:Panel width="600" height="400" title="FlexEncryptionExample1">
        <mx:Form width="100%">
            <mx:FormHeading label="Stored WebService credentials"/>
            <mx:FormItem label="User: " width="100%">
                <mx:TextInput id="plainText" width="100%"/>
            </mx:FormItem>
            <mx:FormItem label="CyperText: " width="100%">
                <mx:TextInput id="cyperText" width="100%"/>
            </mx:FormItem>
            <mx:FormItem label="plainText2: " width="100%">
                <mx:TextInput id="plainText2"  width="100%"/>
            </mx:FormItem>
            <s:Label text="" id="encryptedText" visible="false"/>
        </mx:Form>
        <mx:HBox width="100%" horizontalAlign="center" verticalAlign="bottom">
            <mx:Button label="Save" click="encryptedSave()"/>
            <mx:Button label="Load" click="encryptedLoad()"/>
        </mx:HBox>
    </mx:Panel>
</s:Application>

任何人都可以帮助我谢谢......

4

1 回答 1

0

在转换为字符串时进行 Aes 加密后,我添加了以下代码,最后一切都正确了......

            aes.encrypt(cleartextBytes);
            trace("after Encription-------------"+cleartextBytes.length+cleartextBytes);
            cleartextBytes.position=0;
            var myEncoder:Base64Encoder = new Base64Encoder();

            myEncoder.encodeBytes(cleartextBytes);

            cyperText.text=myEncoder.toString();

            trace("Encoded string length"+cyperText.text.length);
于 2012-06-19T10:10:17.773 回答