Des encryption and decryption in script language

Source: Internet
Author: User
<Script language = "JavaScript">

// By liuminghua at 2005.1.10

VaR g_ip = new array (// initial IP address replacement
,
);

VaR g_invip = new array (// reverse initial IP address replacement
,
);

VaR g_e = new array (// e select table
, 28, 29, 28, 29, 30, 31, 32, 31
);

VaR g_p = new array (// P-bit table

);

VaR G_s = new array (// 8 s combination
,,
,,
,,
,,
,,
,,
,,
,
);

VaR g_keyout = new array (768 );

Function initkey (str_key)
{
VaR pC1 = new array (// replace 64-bit keys with 56-bit
,
);
VaR PC2 = new array (// obtain the 48-bit key for the 64-bit shift.
, 34,53, 46,42, 50 ,36, 29,32
);
VaR NLS = new array );

VaR B _array_key = new array (64 );
For (n = I = 0; I <8; I ++)
B _array_key [n ++] = str_key.charcodeat (I)> I & 0x01; // obtain binary

VaR Cd = new array (56 );
VaR c = new array (28 );
VaR d = new array (28 );
 
For (I = 0; I <56; I ++)
CD [I] = B _array_key [pC1 [I]-1];
For (I = 0; I <28; I ++ ){
C [I] = CD [I];
D [I] = CD [I + 28]; // separate c, d
}
For (CNT = 0; CNT <16; CNT ++ ){
For (I = 0; I <NLS [CNT]; I ++ ){
C = lshift (C );
D = lshift (d );
}
For (I = 0; I <28; I ++ ){
CD [I] = C [I];
CD [28 + I] = d [I];
}
For (I = 0; I <48; I ++ ){
G_keyout [CNT * 48 + I] = CD [PC2 [I]-1];
}
}
}
Function lshift (p_array) // shifts one position left in a loop
{
VaR tmp_array = new array (28 );
For (I = 0; I <27; I ++) tmp_array [I] = p_array [I + 1];
Tmp_array [27] = p_array [0];
Return tmp_array;

}

Function frk (p_array_r, CNT)
{
VaR B _array_fout = new array (32 );
VaR B _array_expand = new array (48 );
VaR array_pout = new array (48 );
 
For (I = 0; I <48; I ++) {// convert 32-bit to 48-bit
B _array_expand [I] = p_array_r [g_e [I]-1];
}
VaR B _array_dor = new array (48 );
For (I = 0; I <48; I ++) {// bitwise OR with the key Ki, 8 groups, each with 6 bits
B _array_dor [I] = B _array_expand [I] ^ g_keyout [CNT * 48 + I];
}
For (SCNT = n = 0; SCNT <8; SCNT ++ ){
Row = (B _array_dor [SCNT * 6 + 0] <1) + B _array_dor [SCNT * 6 + 5];
Col = (B _array_dor [SCNT * 6 + 1] <3) + (B _array_dor [SCNT * 6 + 2] <2) + (B _array_dor [SCNT * 6 + 3] <1) + B _array_dor [SCNT * 6 + 4]
VaR sout = new array (8 );
Sout [SCNT] = G_s [SCNT * 64 + (row <4) + Col];
For (I = 3; I> = 0; I --){
Array_pout [N] = sout [SCNT]> I;
Sout [SCNT] = sout [SCNT]-(array_pout [n ++] <I );
}
}
For (I = 0; I <32; I ++)
B _array_fout [I] = array_pout [g_p [I]-1];

Return B _array_fout;
}
Function des_encrypt (str_input) // Encrypt
{
// Initkey ("454554454"); // only test, here
 
VaR B _array_input = new array (64 );
VaR B _array_lr = new array (64 );
VaR B _array_l = new array (32 );
VaR B _array_r = new array (32 );
VaR B _array_fn = new array (32 );
 
For (n = I = 0; I <8; I ++)
For (j = 0; j <8; j ++)
B _array_input [n ++] = str_input.charcodeat (I)> J & 0x01; // obtain binary
 
For (I = 0; I <64; I ++ ){
B _array_lr [I] = B _array_input [g_ip [I]-1];
If (I <32) B _array_l [I] = B _array_lr [I];
Else B _array_r [i-32] = B _array_lr [I]; // separate L, R
}
 
For (CNT = 0; CNT <16; CNT ++)
{
B _array_fn = frk (B _array_r, CNT );
For (I = 0; I <32; I ++ ){
J = B _array_r [I];
B _array_r [I] = B _array_l [I] ^ B _array_fn [I];
B _array_l [I] = J;
}
}
 
For (I = 0; I <32; I ++ ){
B _array_lr [I] = B _array_r [I];
B _array_lr [I + 32] = B _array_l [I];
}
 
VaR fout = new array (64 );
For (I = 0; I <64; I ++) fout [I] = B _array_lr [g_invip [I]-1];
VaR Strout = "";
For (I = 0; I <8; I ++ ){
Sum = 0;
For (j = 0; j <8; j ++ ){
Sum + = (fout [I * 8 + J] <j );
}
Strout + = string. fromcharcode (SUM );
// Document. Write (string. fromcharcode (SUM ));
}
 
Return Strout;

}
Function des_decrypt (str_input) // decrypt
{
VaR B _array_input = new array (64 );
VaR B _array_lr = new array (64 );
VaR B _array_l = new array (32 );
VaR B _array_r = new array (32 );
VaR B _array_fn = new array (32 );
 
For (n = I = 0; I <8; I ++)
For (j = 0; j <8; j ++)
B _array_input [n ++] = str_input.charcodeat (I)> J & 0x01; // obtain binary
 
For (I = 0; I <64; I ++ ){
B _array_lr [I] = B _array_input [g_ip [I]-1];
If (I <32) B _array_l [I] = B _array_lr [I];
Else B _array_r [i-32] = B _array_lr [I]; // separate L, R
}
 
For (CNT = 15; CNT> = 0; CNT --)
{
B _array_fn = frk (B _array_r, CNT );
For (I = 0; I <32; I ++ ){
J = B _array_r [I];
B _array_r [I] = B _array_l [I] ^ B _array_fn [I];
B _array_l [I] = J;
}
}
 
For (I = 0; I <32; I ++ ){
B _array_lr [I] = B _array_r [I];
B _array_lr [I + 32] = B _array_l [I];
}
 
VaR fout = new array (64 );
For (I = 0; I <64; I ++) fout [I] = B _array_lr [g_invip [I]-1];
VaR Strout = "";
For (I = 0; I <8; I ++ ){
Sum = 0;
For (j = 0; j <8; j ++ ){
Sum + = (fout [I * 8 + J] <j );
}
Strout + = string. fromcharcode (SUM );
}
Return Strout;
}
</SCRIPT>

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.