1#!/usr/local/bin/perl 2# des.pl - eric young 22/11/1991 eay@mincom.oz.au or eay@psych.psy.uq.oz.au 3# 4# Copyright (C) 1993 Eric Young 5# 6# 11 April 1996 - patched to circumvent Perl 5 (through 5.002) problem 7# with sign-extension on right shift operations. 8# Ed Kubaitis - ejk@uiuc.edu 9# 10# eay - 92/08/31 - I think I have fixed all problems for 64bit 11# versions of perl but I could be wrong since I have not tested it yet :-). 12# 13# This is an implementation of DES in perl. 14# The two routines (des_set_key and des_ecb_encrypt) 15# take 8 byte objects as arguments. 16# 17# des_set_key takes an 8 byte string as a key and returns a key schedule 18# for use in calls to des_ecb_encrypt. 19# des_ecb_encrypt takes three arguments, the first is a key schedule 20# (make sure to pass it by reference with the *), the second is 1 21# to encrypt, 0 to decrypt. The third argument is an 8 byte object 22# to encrypt. The function returns an 8 byte object that has been 23# DES encrypted. 24# 25# example: 26# require 'des.pl' 27# 28# $key =pack("C8",0x12,0x23,0x45,0x67,0x89,0xab,0xcd,0xef); 29# @ks= &des_set_key($key); 30# 31# $outbytes= &des_ecb_encrypt(*ks,1,$data); 32# @enc =unpack("C8",$outbytes); 33# 34 35package des; 36 37eval("usr integer;") if (int($]) > 4); 38 39# The following 8 arrays are used in des_set_key 40@skb0=( 41# for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 420x00000000,0x00000010,0x20000000,0x20000010, 430x00010000,0x00010010,0x20010000,0x20010010, 440x00000800,0x00000810,0x20000800,0x20000810, 450x00010800,0x00010810,0x20010800,0x20010810, 460x00000020,0x00000030,0x20000020,0x20000030, 470x00010020,0x00010030,0x20010020,0x20010030, 480x00000820,0x00000830,0x20000820,0x20000830, 490x00010820,0x00010830,0x20010820,0x20010830, 500x00080000,0x00080010,0x20080000,0x20080010, 510x00090000,0x00090010,0x20090000,0x20090010, 520x00080800,0x00080810,0x20080800,0x20080810, 530x00090800,0x00090810,0x20090800,0x20090810, 540x00080020,0x00080030,0x20080020,0x20080030, 550x00090020,0x00090030,0x20090020,0x20090030, 560x00080820,0x00080830,0x20080820,0x20080830, 570x00090820,0x00090830,0x20090820,0x20090830, 58); 59@skb1=( 60# for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 610x00000000,0x02000000,0x00002000,0x02002000, 620x00200000,0x02200000,0x00202000,0x02202000, 630x00000004,0x02000004,0x00002004,0x02002004, 640x00200004,0x02200004,0x00202004,0x02202004, 650x00000400,0x02000400,0x00002400,0x02002400, 660x00200400,0x02200400,0x00202400,0x02202400, 670x00000404,0x02000404,0x00002404,0x02002404, 680x00200404,0x02200404,0x00202404,0x02202404, 690x10000000,0x12000000,0x10002000,0x12002000, 700x10200000,0x12200000,0x10202000,0x12202000, 710x10000004,0x12000004,0x10002004,0x12002004, 720x10200004,0x12200004,0x10202004,0x12202004, 730x10000400,0x12000400,0x10002400,0x12002400, 740x10200400,0x12200400,0x10202400,0x12202400, 750x10000404,0x12000404,0x10002404,0x12002404, 760x10200404,0x12200404,0x10202404,0x12202404, 77); 78@skb2=( 79# for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 800x00000000,0x00000001,0x00040000,0x00040001, 810x01000000,0x01000001,0x01040000,0x01040001, 820x00000002,0x00000003,0x00040002,0x00040003, 830x01000002,0x01000003,0x01040002,0x01040003, 840x00000200,0x00000201,0x00040200,0x00040201, 850x01000200,0x01000201,0x01040200,0x01040201, 860x00000202,0x00000203,0x00040202,0x00040203, 870x01000202,0x01000203,0x01040202,0x01040203, 880x08000000,0x08000001,0x08040000,0x08040001, 890x09000000,0x09000001,0x09040000,0x09040001, 900x08000002,0x08000003,0x08040002,0x08040003, 910x09000002,0x09000003,0x09040002,0x09040003, 920x08000200,0x08000201,0x08040200,0x08040201, 930x09000200,0x09000201,0x09040200,0x09040201, 940x08000202,0x08000203,0x08040202,0x08040203, 950x09000202,0x09000203,0x09040202,0x09040203, 96); 97@skb3=( 98# for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 990x00000000,0x00100000,0x00000100,0x00100100, 1000x00000008,0x00100008,0x00000108,0x00100108, 1010x00001000,0x00101000,0x00001100,0x00101100, 1020x00001008,0x00101008,0x00001108,0x00101108, 1030x04000000,0x04100000,0x04000100,0x04100100, 1040x04000008,0x04100008,0x04000108,0x04100108, 1050x04001000,0x04101000,0x04001100,0x04101100, 1060x04001008,0x04101008,0x04001108,0x04101108, 1070x00020000,0x00120000,0x00020100,0x00120100, 1080x00020008,0x00120008,0x00020108,0x00120108, 1090x00021000,0x00121000,0x00021100,0x00121100, 1100x00021008,0x00121008,0x00021108,0x00121108, 1110x04020000,0x04120000,0x04020100,0x04120100, 1120x04020008,0x04120008,0x04020108,0x04120108, 1130x04021000,0x04121000,0x04021100,0x04121100, 1140x04021008,0x04121008,0x04021108,0x04121108, 115); 116@skb4=( 117# for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 1180x00000000,0x10000000,0x00010000,0x10010000, 1190x00000004,0x10000004,0x00010004,0x10010004, 1200x20000000,0x30000000,0x20010000,0x30010000, 1210x20000004,0x30000004,0x20010004,0x30010004, 1220x00100000,0x10100000,0x00110000,0x10110000, 1230x00100004,0x10100004,0x00110004,0x10110004, 1240x20100000,0x30100000,0x20110000,0x30110000, 1250x20100004,0x30100004,0x20110004,0x30110004, 1260x00001000,0x10001000,0x00011000,0x10011000, 1270x00001004,0x10001004,0x00011004,0x10011004, 1280x20001000,0x30001000,0x20011000,0x30011000, 1290x20001004,0x30001004,0x20011004,0x30011004, 1300x00101000,0x10101000,0x00111000,0x10111000, 1310x00101004,0x10101004,0x00111004,0x10111004, 1320x20101000,0x30101000,0x20111000,0x30111000, 1330x20101004,0x30101004,0x20111004,0x30111004, 134); 135@skb5=( 136# for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 1370x00000000,0x08000000,0x00000008,0x08000008, 1380x00000400,0x08000400,0x00000408,0x08000408, 1390x00020000,0x08020000,0x00020008,0x08020008, 1400x00020400,0x08020400,0x00020408,0x08020408, 1410x00000001,0x08000001,0x00000009,0x08000009, 1420x00000401,0x08000401,0x00000409,0x08000409, 1430x00020001,0x08020001,0x00020009,0x08020009, 1440x00020401,0x08020401,0x00020409,0x08020409, 1450x02000000,0x0A000000,0x02000008,0x0A000008, 1460x02000400,0x0A000400,0x02000408,0x0A000408, 1470x02020000,0x0A020000,0x02020008,0x0A020008, 1480x02020400,0x0A020400,0x02020408,0x0A020408, 1490x02000001,0x0A000001,0x02000009,0x0A000009, 1500x02000401,0x0A000401,0x02000409,0x0A000409, 1510x02020001,0x0A020001,0x02020009,0x0A020009, 1520x02020401,0x0A020401,0x02020409,0x0A020409, 153); 154@skb6=( 155# for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 1560x00000000,0x00000100,0x00080000,0x00080100, 1570x01000000,0x01000100,0x01080000,0x01080100, 1580x00000010,0x00000110,0x00080010,0x00080110, 1590x01000010,0x01000110,0x01080010,0x01080110, 1600x00200000,0x00200100,0x00280000,0x00280100, 1610x01200000,0x01200100,0x01280000,0x01280100, 1620x00200010,0x00200110,0x00280010,0x00280110, 1630x01200010,0x01200110,0x01280010,0x01280110, 1640x00000200,0x00000300,0x00080200,0x00080300, 1650x01000200,0x01000300,0x01080200,0x01080300, 1660x00000210,0x00000310,0x00080210,0x00080310, 1670x01000210,0x01000310,0x01080210,0x01080310, 1680x00200200,0x00200300,0x00280200,0x00280300, 1690x01200200,0x01200300,0x01280200,0x01280300, 1700x00200210,0x00200310,0x00280210,0x00280310, 1710x01200210,0x01200310,0x01280210,0x01280310, 172); 173@skb7=( 174# for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 1750x00000000,0x04000000,0x00040000,0x04040000, 1760x00000002,0x04000002,0x00040002,0x04040002, 1770x00002000,0x04002000,0x00042000,0x04042000, 1780x00002002,0x04002002,0x00042002,0x04042002, 1790x00000020,0x04000020,0x00040020,0x04040020, 1800x00000022,0x04000022,0x00040022,0x04040022, 1810x00002020,0x04002020,0x00042020,0x04042020, 1820x00002022,0x04002022,0x00042022,0x04042022, 1830x00000800,0x04000800,0x00040800,0x04040800, 1840x00000802,0x04000802,0x00040802,0x04040802, 1850x00002800,0x04002800,0x00042800,0x04042800, 1860x00002802,0x04002802,0x00042802,0x04042802, 1870x00000820,0x04000820,0x00040820,0x04040820, 1880x00000822,0x04000822,0x00040822,0x04040822, 1890x00002820,0x04002820,0x00042820,0x04042820, 1900x00002822,0x04002822,0x00042822,0x04042822, 191); 192 193@shifts2=(0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0); 194 195# used in ecb_encrypt 196@SP0=( 1970x00410100, 0x00010000, 0x40400000, 0x40410100, 1980x00400000, 0x40010100, 0x40010000, 0x40400000, 1990x40010100, 0x00410100, 0x00410000, 0x40000100, 2000x40400100, 0x00400000, 0x00000000, 0x40010000, 2010x00010000, 0x40000000, 0x00400100, 0x00010100, 2020x40410100, 0x00410000, 0x40000100, 0x00400100, 2030x40000000, 0x00000100, 0x00010100, 0x40410000, 2040x00000100, 0x40400100, 0x40410000, 0x00000000, 2050x00000000, 0x40410100, 0x00400100, 0x40010000, 2060x00410100, 0x00010000, 0x40000100, 0x00400100, 2070x40410000, 0x00000100, 0x00010100, 0x40400000, 2080x40010100, 0x40000000, 0x40400000, 0x00410000, 2090x40410100, 0x00010100, 0x00410000, 0x40400100, 2100x00400000, 0x40000100, 0x40010000, 0x00000000, 2110x00010000, 0x00400000, 0x40400100, 0x00410100, 2120x40000000, 0x40410000, 0x00000100, 0x40010100, 213); 214@SP1=( 2150x08021002, 0x00000000, 0x00021000, 0x08020000, 2160x08000002, 0x00001002, 0x08001000, 0x00021000, 2170x00001000, 0x08020002, 0x00000002, 0x08001000, 2180x00020002, 0x08021000, 0x08020000, 0x00000002, 2190x00020000, 0x08001002, 0x08020002, 0x00001000, 2200x00021002, 0x08000000, 0x00000000, 0x00020002, 2210x08001002, 0x00021002, 0x08021000, 0x08000002, 2220x08000000, 0x00020000, 0x00001002, 0x08021002, 2230x00020002, 0x08021000, 0x08001000, 0x00021002, 2240x08021002, 0x00020002, 0x08000002, 0x00000000, 2250x08000000, 0x00001002, 0x00020000, 0x08020002, 2260x00001000, 0x08000000, 0x00021002, 0x08001002, 2270x08021000, 0x00001000, 0x00000000, 0x08000002, 2280x00000002, 0x08021002, 0x00021000, 0x08020000, 2290x08020002, 0x00020000, 0x00001002, 0x08001000, 2300x08001002, 0x00000002, 0x08020000, 0x00021000, 231); 232@SP2=( 2330x20800000, 0x00808020, 0x00000020, 0x20800020, 2340x20008000, 0x00800000, 0x20800020, 0x00008020, 2350x00800020, 0x00008000, 0x00808000, 0x20000000, 2360x20808020, 0x20000020, 0x20000000, 0x20808000, 2370x00000000, 0x20008000, 0x00808020, 0x00000020, 2380x20000020, 0x20808020, 0x00008000, 0x20800000, 2390x20808000, 0x00800020, 0x20008020, 0x00808000, 2400x00008020, 0x00000000, 0x00800000, 0x20008020, 2410x00808020, 0x00000020, 0x20000000, 0x00008000, 2420x20000020, 0x20008000, 0x00808000, 0x20800020, 2430x00000000, 0x00808020, 0x00008020, 0x20808000, 2440x20008000, 0x00800000, 0x20808020, 0x20000000, 2450x20008020, 0x20800000, 0x00800000, 0x20808020, 2460x00008000, 0x00800020, 0x20800020, 0x00008020, 2470x00800020, 0x00000000, 0x20808000, 0x20000020, 2480x20800000, 0x20008020, 0x00000020, 0x00808000, 249); 250@SP3=( 2510x00080201, 0x02000200, 0x00000001, 0x02080201, 2520x00000000, 0x02080000, 0x02000201, 0x00080001, 2530x02080200, 0x02000001, 0x02000000, 0x00000201, 2540x02000001, 0x00080201, 0x00080000, 0x02000000, 2550x02080001, 0x00080200, 0x00000200, 0x00000001, 2560x00080200, 0x02000201, 0x02080000, 0x00000200, 2570x00000201, 0x00000000, 0x00080001, 0x02080200, 2580x02000200, 0x02080001, 0x02080201, 0x00080000, 2590x02080001, 0x00000201, 0x00080000, 0x02000001, 2600x00080200, 0x02000200, 0x00000001, 0x02080000, 2610x02000201, 0x00000000, 0x00000200, 0x00080001, 2620x00000000, 0x02080001, 0x02080200, 0x00000200, 2630x02000000, 0x02080201, 0x00080201, 0x00080000, 2640x02080201, 0x00000001, 0x02000200, 0x00080201, 2650x00080001, 0x00080200, 0x02080000, 0x02000201, 2660x00000201, 0x02000000, 0x02000001, 0x02080200, 267); 268@SP4=( 2690x01000000, 0x00002000, 0x00000080, 0x01002084, 2700x01002004, 0x01000080, 0x00002084, 0x01002000, 2710x00002000, 0x00000004, 0x01000004, 0x00002080, 2720x01000084, 0x01002004, 0x01002080, 0x00000000, 2730x00002080, 0x01000000, 0x00002004, 0x00000084, 2740x01000080, 0x00002084, 0x00000000, 0x01000004, 2750x00000004, 0x01000084, 0x01002084, 0x00002004, 2760x01002000, 0x00000080, 0x00000084, 0x01002080, 2770x01002080, 0x01000084, 0x00002004, 0x01002000, 2780x00002000, 0x00000004, 0x01000004, 0x01000080, 2790x01000000, 0x00002080, 0x01002084, 0x00000000, 2800x00002084, 0x01000000, 0x00000080, 0x00002004, 2810x01000084, 0x00000080, 0x00000000, 0x01002084, 2820x01002004, 0x01002080, 0x00000084, 0x00002000, 2830x00002080, 0x01002004, 0x01000080, 0x00000084, 2840x00000004, 0x00002084, 0x01002000, 0x01000004, 285); 286@SP5=( 2870x10000008, 0x00040008, 0x00000000, 0x10040400, 2880x00040008, 0x00000400, 0x10000408, 0x00040000, 2890x00000408, 0x10040408, 0x00040400, 0x10000000, 2900x10000400, 0x10000008, 0x10040000, 0x00040408, 2910x00040000, 0x10000408, 0x10040008, 0x00000000, 2920x00000400, 0x00000008, 0x10040400, 0x10040008, 2930x10040408, 0x10040000, 0x10000000, 0x00000408, 2940x00000008, 0x00040400, 0x00040408, 0x10000400, 2950x00000408, 0x10000000, 0x10000400, 0x00040408, 2960x10040400, 0x00040008, 0x00000000, 0x10000400, 2970x10000000, 0x00000400, 0x10040008, 0x00040000, 2980x00040008, 0x10040408, 0x00040400, 0x00000008, 2990x10040408, 0x00040400, 0x00040000, 0x10000408, 3000x10000008, 0x10040000, 0x00040408, 0x00000000, 3010x00000400, 0x10000008, 0x10000408, 0x10040400, 3020x10040000, 0x00000408, 0x00000008, 0x10040008, 303); 304@SP6=( 3050x00000800, 0x00000040, 0x00200040, 0x80200000, 3060x80200840, 0x80000800, 0x00000840, 0x00000000, 3070x00200000, 0x80200040, 0x80000040, 0x00200800, 3080x80000000, 0x00200840, 0x00200800, 0x80000040, 3090x80200040, 0x00000800, 0x80000800, 0x80200840, 3100x00000000, 0x00200040, 0x80200000, 0x00000840, 3110x80200800, 0x80000840, 0x00200840, 0x80000000, 3120x80000840, 0x80200800, 0x00000040, 0x00200000, 3130x80000840, 0x00200800, 0x80200800, 0x80000040, 3140x00000800, 0x00000040, 0x00200000, 0x80200800, 3150x80200040, 0x80000840, 0x00000840, 0x00000000, 3160x00000040, 0x80200000, 0x80000000, 0x00200040, 3170x00000000, 0x80200040, 0x00200040, 0x00000840, 3180x80000040, 0x00000800, 0x80200840, 0x00200000, 3190x00200840, 0x80000000, 0x80000800, 0x80200840, 3200x80200000, 0x00200840, 0x00200800, 0x80000800, 321); 322@SP7=( 3230x04100010, 0x04104000, 0x00004010, 0x00000000, 3240x04004000, 0x00100010, 0x04100000, 0x04104010, 3250x00000010, 0x04000000, 0x00104000, 0x00004010, 3260x00104010, 0x04004010, 0x04000010, 0x04100000, 3270x00004000, 0x00104010, 0x00100010, 0x04004000, 3280x04104010, 0x04000010, 0x00000000, 0x00104000, 3290x04000000, 0x00100000, 0x04004010, 0x04100010, 3300x00100000, 0x00004000, 0x04104000, 0x00000010, 3310x00100000, 0x00004000, 0x04000010, 0x04104010, 3320x00004010, 0x04000000, 0x00000000, 0x00104000, 3330x04100010, 0x04004010, 0x04004000, 0x00100010, 3340x04104000, 0x00000010, 0x00100010, 0x04004000, 3350x04104010, 0x00100000, 0x04100000, 0x04000010, 3360x00104000, 0x00004010, 0x04004010, 0x04100000, 3370x00000010, 0x04104000, 0x00104010, 0x00000000, 3380x04000000, 0x04100010, 0x00004000, 0x00104010, 339); 340 341sub main'des_set_key 342 { 343 local($param)=@_; 344 local(@key); 345 local($c,$d,$i,$s,$t); 346 local(@ks)=(); 347 348 # Get the bytes in the order we want. 349 @key=unpack("C8",$param); 350 351 $c= ($key[0] )| 352 ($key[1]<< 8)| 353 ($key[2]<<16)| 354 ($key[3]<<24); 355 $d= ($key[4] )| 356 ($key[5]<< 8)| 357 ($key[6]<<16)| 358 ($key[7]<<24); 359 360 &doPC1(*c,*d); 361 362 for $i (@shifts2) 363 { 364 if ($i) 365 { 366 $c=($c>>2)|($c<<26); 367 $d=($d>>2)|($d<<26); 368 } 369 else 370 { 371 $c=($c>>1)|($c<<27); 372 $d=($d>>1)|($d<<27); 373 } 374 $c&=0x0fffffff; 375 $d&=0x0fffffff; 376 $s= $skb0[ ($c )&0x3f ]| 377 $skb1[(($c>> 6)&0x03)|(($c>> 7)&0x3c)]| 378 $skb2[(($c>>13)&0x0f)|(($c>>14)&0x30)]| 379 $skb3[(($c>>20)&0x01)|(($c>>21)&0x06) | 380 (($c>>22)&0x38)]; 381 $t= $skb4[ ($d )&0x3f ]| 382 $skb5[(($d>> 7)&0x03)|(($d>> 8)&0x3c)]| 383 $skb6[ ($d>>15)&0x3f ]| 384 $skb7[(($d>>21)&0x0f)|(($d>>22)&0x30)]; 385 push(@ks,(($t<<16)|($s&0x0000ffff))&0xffffffff); 386 $s= (($s>>16)&0x0000ffff)|($t&0xffff0000) ; 387 push(@ks,(($s<<4)|(($s>>28)&0xf))&0xffffffff); 388 } 389 @ks; 390 } 391 392sub doPC1 393 { 394 local(*a,*b)=@_; 395 local($t); 396 397 $t=(($b>>4)^$a)&0x0f0f0f0f; 398 $b^=($t<<4); $a^=$t; 399 # do $a first 400 $t=(($a<<18)^$a)&0xcccc0000; 401 $a=$a^$t^(($t>>18)&0x00003fff); 402 $t=(($a<<17)^$a)&0xaaaa0000; 403 $a=$a^$t^(($t>>17)&0x00007fff); 404 $t=(($a<< 8)^$a)&0x00ff0000; 405 $a=$a^$t^(($t>> 8)&0x00ffffff); 406 $t=(($a<<17)^$a)&0xaaaa0000; 407 $a=$a^$t^(($t>>17)&0x00007fff); 408 409 # now do $b 410 $t=(($b<<24)^$b)&0xff000000; 411 $b=$b^$t^(($t>>24)&0x000000ff); 412 $t=(($b<< 8)^$b)&0x00ff0000; 413 $b=$b^$t^(($t>> 8)&0x00ffffff); 414 $t=(($b<<14)^$b)&0x33330000; 415 $b=$b^$t^(($t>>14)&0x0003ffff); 416 $b=(($b&0x00aa00aa)<<7)|(($b&0x55005500)>>7)|($b&0xaa55aa55); 417 $b=(($b>>8)&0x00ffffff)|((($a&0xf0000000)>>4)&0x0fffffff); 418 $a&=0x0fffffff; 419 } 420 421sub doIP 422 { 423 local(*a,*b)=@_; 424 local($t); 425 426 $t=(($b>> 4)^$a)&0x0f0f0f0f; 427 $b^=($t<< 4); $a^=$t; 428 $t=(($a>>16)^$b)&0x0000ffff; 429 $a^=($t<<16); $b^=$t; 430 $t=(($b>> 2)^$a)&0x33333333; 431 $b^=($t<< 2); $a^=$t; 432 $t=(($a>> 8)^$b)&0x00ff00ff; 433 $a^=($t<< 8); $b^=$t; 434 $t=(($b>> 1)^$a)&0x55555555; 435 $b^=($t<< 1); $a^=$t; 436 $t=$a; 437 $a=$b&0xffffffff; 438 $b=$t&0xffffffff; 439 } 440 441sub doFP 442 { 443 local(*a,*b)=@_; 444 local($t); 445 446 $t=(($b>> 1)^$a)&0x55555555; 447 $b^=($t<< 1); $a^=$t; 448 $t=(($a>> 8)^$b)&0x00ff00ff; 449 $a^=($t<< 8); $b^=$t; 450 $t=(($b>> 2)^$a)&0x33333333; 451 $b^=($t<< 2); $a^=$t; 452 $t=(($a>>16)^$b)&0x0000ffff; 453 $a^=($t<<16); $b^=$t; 454 $t=(($b>> 4)^$a)&0x0f0f0f0f; 455 $b^=($t<< 4); $a^=$t; 456 $a&=0xffffffff; 457 $b&=0xffffffff; 458 } 459 460sub main'des_ecb_encrypt 461 { 462 local(*ks,$encrypt,$in)=@_; 463 local($l,$r,$i,$t,$u,@input); 464 465 @input=unpack("C8",$in); 466 # Get the bytes in the order we want. 467 $l= ($input[0] )| 468 ($input[1]<< 8)| 469 ($input[2]<<16)| 470 ($input[3]<<24); 471 $r= ($input[4] )| 472 ($input[5]<< 8)| 473 ($input[6]<<16)| 474 ($input[7]<<24); 475 476 $l&=0xffffffff; 477 $r&=0xffffffff; 478 &doIP(*l,*r); 479 if ($encrypt) 480 { 481 for ($i=0; $i<32; $i+=4) 482 { 483 $t=((($r&0x7fffffff)<<1)|(($r>>31)&0x00000001)); 484 $u=$t^$ks[$i ]; 485 $t=$t^$ks[$i+1]; 486 $t2=(($t&0x0000000f)<<28); 487 488 $t=((($t>>4)&0x0fffffff)|(($t&0x0000000f)<<28)); 489 $l^= $SP1[ $t &0x3f]| 490 $SP3[($t>> 8)&0x3f]| 491 $SP5[($t>>16)&0x3f]| 492 $SP7[($t>>24)&0x3f]| 493 $SP0[ $u &0x3f]| 494 $SP2[($u>> 8)&0x3f]| 495 $SP4[($u>>16)&0x3f]| 496 $SP6[($u>>24)&0x3f]; 497 498 $t=(($l<<1)|(($l>>31)&0x1))&0xffffffff; 499 $u=$t^$ks[$i+2]; 500 $t=$t^$ks[$i+3]; 501 $t=((($t>>4)&0x0fffffff)|($t<<28))&0xffffffff; 502 $r^= $SP1[ $t &0x3f]| 503 $SP3[($t>> 8)&0x3f]| 504 $SP5[($t>>16)&0x3f]| 505 $SP7[($t>>24)&0x3f]| 506 $SP0[ $u &0x3f]| 507 $SP2[($u>> 8)&0x3f]| 508 $SP4[($u>>16)&0x3f]| 509 $SP6[($u>>24)&0x3f]; 510 } 511 } 512 else 513 { 514 for ($i=30; $i>0; $i-=4) 515 { 516 $t=(($r<<1)|(($r>>31)&0x1))&0xffffffff; 517 $u=$t^$ks[$i ]; 518 $t=$t^$ks[$i+1]; 519 $t=((($t>>4)&0x0fffffff)|($t<<28))&0xffffffff; 520 $l^= $SP1[ $t &0x3f]| 521 $SP3[($t>> 8)&0x3f]| 522 $SP5[($t>>16)&0x3f]| 523 $SP7[($t>>24)&0x3f]| 524 $SP0[ $u &0x3f]| 525 $SP2[($u>> 8)&0x3f]| 526 $SP4[($u>>16)&0x3f]| 527 $SP6[($u>>24)&0x3f]; 528 529 $t=(($l<<1)|(($l>>31)&0x1))&0xffffffff; 530 $u=$t^$ks[$i-2]; 531 $t=$t^$ks[$i-1]; 532 $t=((($t>>4)&0x0fffffff)|($t<<28))&0xffffffff; 533 $r^= $SP1[ $t &0x3f]| 534 $SP3[($t>> 8)&0x3f]| 535 $SP5[($t>>16)&0x3f]| 536 $SP7[($t>>24)&0x3f]| 537 $SP0[ $u &0x3f]| 538 $SP2[($u>> 8)&0x3f]| 539 $SP4[($u>>16)&0x3f]| 540 $SP6[($u>>24)&0x3f]; 541 } 542 } 543 &doFP(*l,*r); 544 pack("C8",$l&0xff, 545 ($l>> 8)&0x00ffffff, 546 ($l>>16)&0x0000ffff, 547 ($l>>24)&0x000000ff, 548 $r&0xff, 549 ($r>> 8)&0x00ffffff, 550 ($r>>16)&0x0000ffff, 551 ($r>>24)&0x000000ff); 552 } 553