DES algorithm, CPP file #include i , in = 8, out = 8) des_block (in, out);}; // ---------------------------- ------------------------------------------ // des_block // Encrpts / Decrypts (According to the key currently loaded int the inter key register) ONE BLOCK OF Eight Bytes At Address 'In' // INTO The Block at Address 'Out'. They can be the same. //// "in" / / "OUT" // -------------------------------------------------------------------------------------------------------- --------------------------- void des :: des_block (unsigned char * in, unsigned char * out) {unsigned long work [2] Scrunch (in, Work); Desfunc (Work, KNL); Unscrun (Work, Out);} // ------------------------- --------------------------------------------- // deskey // sets the internal key register (KnR) according to the hexadecimal // key contained in the 8 bytes of hexkey, according to the DES, // for encryption or decrytion according to MODE // // "key" is the 64 bits key. // "MD" means encryption or decryption. // -------------------------------------- -------------------------------- Void des :: Deskey (unsigned ch Ar Key [8], MODE MD) / * THANKS TO JAMES GILLOGLY & Phil Karn! * / {Register INT I, J, L, M, N; Unsigned Char PC1M [56], PCR [56]; unsigned long kn [ 32]; for (j = 0; j <56; j ) {l = pc1 [j]; m = l & 07; pc1m [j] = (key [l >> 3] & Bytebit [M])? 1 : 0;} for (i = 0; i <16; i ) {if (md == Decrypt) m = (15 - i) << 1; ELSE M = i << 1; n = m 1; kn [m] = kN [n] = 0L; 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 ]]]) KN [M] | = Bigbyte [J]] KN [N] | = Bigbyte [J];}} cookey (kn); Return;} // -------------------------------------------------- ------------------- // Cookey // Only Called by deskey. // ------------------- -------------------------------------------------- - Void des :: Cookey (register unsigned long * cook, * raw0; unsigned long dough [32]; register int i; cook = dough; for (i = 0; i <16; i , Raw1 ) {raw0 = Raw1 ; * cook = (* Raw0 & 0x00FC0000L) << 6; * cook | = (* Raw0 & 0x00000FC0L) << 10; * cook | = (* Raw1 & 0x00FC0000L) >> 10; * Cook | = (* Raw1 & 0x00000FC0L) >> 6; * Cook = (* Raw0 & 0x0003F000L) << 12; * cook | = (* Raw0 & 0x0000003FL) << 16; * cook | = (* Raw1 & 0x0003F000L) >> 4; * Cook | = (* Raw1 & 0x0000003FL);} Usekey (DOUGH); Return;} // ------------------------ -------------------------------------------- // UseKey / / Only Called By Co Okey. // Loads The Interal Key Register with the data in cookedKey. // --------------------------------- -------------------------------------- Void des :: UseKey (Register unsigned long * from) {register unsigned long * to, * ENDP; to = KNL, ENDP = & kNL [32]; while (to (* OUTOF & 0xffl) << 8; * INTO | = (* Outof & 0xffl); * INTO = (* Outof & 0xffl) << 24; * INTO | = (* Outof & 0xffl) << 16; * INTO | = (* OUTOF & 0xffl) << 8; * INTO | = (* Outof & 0xffl); Return;} void des :: Unscrun (register unsigned long * outof, register unsigned char * inTo) {* INTO = (* OUTOF >> 24) & 0xffl; * INTO = (* Outof >> 16) & 0xffl; * INTO = (* Outof >> 8) & 0xffl; * INTO = * OUTOF & 0xffl; * INTO = (* Outof >> 24) & 0xffl; * INTO = (* Outof >> 16) & 0xffl; * INTO = (* Outof >> 8) & 0xffl; * @ = * Outof & 0xffl; return;} void des :: Desfunc (register unsigned Long * Block, Register unsigned long * keys) {register unsigned long fval, work, right, leftt; register int round; leftt = block [0]; Right = Block [1]; Work = ((Leftt >> 4) ^ Right) & 0x0f0f0f0fl; Right ^ = Work; LEFTT ^ = (Work << 4); Work = ((Leftt >> 16) ^ Right) & 0x0000FFFFL; Right ^ = Work; Leftt ^ = (Work << 16); Work = ((Right >> 2) ^ leftt) & 0x333333331; Leftt ^ = Work; Right ^ = (Work << 2); Work = ((Right >> 8) ^ lef TT) & 0x00FF00FFL; LEFTT ^ = Work; Right ^ = (Work << 8); Right = ((Right << 1) | ((Right >> 31) & 1L) & 0xfffffffl; Work = (Leftt ^ Right ) & 0xAaaaaAaAl; LEFTT ^ = Work; Right ^ = Work; Leftt = ((Leftt << 1) | ((Leftt >> 31) & 1L) & 0xffffffl; for (Round = 0; Round <8; Round ) {Work = (Right << 28) | (Right >> 4); Work ^ = * Keys ; Fval = SP7 [Work & 0x3fl]; FVAL | = SP5 [(Work >> 8) & 0x3fl]; FVAL | = SP3 [(Work >> 16) & 0x3fl]; FVAL | = SP1 [(Work >> 24) & 0x3fl]; Work = Right ^ * Keys ; FVAL | = SP8 [Work & 0x3fl]; fval | = SP6 [(Work >> 8) & 0x3fl]; FVAL | = SP4 [(Work >> 16) & 0x3fl]; FVAL | = SP2 [(Work >> 24) & 0x3fl]; Leftt ^ = fval; work = (Leftt << 28) | (Leftt >> 4); Work ^ = * Keys ; Fval = SP7 [Work & 0x3fl]; FVAL | = SP5 [(Work >> 8) & 0x3fl]; Fval | = SP3 [(Work> > 16) & 0x3fl]; FVAL | = SP1 [(Work >> 24) & 0x3fl]; Work = Leftt ^ * Keys ; FVAL | = SP8 [Work & 0x3fl]; Fval | = SP6 [(Work >> 8) & 0x3fl]; FVAL | = SP4 [(Work >> 16) & 0x3fl]; FVAL | = SP2 [(Work >> 24) & 0x3fl]; Right ^ = fval;} Right = (Right << 31) | Right >> 1); Work = (leftt ^ Right) & 0xAAAAAAAAl; Leftt ^ = Work; Right ^ = Work; LEFTT = (Leftt << 31) | (Leftt >> 1); Work = ((Leftt >> 8 ) ^ Right) & 0x00FF00FFL; Right ^ = Work; Leftt ^ = (Work << 8); Work = ((Leftt >> 2) ^ Right) & 0x33333333L; Right ^ = Work; LEFTT ^ = (Work << 2); Work = ((Right >> 16) ^ leftt) & 0x0000FFFFL; Leftt ^ = Work; Right ^ = (Work << 16); Work = ((Right >> 4) ^ leftt) & 0x0f0f0f0fl; Leftt ^ = Work; Right ^ = (Work << 4); * block = Right; * block = leftt; return; } // ----------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ // Initial of Static Data Me MEMBERS. THESE DATA WILL BE Used by All the // Instances of Class, And Can Not Be Changed. // ---------------------------------------------- ------------------------- Unsigned Char des :: DF_KEY [24] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAb, 0xcd, 0xef, 0x98, 0x76, 0x54, 0x32, 0x10, 0x89, 0xAb, 0xcd, 0x, 0x01, 0x23, 0x45, 0x67}; unsigned short des :: Bytebit [8] = {0200, 0100, 040, 020, 010, 04, 02, 01}; Unsigned long des :: Bigbyte [24] = {0x800000L, 0x400000L, 0x200000L, 0x100000L, 0x80000L, 0x10000L, 0x8000L, 0x4000L, 0x2000L, 0x1000L, 0x800L, 0x400L, 0x200L, 0x100L, 0x80L, 0x40L, 0x20L, 0x10L, 0x8L, 0x4L, 0x2L, 0x1L}; unsigned char des :: PC1 [56] = {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}; Unsigned Char des :: Totrot [16] = {1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28} Unsigned char des :: PC2 [48] = {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}; unsigned long des :: sp1 [64] = {0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L, 0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L, 0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L, 0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L, 0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L, 0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L, 0x00010004L , 0x01000004L, 0x01000004L, 0x00010004L, 0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L, 0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L, 0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L, 0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L, 0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L, 0x01010404L, 0x00010004L , 0x01010000L, 0x01000404L, 0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L, 0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L, 0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L}; unsigned long DES :: SP2 [64] = {0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L, 0x00100000L , 0x00000020L, 0x80100020L, 0x80000020L, 0x80108020L, 0x8 0108000L, 0x80000000L, 0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L, 0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L, 0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L, 0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L, 0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L, 0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L, 0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L, 0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L, 0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L, 0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L, 0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L, 0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L, 0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L}; unsigned long des :: sp3 [64] = {0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L, 0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L, 0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L, 0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L, 0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L, 0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L, 0x08000208L , 0x00020200L, 0x00020000L, 0x08000208L, 0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L, 0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L, 0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L, 0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L, 0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L, 0x08000208L, 0x00020000L , 0x08000000L, 0x08020208L, 0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L, 0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L, 0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L}; unsigned long DES :: SP4 [64] = {0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L, 0x00802080L 0x00800081L, 0x00800001L, 0x00002001L, 0x00000000L, 0x00802000L, 0x0 0802000L, 0x00802081L, 0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L, 0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L, 0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L, 0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L, 0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L, 0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L, 0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L, 0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L, 0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L, 0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L, 0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L, 0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L, 0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L}; unsigned long des :: sp5 [64] = {0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L, 0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L, 0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L, 0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L, 0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L, 0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L, 0x42080000L , 0x40000100L, 0x00000000L, 0x42000000L, 0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L, 0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L, 0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L, 0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L, 0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L, 0x42080100L, 0x00080100L , 0x42000000L, 0x42080100L, 0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L, 0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L, 0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L}; unsigned long DES :: SP6 [64] = {0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L, 0x20400000L 0x00000010L, 0x20404010L, 0x00400000L, 0x20004000L, 0x00404010L, 0x0 0400000L, 0x20000010L, 0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L, 0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L, 0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L, 0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L, 0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L, 0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L, 0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L, 0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L, 0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L, 0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L, 0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L, 0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L, 0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L}; unsigned long des :: SP7 [64] = {0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L, 0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L, 0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L, 0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L, 0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L, 0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L, 0x04200000L , 0x00000800L, 0x00000802L, 0x04200802L, 0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L, 0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L, 0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L, 0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L, 0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L, 0x00000802L, 0x04000002L , 0x04200802L, 0x04200000L, 0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L, 0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L, 0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L}; unsigned long DES :: SP8 [64] = {0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L, 0x10000000L 0x10001040L, 0x00000040L, 0x10000000L, 0x00040040L, 0x10040000L, 0x1 0041040L, 0x00041000L, 0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L, 0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L, 0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L, 0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L, 0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L, 0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L, 0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L, 0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L, 0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L, 0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L, 0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L, 0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L, 0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L};