Advertisement
Anton0093

Lab_6

Oct 27th, 2020
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. public class Des{
  2.  
  3.  
  4.     private final static int QuantityOfRounds = 3;
  5.  
  6.  
  7.     public static void main(String[] args){
  8.  
  9.         int[]OurLine = new int[]{1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}; //наша строчка
  10.  
  11.         System.out.println("Исходная строка: ");
  12.         PrintMyBlocks(OurLine);
  13.  
  14.         System.out.println("Наш ключ: ");
  15.         PrintMyBlocks(keygen(111450681)); //число можно взять любое в пределах int
  16.  
  17.         System.out.println("Строка после кодирования сетью Фейстеля: ");
  18.         PrintMyBlocks(FeistEncode(OurLine, keygen(111450681)));
  19.  
  20.         System.out.println("Строка после декодирования сетью Фейстеля: ");
  21.         PrintMyBlocks(FeistDEcode(OurLine, keygen(111450681)));
  22.     }
  23.  
  24.  
  25.     public static int[] keygen( int key){
  26.  
  27.         int[] generateKey = new int[QuantityOfRounds];
  28.  
  29.         for (int i = 0; i < QuantityOfRounds; i++) {
  30.             generateKey[i] = right(key,6) ^ left(key,7);
  31.         }
  32.         return generateKey;
  33.     }
  34.  
  35.     public static int[] FeistEncode(int[] input, int[] key) {
  36.  
  37.         for (int i = 0; i < input.length - 1; i += 2) {
  38.  
  39.                 //Разбиваем на левый и правый блок
  40.             int leftBlock = input[i];
  41.             int rightBlock = input[i + 1];
  42.  
  43.             for (int j = 0; j < QuantityOfRounds; j++) {
  44.                         //Начало шифрования
  45.                 if (j < QuantityOfRounds - 1) {
  46.  
  47.                     leftBlock = leftBlock ^ f(key[j] ^ rightBlock ,  key[j]);
  48.  
  49.                     int z = leftBlock;
  50.                     leftBlock = rightBlock; // меняем местами
  51.                     rightBlock = z;
  52.                 }
  53.                 else {
  54.                     leftBlock = leftBlock ^ f(key[j] ^ rightBlock ,  key[j]); //последний раунд без обмена
  55.                 }
  56.             }
  57.  
  58.             input[i] = leftBlock; //присваиваем элементам массива наши части
  59.             input[i+ 1] = rightBlock;
  60.         }
  61.         return input;
  62.     }
  63.  
  64.     public static void PrintMyBlocks(int[] blocks){
  65.  
  66.         for (int s : blocks ) {
  67.             System.out.print(Integer.toBinaryString(s));
  68.         }
  69.  
  70.         System.out.println("\n");
  71.     }
  72.  
  73.  
  74.     public static int[] FeistDEcode(int[] input, int[] key) {
  75.  
  76.         for (int i = 0; i < input.length - 1; i += 2) {
  77.  
  78.                 //Разбиваем на левый и правый блок
  79.             int leftBlock = input[i];
  80.             int rightBlock = input[i + 1];
  81.             int k = key.length - 1;
  82.  
  83.             for (int j = 0; j < QuantityOfRounds; j++) {
  84.                     //Начало шифрования
  85.                 if (j < QuantityOfRounds - 1) {
  86.  
  87.                     leftBlock = leftBlock ^ f(key[k] ^ rightBlock ,  key[j]);
  88.  
  89.                     int z = leftBlock;
  90.                     leftBlock = rightBlock; // меняем местами
  91.                     rightBlock = z;
  92.                 }
  93.                 else {
  94.  
  95.                     leftBlock = leftBlock ^ f(key[k] ^ rightBlock ,  key[j]); //последний раунд без обмена
  96.                 }
  97.             }
  98.  
  99.             input[i] = leftBlock;
  100.             input[i+ 1] = rightBlock;
  101.         }
  102.         return input;
  103.     }
  104.  
  105.  
  106.     public static int left(int key , int i ){
  107.  
  108.         return (key << i) | (key >> 32-i);
  109.     }
  110.  
  111.     public static int right(int key, int i ){
  112.  
  113.         return (key >> i) | (key << 32-i);
  114.     }
  115.  
  116.  
  117.     private static int f(int x, int k) {
  118.  
  119.         return ((x << 4)) ^ ((k << 6));
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement