Advertisement
MarcelloGrechi

DES Java Class

Apr 20th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.39 KB | None | 0 0
  1. package applet;
  2.  
  3. public class Des
  4. {
  5.   protected static final int ENCRYPT = 0;
  6.   protected static final int DECRYPT = 1;
  7.   protected static int[] bytebit = { 128, 64, 32, 16, 8, 4, 2, 1 };
  8.  
  9.   protected static byte[] pc1 = {
  10.     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,
  11.     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 };
  12.  
  13.   protected static byte[] totrot = { 1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28 };
  14.   protected static byte[] pc2 = {
  15.     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,
  16.     50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
  17.  
  18.   protected static byte[] ip = {
  19.     57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15,
  20.     7, 56, 48, 40, 32, 24, 16, 8, 0, 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30,
  21.     22, 14, 6 };
  22.  
  23.   protected static byte[] ebit = {
  24.     31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9, 10, 11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17, 18, 19, 20, 19, 20, 21,
  25.     22, 23, 24, 23, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31 };
  26.  
  27.   protected static byte[][] Sn = {
  28.     { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1,
  29.     14, 8, 13, 6, 2, 11, 5, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 },
  30.     { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14,
  31.     7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 },
  32.     { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6,
  33.     4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 },
  34.     { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6,
  35.     9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 },
  36.     { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2,
  37.     1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 },
  38.     { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14,
  39.     15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 },
  40.     { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4,
  41.     11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 },
  42.     { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11,
  43.     4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };
  44.  
  45.   protected static byte[] p = {
  46.     16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 };
  47.  
  48.   protected static byte[] ip_1 = {
  49.     40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61,
  50.     29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49,
  51.     17, 57, 25 };
  52.   protected byte[] K;
  53.   protected int[][] Kn;
  54.  
  55.   public Des(int[] ai)
  56.   {
  57.     this.K = new byte[8];
  58.     this.Kn = new int[16][3];
  59.     byte[] abyte0 = new byte[56];
  60.     byte[][] abyte1 = new byte[16][28];
  61.     byte[][] abyte2 = new byte[16][28];
  62.     for (int i = 0; i < 8; i++) {
  63.       int j = ai[i];
  64.       int l = 128;
  65.       for (int k1 = 0; k1 < 7; k1++) {
  66.         j <<= 1;
  67.         l ^= j & 0x80;
  68.       }
  69.  
  70.       this.K[i] = (byte)(ai[i] & 0x7F | l);
  71.     }
  72.  
  73.     for (int k = 0; k < 56; k++) {
  74.       int i1 = pc1[k] >> 3;
  75.       int l1 = pc1[k] & 0x7;
  76.       abyte0[k] = (byte)((this.K[i1] & bytebit[l1]) != 0 ? 1 : 0);
  77.     }
  78.  
  79.     for (int j1 = 0; j1 < 16; j1++) {
  80.       for (int i2 = 0; i2 < 28; i2++) {
  81.         abyte1[j1][i2] = abyte0[((i2 + totrot[j1]) % 28)];
  82.         abyte2[j1][i2] = abyte0[((i2 + totrot[j1]) % 28 + 28)];
  83.       }
  84.  
  85.     }
  86.  
  87.     for (int j2 = 0; j2 < 16; j2++) {
  88.       for (int k2 = 0; k2 < 3; k2++) {
  89.         this.Kn[j2][k2] = 0;
  90.       }
  91.       for (int l2 = 0; l2 < 48; l2++)
  92.         if (pc2[l2] < 28)
  93.           this.Kn[j2][(l2 >> 4)] |= abyte1[j2][pc2[l2]] << 15 - (l2 & 0xF);
  94.         else
  95.           this.Kn[j2][(l2 >> 4)] |= abyte2[j2][(pc2[l2] - 28)] << 15 - (l2 & 0xF);
  96.     }
  97.   }
  98.  
  99.   public int[] decrypt_block(int[] ai, int i)
  100.   {
  101.     int j = i > 0 ? i : i % 8 != 0 ? i + (8 - i % 8) : 8;
  102.     long[] al = new long[2];
  103.     int[] ai1 = new int[j];
  104.     int k = 0;
  105.     for (; i > 0; i -= 8) {
  106.       int l = 0;
  107.       al[0] = 0L;
  108.       al[1] = 0L;
  109.       for (; l < 8; l++) {
  110.         al[(l >> 2)] |= (l < i ? ai[(l + k)] : 0) << 24 - ((l & 0x3) << 3);
  111.       }
  112.       long[] al1 = des_block(al, 1);
  113.       for (int i1 = 0; i1 < 8; i1++) {
  114.         ai1[(k + i1)] = (int)(al1[(i1 >> 2)] >> 24 - ((i1 & 0x3) << 3) & 0xFF);
  115.       }
  116.       k += 8;
  117.     }
  118.  
  119.     return ai1;
  120.   }
  121.  
  122.   public String decrypt_string(String s) {
  123.     String s1 = "";
  124.     int i = s.length() / 2;
  125.     int[] ai = new int[i];
  126.     for (int j = 0; j < i; j++) {
  127.       ai[j] = Integer.valueOf(s.substring(j * 2, j * 2 + 2), 16).intValue();
  128.     }
  129.     int[] ai1 = decrypt_block(ai, i);
  130.     for (int k = 0; k < i; k++) {
  131.       if (ai1[k] != 0)
  132.         s1 = s1 + (char)ai1[k];
  133.     }
  134.     return s1;
  135.   }
  136.  
  137.   protected long[] des_block(long[] al, int i) {
  138.     long[] al1 = new long[2];
  139.     long[] al2 = new long[17];
  140.     long[] al3 = new long[17];
  141.     int[] ai = new int[3];
  142.     int[] ai1 = new int[3];
  143.     byte[] abyte0 = new byte[8];
  144.     long tmp39_38 = 0L; al3[0] = tmp39_38; al2[0] = tmp39_38;
  145.     for (int j = 0; j < 32; j++) {
  146.       al2[0] |= ((al[(ip[j] >> 5)] & 1 << 31 - ip[j]) != 0L ? 1 : 0) << 31 - j;
  147.       al3[0] |= ((al[(ip[(j + 32)] >> 5)] & 1 << 31 - (ip[(j + 32)] & 0x1F)) != 0L ? 1 : 0) << 31 - j;
  148.     }
  149.  
  150.     for (int k = 1; k <= 16; k++) {
  151.       for (int l = 0; l < 3; l++) {
  152.         ai[l] = 0;
  153.       }
  154.       for (int i1 = 0; i1 < 48; i1++) {
  155.         ai[(i1 >> 4)] |= ((al3[(k - 1)] & 1 << 31 - ebit[i1]) != 0L ? 1 : 0) << (15 - i1 & 0xF);
  156.       }
  157.       if (i == 0) {
  158.         for (int j1 = 0; j1 < 3; j1++)
  159.           ai[j1] ^= this.Kn[(k - 1)][j1];
  160.       }
  161.       else {
  162.         for (int k1 = 0; k1 < 3; k1++) {
  163.           ai[k1] ^= this.Kn[(16 - k)][k1];
  164.         }
  165.       }
  166.       abyte0[0] = (byte)(ai1[0] >> 10);
  167.       abyte0[1] = (byte)(ai1[0] >> 4 & 0x3F);
  168.       abyte0[2] = (byte)((ai1[0] & 0xF) << 2 | ai1[1] >> 14);
  169.       abyte0[3] = (byte)(ai1[1] >> 8 & 0x3F);
  170.       abyte0[4] = (byte)(ai1[1] >> 2 & 0x3F);
  171.       abyte0[5] = (byte)((ai1[1] & 0x3) << 4 | ai1[2] >> 12);
  172.       abyte0[6] = (byte)(ai1[2] >> 6 & 0x3F);
  173.       abyte0[7] = (byte)(ai1[2] & 0x3F);
  174.       for (int i2 = 0; i2 < 8; i2++)
  175.         abyte0[i2] = (byte)(abyte0[i2] >> 1 & 0xF | abyte0[i2] & 0x20 | (abyte0[i2] & 0x1) << 4);
  176.       long l3;
  177.       long l4 = l3 = 0L;
  178.       for (int k2 = 0; k2 < 8; k2++) {
  179.         l4 |= Sn[k2][abyte0[k2]] << 28 - k2 * 4;
  180.       }
  181.       for (int i3 = 0; i3 < 32; i3++) {
  182.         l3 |= ((l4 & 1 << 32 - p[i3]) != 0L ? 1 : 0) << 31 - i3;
  183.       }
  184.       al2[k] = al3[(k - 1)];
  185.       al3[k] = (al2[(k - 1)] ^ l3);
  186.     }
  187.     long l1;
  188.     long l2 = l1 = 0L;
  189.     for (int j2 = 0; j2 < 32; j2++) {
  190.       if (ip_1[j2] <= 32)
  191.         l2 |= ((al3[16] & 1 << 32 - ip_1[j2]) != 0L ? 1 : 0) << 31 - j2;
  192.       else
  193.         l2 |= ((al2[16] & 1 << 32 - (ip_1[j2] - 32)) != 0L ? 1 : 0) << 31 - j2;
  194.       if (ip_1[(j2 + 32)] <= 32)
  195.         l1 |= ((al3[16] & 1 << 32 - ip_1[(j2 + 32)]) != 0L ? 1 : 0) << 31 - j2;
  196.       else {
  197.         l1 |= ((al2[16] & 1 << 32 - (ip_1[(j2 + 32)] - 32)) != 0L ? 1 : 0) << 31 - j2;
  198.       }
  199.     }
  200.     al1[0] = l2;
  201.     al1[1] = l1;
  202.     return al1;
  203.   }
  204.  
  205.   public int[] encrypt_block(int[] ai, int i) {
  206.     int j = i > 0 ? i : i % 8 != 0 ? i + (8 - i % 8) : 8;
  207.     long[] al = new long[2];
  208.     int[] ai1 = new int[j];
  209.     int k = 0;
  210.     for (; i > 0; i -= 8) {
  211.       int l = 0;
  212.       al[0] = 0L;
  213.       al[1] = 0L;
  214.       for (; l < 8; l++) {
  215.         al[(l >> 2)] |= (l < i ? ai[(l + k)] : 0) << 24 - ((l & 0x3) << 3);
  216.       }
  217.       long[] al1 = des_block(al, 0);
  218.       for (int i1 = 0; i1 < 8; i1++) {
  219.         ai1[(k + i1)] = (int)(al1[(i1 >> 2)] >> 24 - ((i1 & 0x3) << 3) & 0xFF);
  220.       }
  221.       k += 8;
  222.     }
  223.  
  224.     return ai1;
  225.   }
  226.  
  227.   public String encrypt_string(String s) {
  228.     String s1 = "";
  229.     int i = s.length();
  230.     int j;
  231.     int[] ai;
  232.     if (i > 0) {
  233.       int[] ai = new int[i];
  234.       int j = i > 0 ? i : i % 8 != 0 ? i + (8 - i % 8) : 8;
  235.       for (int k = 0; k < i; k++)
  236.         ai[k] = s.charAt(k);
  237.     }
  238.     else {
  239.       i = 1;
  240.       j = 8;
  241.       ai = new int[1];
  242.       ai[0] = 0;
  243.     }
  244.     int[] ai1 = encrypt_block(ai, i);
  245.     for (int l = 0; l < j; l++) {
  246.       if (ai1[l] < 16)
  247.         s1 = s1 + "0" + Integer.toHexString(ai1[l]);
  248.       else
  249.         s1 = s1 + Integer.toHexString(ai1[l]);
  250.     }
  251.     return s1.toUpperCase();
  252.   }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement