2016-10-13 00:16:48 点击量:14 标签: 收藏本文
I want tp encrypt and decrypt string, with defined salt. But the result must be same if the code run in java and adobe flex. The main goal is: the app in adobe flex will be generate a string that can be decrypt in server using java.
I use this flex library http://crypto.hurlant.com/demo/
Try to 'Secret Key' Tab. I want to use AES Encryption, 'CBC' or 'PKCS5'.
- var k:String = "1234567890123456";
- var kdata:ByteArray = Hex.toArray(k);
- var txt:String = "hello";
- var data:ByteArray = Hex.toArray(Hex.fromString(txt));;
- var name:String = "simple-aes-cbc";
- var pad:IPad =new PKCS5();
- var mode:ICipher = Crypto.getCipher(name, kdata, pad);
- pad.setBlockSize(mode.getBlockSize());
- mode.encrypt(data);
- encrypted.text=Hex.fromArray(data);
- trace(Hex.fromArray(data));
And here is the code in java
- String plaintext = "hello";
- String key = "1234567890123456";
- SecretKey keyspec = new SecretKeySpec(key.getBytes(), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE,keyspec);
- byte[] encrypted = cipher.doFinal(plaintext.getBytes());
- BASE64Encoder base64 = new BASE64Encoder();
- String encodedString = base64.encode(encrypted);
- System.out.println(encodedString);
Why the result is not same? Can you guys provide the sample with the same result both of java and flex (encrypt and decrypt)? And if I want to change the paramater, for example, from cbc to ebc, which line that need to be changed?
Thanks!
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室