Actionscript DES encryption and decryption (AES, other encryption)

Source: Internet
Author: User

I have not found any good DES encryption code for a long time.

It is no wonder that a blogger said that he had not found himself for a long time. Http://www.iteye.com/topic/422575

But it seems that he said that the encrypted plaintext will go wrong, so I did not dare to try it out.

In addition, I downloaded one from Tiandi, and found that the function was still possible. It seems that I switched the js version. However, the encryption speed is very low, which should be a problem of implementation. It is dozens of times slower than the AES encryption code I found on a foreign blog.

(!!! In addition, I finally found the C written by the foreign guy.RyPto Library(Http://code.google.com/p/as3crypto/) the best, although it is very troublesome to use. See the end of the article .)

However, in any case, it is commendable to share the code here.

Package crypto {import flash. display. sprite; import flash. events. eventDispatcher; import flash. events. progressEvent; import flash. utils. byteArray;/*** Des encryption ** @ author winning xiaoba */public class DES {// IP address conversion, used to disrupt the 64-bit plaintext private const IP: Array = [, 50,,, 35, 47,39, 31,23, 15,7]; // IP-1 transform, user output 64-bit ciphertext private const IP_1: Array =,, ,]; // PC_1 transform private const PC_1: Array =,, 35, 54, 53, 29, 4]; // PC_2 transform private const PC_2: Array =, 3, 19, 50,36, 29,32]; // E transformation, expands 32 bits to 48 bits private const E: Array =, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31 ,]; // P transform, compress the expanded data back to 32-bit private static var P: Array =,, 10,]; private const sbox: Array = [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7], [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8], [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0], [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13], [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10], [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5], [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15], [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9], [[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8], [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1], [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7], [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12], [[1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12], [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9], [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4], [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14], [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9], [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6], [4, 2, 1, 11, 1 0, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14], [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3], [[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11], [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8], [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6], [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13], [[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1], [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6], [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2], [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12], [[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7], [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2], [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8], [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]; public const leftMoveStep: Array = [,]; Private var key: ByteArray; private var key_ I: Array; public function DES (key: ByteArray) {this. key = key; if (this. key. length! = 8) {throw new Error ("key Error");} else {var keyArray: Array = new Array (); for (var I: int = 0; I <8; I ++) {var ts: Array = this. d2B (this. key [I]); for (var j: int = 0; j <8; j ++) {keyArray. push (ts [j]) ;}// trace (keyArray); this. key_ I = this. getKeys (keyArray); for (var m: int = 0; m <key_ I .length; m ++) {// trace ("key" + m + ": "+ key_ I [m]);} // trace (this. key_ I) // trace (this. key_ I) ;}} public function encrypt (source: ByteArray): ByteArray {v Ar ret: ByteArray = new ByteArray (); var filledSource: ByteArray = this. fullFill64 (source); var a: Number = filledSource. length/8; var B: Number = filledSource. length % 8; for (var I: int = 0; I <a; I ++) {var block: Array = new Array (); for (var m: int = 0; m <8; m ++) {var bytes: Array = this. d2B (filledSource [I * 8 + m]); for (var n: int = 0; n <8; n ++) {block. push (bytes [n]);} var encryptedBlock: Array = this. encryptUnit (block); for (v Ar k: int = 0; k <8; k ++) {var encryptedByte: Array = new Array (); for (var l: int = 0; l <8; l ++) {encryptedByte. push (encryptedBlock [k * 8 + l]);} var encryptedInt: int = this. b2D (encryptedByte); ret. writeByte (encryptedInt) ;}} return ret;} public function decrypt (source: ByteArray): ByteArray {var ret: ByteArray = new ByteArray (); var a: Number = source. length/8; var B: Number = source. length % 8; for (var I: int = 0; I <a; I ++) {var Block: Array = new Array (); for (var m: int = 0; m <8; m ++) {var bytes: Array = this. d2B (source [I * 8 + m]); for (var n: int = 0; n <8; n ++) {block. push (bytes [n]);} var decryptedBlock: Array = this. decriptUnit (block); for (var k: int = 0; k <8; k ++) {var decryptedByte: Array = new Array (); for (var l: int = 0; l <8; l ++) {decryptedByte. push (decryptedBlock [k * 8 + l]);} var decryptedInt: int = this. b2D (decryptedByte); ret. writeByte (decrypte DInt) ;}} ret = this. filterFilled (ret); return ret;} private function encryptUnit (source: Array): Array {var ret: Array; var IPRes: Array = this. IPConvort (source); // trace ("IPRes" + IPRes); for (var I: int = 0; I <16; I ++) {this. f (IPRes, I, 1); // trace ("the" + I + "loop"); // trace (IPRes);} ret = this. IP_1Convert (IPRes); return ret;} private function decriptUnit (source: Array): Array {var ret: Array; var IPRes: Array = this. IPConvort (Source); for (var I: int = 15; I> = 0; I --) {this. f (IPRes, I, 0);} ret = this. IP_1Convert (IPRes); return ret;} public function IPConvort (source: Array): Array {var ret: Array = new Array (64); for (var I: int = 0; I <ret. length; I ++) {ret [I] = source [IP [I]-1];} return ret;} public function IP_1Convert (source: Array ): array {var ret: Array = new Array (64); for (var I: int = 0; I <64; I ++) {ret [I] = source [IP_1 [I]-1];} return ret;} publi C function PC_1Convert (source: Array): Array {var ret: Array = new Array (56); for (var I: int = 0; I <56; I ++) {ret [I] = source [PC_1 [I]-1];} return ret;} public function B2D (source: Array): int {var ret: int = 0; for (var I: int = 0; I <source. length; I ++) {ret + = source [source. length-1-i] * Math. pow (2, I);} return ret;} public function D2B (source: int): Array {var ret: Array = new Array (); var m: int = source; var ts: int = 0; var n: I Nt = 0; do {ts = m; m = Math. floor (m/2); n = ts-m * 2; ret. push (n) ;}while (m! = 0); for (m = ret. length; m <8; m ++) {ret. push (0);} m = Math. floor (ret. length/2); for (n = 0; n <m; n ++) {ts = ret [n]; ret [n] = ret [ret. length-1-n]; ret [ret. length-1-n] = ts;} return ret;} public function getKeys (source: Array): Array {var ret: Array = new Array (); var keyByte: array = PC_1Convert (source); for (var I: int = 0; I <16; I ++) {leftMove (keyByte, leftMoveStep [I]); var keyI: array = new Array (48) for (var j: int = 0; j <48; j ++) {keyI [j] = keyByte [PC_2 [j]-1];} ret. push (keyI);} return ret;} public function leftMove (source: Array, offset: int): void {var ret: Array = new Array (56); var c0: array = new Array (28); var d0: Array = new Array (28); var c1: Array = new Array (28); var d1: array = new Array (28); for (var I: int = 0; I <28; I ++) {c0 [I] = source [I]; d0 [I] = source [I + 28];} if (offset = 1) {for (var I: int = 0; I <27; I ++) {c1 [I] = c0 [I + 1]; d1 [I] = d0 [I + 1];} c1 [27] = c0 [0]; d1 [27] = d0 [0];} else if (offset = 2) {for (var I: int = 0; I <26; I ++) {c1 [I] = c0 [I + 2]; d1 [I] = d0 [I + 2];} c1 [26] = c0 [0]; d1 [26] = d0 [0]; c1 [27] = c0 [1]; d1 [27] = d0 [1];} for (var I: int = 0; I <28; I ++) {source [I] = c1 [I]; source [I + 28] = d1 [I] ;}} private function fullFill64 (byte: byteArray): ByteArray {var ret: ByteArray = new ByteArray (); var len: int = byte. length; var padlen: int = 8-(len % 8); var newlen: int = len + padlen; ret. length = newlen; ret. writeBytes (byte, 0, byte. length); for (var I: int = len; I <newlen; I ++) {ret. writeByte (padlen);} return ret;} private function filterFilled (byte: ByteArray): ByteArray {var ret: ByteArray = new ByteArray (); var padlen: int = byte [byte. length-1]; var len: int = byte. length-padlen; ret. length = len; ret. writeBytes (byte, 0, len); return ret;} private function f (source: Array, times: int, flag: int): void {var l0: array = new Array (32); var r0: Array = new Array (32); var l1: Array = new Array (32); var r1: array = new Array (32); var re: Array = new Array (48); var s: Array = new Array (8); for (var I: int = 0; I <8; I ++) {s [I] = new Array (6);} var sout: Array = new Array (32); var rp: array = new Array (32); for (var I: int = 0; I <32; I ++) {l0 [I] = source [I]; r0 [I] = source [I + 32];} // trace ("l0" + l0); // trace ("r0" + r0); for (var I: int = 0; I <48; I ++) {re [I] = r0 [E [I]-1]; re [I] = re [I] + this. key_ I [times] [I]; if (re [I] = 2) {re [I] = 0 ;}// var outS: string = "=================================\ n"; for (var I: int = 0; I <8; I ++) {for (var j: int = 0; j <6; j ++) {s [I] [j] = re [I * 6 + j]; // outS + = s [I] [j]; // outS + = "," ;}// outS + = "\ n"; var x: int = s [I] [1] * 8 + s [I] [2] * 4 + s [I] [3] * 2 + s [I] [4] * 1; var y: int = s [I] [0] * 2 + s [I] [5] * 1; var sboxData: int = this. sbox [I] [y] [x]; // trace ("s" + I + ":" + sboxData); var outB: Array = this. d2B (sboxData); for (var j: int = 0; j <4; j ++) {sout [I * 4 + j] = outB [4 + j] ;}} // outS + = "========================"; // trace (outS); for (var I: int = 0; I <32; I ++) {rp [I] = sout [P [I]-1]; l1 [I] = r0 [I]; r1 [I] = l0 [I]; r1 [I] = l0 [I] + rp [I]; if (r1 [I] = 2) {r1 [I] = 0;} if (flag = 0) & (times = 0) | (flag = 1) & (times = 15) {source [I] = r1 [I]; source [I + 32] = l1 [I];} else {source [I] = l1 [I]; source [I + 32] = r1 [I] ;}}}

In addition, for AES encryption, you can look at the http://code.google.com/p/as3crypto/

Write a DES encapsulation (because I only use DES): You only need to download the Crypto library and add it to the project to use the following class. The encryption speed is very good. The text in 300 bytes only requires over milliseconds of encryption and decryption (2.8G dual-core CPU)

package crypto{import com.hurlant.crypto.Crypto;import com.hurlant.crypto.symmetric.AESKey;import com.hurlant.util.Hex;import flash.utils.ByteArray;public class Crypto{private var key:ByteArray = Hex.toArray("abababababababab"); //8 bytes hex numpublic function Crypto(){}public function encryptByDES(plainText:ByteArray):ByteArray{var copy:ByteArray = copyArray(plainText);Crypto.getCipher("des-ecb", key).encrypt(copy);return copy;}public function decryptByDES(encryptedText:ByteArray):ByteArray{var copy:ByteArray = copyArray(encryptedText);Crypto.getCipher("des-ecb", key).decrypt(copy);return copy;}private function copyArray(origin:ByteArray):ByteArray{var copyArray:ByteArray = new ByteArray;var pos:int = origin.position;origin.position = 0;copyArray.writeBytes(origin, 0, origin.length);origin.position = pos;return copyArray;}}}

Or http://code.google.com/p/lostinactionscript/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.