import flash.utils.ByteArray;
import flash.utils.Endian;
public class RC6
{
private static var l_key:Vector.<uint>=new Vector.<uint>(44);
private static var instance:RC6 = null;
public function RC6()
{
}
public static function getInstance():RC6{
if(instance == null){
instance = new RC6();
}
return instance;
}
private function rotr(x:uint,n:uint):uint {
return ((x >>> n)) | (x << (32 - n));
}
private function rotl(x:uint,n:uint):uint{
return (x << n) | (x >>> (32 - n));
}
private function f_rnd(i:int,a:uint,b:uint,c:uint,d:uint):Vector.<uint>{
var retArr:Vector.<uint> = new Vector.<uint>(2);
var u_temp1:uint = (d + d + 1);
var u_temp2:uint = d * u_temp1;
var u:uint = rotl(u_temp2, 5);
var t_temp1:uint = (b + b + 1);
var t_temp2:uint = b * t_temp1;
var t:uint = rotl(t_temp2, 5);
a = rotl(a ^ t, u) + l_key[i];
c = rotl(c ^ u, t) + l_key[i + 1];
retArr[0] = a;
retArr[1] = c;
return retArr;
}
private function i_rnd(i:int,a:uint,b:uint,c:uint,d:uint):Vector.<uint>{
var retArr:Vector.<uint> = new Vector.<uint>(2);
var u_temp1:uint = (d + d + 1);
var u_temp2:uint = d * u_temp1;
var u:uint = rotl(u_temp2, 5);
var t_temp1:uint = (b + b + 1);
var t_temp2:uint = b * t_temp1;
var t:uint = rotl(t_temp2, 5);
c = rotr(c - l_key[i + 1], t) ^ u;
a = rotr(a - l_key[i], u) ^ t;
retArr[0] = a;
retArr[1] = c;
return retArr;
}
public function rc6_set_key(in_key:ByteArray):void{
var key_len:int = in_key.length * 8;
var i:uint = 0;
var j:uint = 0;
var k:uint = 0;
var a:uint = 0;
var b:uint = 0;
var t:int;
var l:Vector.<uint> = new Vector.<uint>(8);
l_key[0] = 0xb7e15163;
for(k = 1; k < 44; ++k){
l_key[k] = l_key[k - 1] + 0x9e3779b9;
}
in_key.position = 0;
for(k = 0; k < key_len / 32; ++k){
l[k] = in_key.readUnsignedInt();
}
t = (key_len / 32) - 1;
for(k = 0; k < 132; ++k){
var tt:uint = l_key[i] + a + b;
a = rotl(l_key[i] + a + b, 3);
b += a;
b = rotl(l[j] + b, b);
l_key[i] = a;
l[j] = b;
i = (i == 43 ? 0 : i + 1);
j = (j == t ? 0 : j + 1);
}
}
private function rc6_encrypt(in_blk:Vector.<uint>):Vector.<uint>{
var out_blk:Vector.<uint> = new Vector.<uint>(4);
var ret:Vector.<uint> = new Vector.<uint>(2);
var a:uint = in_blk[0];
var b:uint = in_blk[1] + l_key[0];
var c:uint = in_blk[2];
var d:uint = in_blk[3] + l_key[1];
ret = f_rnd(2, a, b, c, d);
a = ret[0];
c = ret[1];
ret = f_rnd(4, b, c, d, a);
b = ret[0];
d = ret[1];
ret = f_rnd(6, c, d, a, b);
c = ret[0];
a = ret[1];
ret = f_rnd(8, d, a, b, c);
d = ret[0];
b = ret[1];
ret = f_rnd(10, a, b, c, d);
a = ret[0];
c = ret[1];
ret = f_rnd(12, b, c, d, a);
b = ret[0];
d = ret[1];
ret = f_rnd(14, c, d, a, b);
c = ret[0];
a = ret[1];
ret = f_rnd(16, d, a, b, c);
d = ret[0];
b = ret[1];
ret = f_rnd(18, a, b, c, d);
a = ret[0];
c = ret[1];
ret = f_rnd(20, b, c, d, a);
b = ret[0];
d = ret[1];
ret = f_rnd(22, c, d, a, b);
c = ret[0];
a = ret[1];
ret = f_rnd(24, d, a, b, c);
d = ret[0];
b = ret[1];
ret = f_rnd(26, a, b, c, d);
a = ret[0];
c = ret[1];
ret = f_rnd(28, b, c, d, a);
b = ret[0];
d = ret[1];
ret = f_rnd(30, c, d, a, b);
c = ret[0];
a = ret[1];
ret = f_rnd(32, d, a, b, c);
d = ret[0];
b = ret[1];
ret = f_rnd(34, a, b, c, d);
a = ret[0];
c = ret[1];
ret = f_rnd(36, b, c, d, a);
b = ret[0];
d = ret[1];
ret = f_rnd(38, c, d, a, b);
c = ret[0];
a = ret[1];
ret = f_rnd(40, d, a, b, c);
d = ret[0];
b = ret[1];
out_blk[0] = a + l_key[42];
out_blk[1] = b;
out_blk[2] = c + l_key[43];
out_blk[3] = d;
return out_blk;
}
private function rc6_decrypt(in_blk:Vector.<uint>):Vector.<uint>{
var out_blk:Vector.<uint> = new Vector.<uint>(4);
var ret:Vector.<uint> = new Vector.<uint>(2);
var d:uint = in_blk[3];
var c:uint = in_blk[2] - l_key[43];
var b:uint = in_blk[1];
var a:uint = in_blk[0] - l_key[42];
ret = i_rnd(40,d,a,b,c);
d = ret[0];
b = ret[1];
ret = i_rnd(38,c,d,a,b);
c = ret[0];
a = ret[1];
ret = i_rnd(36,b,c,d,a);
b = ret[0];
d = ret[1];
ret = i_rnd(34,a,b,c,d);
a = ret[0];
c = ret[1];
ret = i_rnd(32,d,a,b,c);
d = ret[0];
b = ret[1];
ret = i_rnd(30,c,d,a,b);
c = ret[0];
a = ret[1];
ret = i_rnd(28,b,c,d,a);
b = ret[0];
d = ret[1];
ret = i_rnd(26,a,b,c,d);
a = ret[0];
c = ret[1];
ret = i_rnd(24,d,a,b,c);
d = ret[0];
b = ret[1];
ret = i_rnd(22,c,d,a,b);
c = ret[0];
a = ret[1];
ret = i_rnd(20,b,c,d,a);
b = ret[0];
d = ret[1];
ret = i_rnd(18,a,b,c,d);
a = ret[0];
c = ret[1];
ret = i_rnd(16,d,a,b,c);
d = ret[0];
b = ret[1];
ret = i_rnd(14,c,d,a,b);
c = ret[0];
a = ret[1];
ret = i_rnd(12,b,c,d,a);
b = ret[0];
d = ret[1];
ret = i_rnd(10,a,b,c,d);
a = ret[0];
c = ret[1];
ret = i_rnd( 8,d,a,b,c);
d = ret[0];
b = ret[1];
ret = i_rnd( 6,c,d,a,b);
c = ret[0];
a = ret[1];
ret = i_rnd( 4,b,c,d,a);
b = ret[0];
d = ret[1];
ret = i_rnd( 2,a,b,c,d);
a = ret[0];
c = ret[1];
out_blk[3] = d - l_key[1];
out_blk[2] = c;
out_blk[1] = b - l_key[0];
out_blk[0] = a;
return out_blk;
}
/**
* 设置
*/
public function set_key(keyStr:String):void{
keyStr = keyStr.substr(0,32)
var key:ByteArray = new ByteArray();
key.endian = Endian.LITTLE_ENDIAN;
key.writeMultiByte(keyStr,"GB2312");
rc6_set_key(key);
}
/**
* 加密
*/
public function encrypt(content:String):Vector.<uint>{
var block:ByteArray = new ByteArray();
block.endian = Endian.LITTLE_ENDIAN;
block.writeMultiByte(content,"GB2312");
if (block.length % 16 != 0) {
var trade:int =(block.length + 15)/16;
var len:int = trade * 16;
block.length = len;
}
var row:int = block.length/4;
var retArr:Vector.<uint> = new Vector.<uint>();
var iarr:Vector.<uint> = new Vector.<uint>(4);
var t:int = 0;
block.position = 0;
for(var i:int = 0;i < row;i++){
var en_num:uint = block.readUnsignedInt();
iarr[t] = en_num;
t++;
if(t%4 == 0){
var enArr:Vector.<uint> = rc6_encrypt(iarr);
for(var j:int=0;j<enArr.length;j++){
retArr.push(enArr[j]);
}
t = 0;
}
}
return retArr;
}
/**
* 加密
*/
public function bt_encrypt(block:ByteArray):ByteArray{
if (block.length % 16 != 0) {
var trade:int =(block.length + 15)/16;
var len:int = trade * 16;
block.length = len;
}
var row:int = block.length/4;
var retArr:ByteArray = new ByteArray();
retArr.endian = Endian.LITTLE_ENDIAN;
var iarr:Vector.<uint> = new Vector.<uint>(4);
var t:int = 0;
block.position = 0;
for(var i:int = 0;i < row;i++){
var en_num:uint = block.readUnsignedInt();
iarr[t] = en_num;
t++;
if(t%4 == 0){
var enArr:Vector.<uint> = rc6_encrypt(iarr);
for(var j:int=0;j<enArr.length;j++){
retArr.writeUnsignedInt(enArr[j]);
}
t = 0;
}
}
return retArr;
}
/**
* 解密
*/
public function decrypt(src:Vector.<uint>):String{
var block:ByteArray = new ByteArray();
block.endian = Endian.LITTLE_ENDIAN;
var iarr:Vector.<uint> = new Vector.<uint>(4);
var t:int = 0;
for(var i:int = 0;i < src.length;i++){
iarr[t] = src[i];
t++;
if(t%4 == 0){
var enArr:Vector.<uint> = rc6_decrypt(iarr);
t = 0;
for(var j:int=0;j<enArr.length;j++){
block.writeUnsignedInt(enArr[j]);
}
}
}
block.position = 0;
var str:String = block.readMultiByte(block.length,"GB2312");
return str;
}
/**
* 解密
*/
public function bt_decrypt(src:ByteArray):ByteArray{
var block:ByteArray = new ByteArray();
block.endian = Endian.LITTLE_ENDIAN;
block.position = 0;
var iarr:Vector.<uint> = new Vector.<uint>(4);
var t:int = 0;
for(var i:int = 0;i < src.length/4;i++){
src.position = i*4;
iarr[t] = src.readUnsignedInt();
t++;
if(t%4 == 0){
var enArr:Vector.<uint> = rc6_decrypt(iarr);
t = 0;
for(var j:int=0;j<enArr.length;j++){
block.writeInt(enArr[j]);
}
}
}
src.length=0;
src.writeBytes(block,0,block.length);
src.position=0;
return src;
}
}
分享到:
相关推荐
RC4加解密算法的例程,用vs2005 C#实现,代码中有部分注释,外部有操作文档,唯一的不足是工程文件命名为RSA,但该例程实现的的确是RC4算法!-RC4 encryption algorithm routines, using vs2005 C# Realize, the ...
对称加密,如DES(Data Encryption Standard)、3DES(Triple DES)和AES(Advanced Encryption Standard),特点是加密和解密使用相同的密钥,速度快,但密钥管理复杂,不适合大规模网络环境。非对称加密,如RSA、...
4. **AS3 Crypto库**:在Flex中实现加密解密,可以借助AS3 Crypto库,这是一个开源的ActionScript 3加密库,包含了多种加密算法的实现。使用这个库,开发者可以方便地集成加密功能到自己的Flex应用中。 5. **加密...
RC6 MARS SEAL Luby-Rackoff MDC various encryption modes (CFB, CBC, OFB, counter) DH DH2 MQV DSA NR ElGamal LUC LUCDIF LUCELG Rabin RW RSA BlumGoldwasser elliptic curve cryptosystems...
MD2, MD5, SHA-1, HAVAL, Tiger, RIPE-MD160, MD5-MAC, HMAC, XOR-MAC, DES,IDEA, WAKE, 3-WAY, TEA, SAFER, Blowfish,SHARK, GOST, CAST-128, Square,Diamond2, Sapphire, RC2, RC5, RC6, MARS, SEAL, Luby-Rackoff...
RC6 SAFER+ Rijndael (aka AES) Twofish SAFER (K64, SK64, K128, SK128) RC2 DES, 3DES CAST5 Noekeon Skipjack Anubis (with optional tweak as proposed by the developers) Khazad Chaining Modes ...
在ASP中加密方法有对应的解密方法好象不多,现在根据前辈资料整理出在asp中加密与解密函数 rsa.asp <% rem 在ASP中实现加密与解密,加密方法:根据RSA rem 联系:hnsoso@sina.com Class clsRSA Public ...
3DES(Triple DES):是基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高; RC2和 RC4:用变长密钥对大量数据进行加密,比 DES 快; IDEA(International Data Encryption Algorithm
3. **安全功能**:具备加密和解密功能,保证数据传输的安全性。 4. **天线调谐**:支持不同天线设计,适应各种尺寸和形状的RFID标签。 5. **防冲突算法**:采用防碰撞算法,允许同时处理多个RFID标签。 6. **操作...
二进制文件可以在/deploy/as3crypto.swc找到 复制到您的库文件夹,将其添加到您的类路径,然后就可以了。 特征 协议:TLS 1.0 支持(部分) 证书:X.509 证书解析和验证,内置根 CA。 公钥加密:RSA(加密/解密、...
3. Kerberos协议:深入理解Kerberos的架构、工作流程,包括AS-REQ/AS-REP、TGS-REQ/TGS-REP等交互过程,以及票证的结构和加密机制。 4. 加密解密算法:熟悉Kerberos协议中使用的加密算法,如AES、RC4等,以及密钥...
解密时,需要使用相同的密钥进行反向操作。例如,使用Python的pycryptodome库可以实现: ```python from Crypto.Cipher import ARC4 import os key = os.urandom(16) # 生成随机16字节密钥 cipher = ARC4.new(key)...
Diamond2, Sapphire, RC2, RC5, RC6, MARS, SEAL, Luby-Rackoff, MDC, various encryption modes (CFB, CBC, OFB, counter), DH, DH2, MQV, DSA, NR, ElGamal, LUC, LUCDIF, LUCELG, Rabin, RW, RSA, BlumGoldwasser...
Encryption Algorithms Hash AlgorithmsBlowfishCast 128Cast 256DES, 3DESIce, Thin Ice, Ice2IDEAMarsMisty1RC2, RC4, RC5, RC6Rijndael (the new AES)SerpentTeaTwofish HavalMD4MD5RipeMD-128RipeMD-160SHA-1SHA...
PyPDF2是Python的一个开源库,它提供了丰富的API接口,可以方便地进行PDF文档的拆分、合并、提取文本、旋转页面、加密和解密等操作。它的功能强大且易于使用,是Python开发者处理PDF文件的理想选择。 PDF加密原理:...
3. PDF加密功能仅适用于标准40位和128位RC4加密,对于更高级的加密方式(如AES),可能需要其他工具。 总之,PyPDF4是一个强大的PDF处理工具,对于需要在Python环境中处理PDF的开发者来说,它提供了丰富的功能和...
需要注意的是,某些算法如RC和DESX由于安全性的考虑,在较新版本的SQL Server中可能不再支持。 下面,我们将通过实例创建非对称密钥、对称密钥以及证书: ```sql create asymmetric key asyc_key_enc with ...