- 浏览: 31917 次
- 性别:
- 来自: 北京
-
最新评论
-
keete:
<div class="quote_title ...
找asctionscript3 des 加密解密函数 -
yshao81710:
你好,请问你的des找到源码了吗?可否共享一份
找asctionscript3 des 加密解密函数 -
kenter1643:
You need to include jta.jar. It ...
spring2.52 + hibernate3.2.6 调试的问题 -
wangle100:
您好,我目前在整合spring2.5+hibernate3, ...
spring2.52 + hibernate3.2.6 调试的问题 -
Run:
Eclipse 3.4版本中已经改回来了!
Eclipse v3.2的自动代码提示后不会自动import相关类的问题的解决
本同学花了三天时间整理的,网上应该没有别的了,我搜索了一天,没找到,才下定决心自己搞
DesCrypt.as代码
mxml调用:
internal var dc:DesCrypt = new DesCrypt;
加密:paras.seqno = Base64.encodeByteArray(dc.des(key,seqNo,true,1,key));
解密:
var resultStrs:ByteArray = dc.des(key,ary[1].密文,false,1,key);
resultStrs.position = 0;
var resultStr:String = resultStrs.readMultiByte(resultStrs.length,"gbk");
能解决中文乱码问题。欢迎一起探讨:http://www.shenfen.info
目前还有一个问题:明文大约50个字符以上的时候能加密,但加密时会出错。欢迎后来者研究一下
DesCrypt.as代码
package { import com.hurlant.util.Base64; import com.hurlant.util.Hex; import flash.utils.ByteArray; public class DesCrypt { private static const bytebit:Array = [ 128, 64, 32, 16, 8, 4, 2, 1 ]; private static const bigbyte:Array = [ 0x800000, 0x400000, 0x200000, 0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 0x8000, 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 ]; /* * Use the key schedule specified in the Standard (ANSI X3.92-1981). */ private static const pc1:Array = [ 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 12,4, 27, 19, 11, 3 ]; private static const totrot:Array = [ 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 ]; private static const pc2:Array = [ 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 ]; public function DesCrypt() { //构造 } public function des(key:String, message:String, encrypt:Boolean, mode:uint, iv:String):ByteArray{ //declaring this locally speeds things up a bit var spfunction1:Array = new Array(0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404, 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004); var spfunction2:Array = new Array(-0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20, -0x7fefffe0, -0x7fff7fe0, -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000, -0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020, -0x7fff7fe0, 0, -0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000, 0x8020, -0x7fef8000, -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000, -0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000, -0x7fff8000, 0x20, -0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000, -0x7fffffe0, 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000, 0x8020, -0x80000000, -0x7fefffe0, -0x7fef7fe0, 0x108000); var spfunction3:Array = new Array(0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200); var spfunction4:Array = new Array(0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080); var spfunction5:Array = new Array(0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100, 0x40000100); var spfunction6:Array = new Array(0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000, 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010); var spfunction7:Array = new Array(0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002); var spfunction8:Array = new Array(0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000); //create the 16 or 48 subkeys we will need var keys:Array = generateWorkingKey(key);//密钥已经完成 var m:uint = 0, i:int = 0, j:uint = 0, temp, right1:int= 0, right2:int= 0, left, right, looping; var cbcleft, cbcleft2, cbcright, cbcright2; var endloop, loopinc; //pad the message out with null bytes 填充 // var messages:ByteArray = Hex.toArray(Hex.fromString(message)); //这玩艺儿还不能随便先换成String去调用函数 // message = urlencodeGBK(message); var messages:ByteArray =new ByteArray; //这玩艺儿还不能随便先换成String去调用函数 // messages.writeUTFBytes(message); //这玩艺儿还不能随便先换成String去调用函数 // messages = Hex.toArray(Hex.fromString(message)); //这玩艺儿还不能随便先换成String去调用函数 if(encrypt){ messages.writeMultiByte(message,"gbk"); pad(messages); }else{ messages.writeBytes(Base64.decodeToByteArray(message)); } // message = Hex.toString(Hex.fromArray(messages)); var len:uint = messages.length; for(var kk:uint = 0; kk < messages.length; kk++){ } // var len:uint = message.length; //这种方法不可取,计算中文长度有问题 // messages.length = 0; // trace("len2:" + messages.length); var chunk:uint = 0; //set up the loops for single and triple des var iterations:uint = keys.length == 32 ? 3 : 9; //single or triple des if (iterations == 3) { looping = encrypt ? new Array(0, 32, 2) : new Array(30, -2, -2); } else { looping = encrypt ? new Array(0, 32, 2, 62, 30, -2, 64, 96, 2) : new Array(94, 62, -2, 32, 64, 2, 30, -2, -2); } //store the result here // var result:String = ""; // var tempresult:String = ""; var result:ByteArray = new ByteArray; var tempresult:ByteArray = new ByteArray; var outOff:uint = 0; if (mode == 1) { //CBC mode cbcleft = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++); cbcright = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++); m = 0; } //loop through each 64 bit chunk of the message while (m<len) { //求左右两部分 // var messages:ByteArray = Hex.toArray(Hex.fromString(message)); // trace("message.charCodeAt(2):" + message.charCodeAt(2)); // left = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++); // right = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++); //字节操作效率高,而且还能有效解决中文问题,不要使用(message.charCodeAt(m++) << 24)方式 left = (messages[m++] << 24) | (messages[m++] << 16) | (messages[m++] << 8) | messages[m++]; right = (messages[m++] << 24) | (messages[m++] << 16) | (messages[m++] << 8) | messages[m++]; //for Cipher Block Chaining mode, xor the message with the previous result if (mode == 1) { if (encrypt) { left ^= cbcleft; right ^= cbcright; } else { cbcleft2 = cbcleft; cbcright2 = cbcright; cbcleft = left; cbcright = right; } } //first each 64 but chunk of the message must be permuted according to IP temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16); temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2); temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8); temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1); left = ((left << 1) | (left >>> 31)); right = ((right << 1) | (right >>> 31)); //do this either 1 or 3 times for each chunk of the message for (j=0; j<iterations; j += 3) { endloop = looping[j+1]; loopinc = looping[j+2]; //now go through and perform the encryption or decryption for (i=looping[j]; i != endloop; i += loopinc) { //for efficiency right1 = right ^ keys[i]; right2 = ((right >>> 4) | (right << 28)) ^ keys[i+1]; //the result is attained by passing these bytes through the S selection functions temp = left; left = right; right = temp ^ (spfunction2[(right1 >>> 24) & 0x3f] | spfunction4[(right1 >>> 16) & 0x3f] | spfunction6[(right1 >>> 8) & 0x3f] | spfunction8[right1 & 0x3f] | spfunction1[(right2 >>> 24) & 0x3f] | spfunction3[(right2 >>> 16) & 0x3f] | spfunction5[(right2 >>> 8) & 0x3f] | spfunction7[right2 & 0x3f]); } temp = left; left = right; right = temp; //unreverse left and right } //for either 1 or 3 iterations //move then each one bit to the right left = ((left >>> 1) | (left << 31)); right = ((right >>> 1) | (right << 31)); //now perform IP-1, which is IP in the opposite direction temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1); temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8); temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2); temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16); temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4); //for Cipher Block Chaining mode, xor the message with the previous result if (mode == 1) { if (encrypt) { cbcleft = left; cbcright = right; } else { left ^= cbcleft2; right ^= cbcright2; } } tempresult[outOff + 0] = (left >>> 24); tempresult[outOff + 1] = ((left >>> 16) & 0xff); tempresult[outOff + 2] = ((left >>> 8) & 0xff); tempresult[outOff + 3] = (left & 0xff); tempresult[outOff + 4] = (right >>> 24); tempresult[outOff + 5] = ((right >>> 16) & 0xff); tempresult[outOff + 6] = ((right >>> 8) & 0xff); tempresult[outOff + 7] = (right & 0xff); outOff += 8; if (outOff == 512) { result.writeBytes(tempresult,result.length); tempresult.length = 0; outOff = 0; } } result.writeBytes(tempresult,result.length); if(encrypt) { //加密 return result; }else{//解密 result.writeBytes(tempresult,result.length) var temary:ByteArray = new ByteArray; temary.writeBytes(result); var templen:uint = temary.length - 1; result.length = 0; result.writeBytes(temary,0,(templen - temary[templen] + 1)); return result; } } public function generateWorkingKey(keyss:String):Array { var off:uint = 0; var key:ByteArray = Hex.toArray(Hex.fromString(keyss)); var encrypting:Boolean = true; //int[] newKey = new int[32]; var newKey:Array = []; //boolean[] pc1m = new boolean[56], pcr = new boolean[56]; var pc1m:ByteArray = new ByteArray; var pcr:ByteArray = new ByteArray; var l:uint; for (var j:uint = 0; j < 56; j++) { l = pc1[j]; pc1m[j] = ((key[off + (l >>> 3)] & bytebit[l & 07]) != 0); } for (var i:uint = 0; i < 16; i++) { var m:uint; var n:uint; if (encrypting) { m = i << 1; } else { m = (15 - i) << 1; } n = m + 1; newKey[m] = newKey[n] = 0; for (j = 0; j < 28; j++) { l = j + totrot[i]; if (l < 28) { pcr[j] = pc1m[l]; } else { pcr[j] = pc1m[l - 28]; } } for (j = 28; j < 56; j++) { l = j + totrot[i]; if (l < 56) { pcr[j] = pc1m[l]; } else { pcr[j] = pc1m[l - 28]; } } for (j = 0; j < 24; j++) { if (pcr[pc2[j]]) { newKey[m] |= bigbyte[j]; } if (pcr[pc2[j + 24]]) { newKey[n] |= bigbyte[j]; } } } // // store the processed key // for (i = 0; i != 32; i += 2) { var i1:uint; var i2:uint; i1 = newKey[i]; i2 = newKey[i + 1]; newKey[i+1] = ((i1 & 0x00fc0000) << 6) | ((i1 & 0x00000fc0) << 10) | ((i2 & 0x00fc0000) >>> 10) | ((i2 & 0x00000fc0) >>> 6); newKey[i] = ((i1 & 0x0003f000) << 12) | ((i1 & 0x0000003f) << 16) | ((i2 & 0x0003f000) >>> 4) | (i2 & 0x0000003f); //这里把newKey[i] 和 newKey[i+1]位置对调一下。 } return newKey; } //填充 public function pad(a:ByteArray):void { var c:uint = 8-a.length%8; for (var i:uint=0;i<c;i++){ a[a.length] = c; } } public function unpad(a:ByteArray):void { var c:uint = a.length%8; if (c!=0) throw new Error("PKCS#5::unpad: ByteArray.length isn't a multiple of the blockSize"); c = a[a.length-1]; for (var i:uint=c;i>0;i--) { var v:uint = a[a.length-1]; a.length--; if (c!=v) throw new Error("PKCS#5:unpad: Invalid padding value. expected ["+c+"], found ["+v+"]"); } } } }
mxml调用:
internal var dc:DesCrypt = new DesCrypt;
加密:paras.seqno = Base64.encodeByteArray(dc.des(key,seqNo,true,1,key));
解密:
var resultStrs:ByteArray = dc.des(key,ary[1].密文,false,1,key);
resultStrs.position = 0;
var resultStr:String = resultStrs.readMultiByte(resultStrs.length,"gbk");
能解决中文乱码问题。欢迎一起探讨:http://www.shenfen.info
目前还有一个问题:明文大约50个字符以上的时候能加密,但加密时会出错。欢迎后来者研究一下
相关推荐
总结来说,AS3中的DES加解密涉及到加密理论、Crypto库的使用以及Flash Builder 4.7项目管理。了解并熟练掌握这些知识,对于开发涉及敏感信息传输的AS3应用至关重要。在实践中,还应注意不断学习和适应最新的加密技术...
《Crypto在ActionScript 3中的应用与加密技术详解》 ActionScript 3(AS3)是Adobe Flash平台上的主要编程语言,广泛应用于网络多媒体内容的开发。在信息安全日益重要的今天,加密技术在AS3中的应用变得至关重要。...
单元测试主要针对加密和解密的函数,确保它们能正确地执行加解密操作。集成测试则会检查整个系统,确保加密后的数据可以在解密后恢复到原始状态,同时验证数据在传输和存储过程中的安全性。 至于压缩包子文件的...
《AS3 Crypto:深入理解加解密工具类》 在ActionScript 3(AS3)中,加密和解密是确保数据安全的重要手段。本文将详细介绍一个名为"Crypto"的AS3加解密工具类,它为开发者提供了一套完整的加密解决方案。由于原...
4. **AS3 Crypto库**:在Flex中实现加密解密,可以借助AS3 Crypto库,这是一个开源的ActionScript 3加密库,包含了多种加密算法的实现。使用这个库,开发者可以方便地集成加密功能到自己的Flex应用中。 5. **加密...
虽然描述中没有提供具体的信息,但我们可以通过标题推测,内容可能涉及到使用Crypto库进行数据加密、解密,以及AS3CORELIB库提供的辅助功能,如类型转换和通用工具函数,这些在处理加密操作时经常是必需的。...
使用这个库,开发者可以在ActionScript3项目中轻松地实现数据的加密和解密,例如,用于保护用户的登录凭据、存储敏感游戏数据或者构建安全的通信协议。 在实际应用中,为了增强安全性,通常还会结合其他技术,如...
3. **MD5加密**:MD5是一种常用的哈希函数,用于生成固定长度的数字指纹。尽管MD5在安全领域已被认为存在弱点,但它在文件校验、密码存储等方面仍有应用。在Flex中,`flash.crypto.MD5`类可以帮助我们计算MD5摘要。 ...