Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Des{
- private final static int QuantityOfRounds = 3;
- public static void main(String[] args){
- int[]OurLine = new int[]{1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0}; //наша строчка
- System.out.println("Исходная строка: ");
- PrintMyBlocks(OurLine);
- System.out.println("Наш ключ: ");
- PrintMyBlocks(keygen(111450681)); //число можно взять любое в пределах int
- System.out.println("Строка после кодирования сетью Фейстеля: ");
- PrintMyBlocks(FeistEncode(OurLine, keygen(111450681)));
- System.out.println("Строка после декодирования сетью Фейстеля: ");
- PrintMyBlocks(FeistDEcode(OurLine, keygen(111450681)));
- }
- public static int[] keygen( int key){
- int[] generateKey = new int[QuantityOfRounds];
- for (int i = 0; i < QuantityOfRounds; i++) {
- generateKey[i] = right(key,6) ^ left(key,7);
- }
- return generateKey;
- }
- public static int[] FeistEncode(int[] input, int[] key) {
- for (int i = 0; i < input.length - 1; i += 2) {
- //Разбиваем на левый и правый блок
- int leftBlock = input[i];
- int rightBlock = input[i + 1];
- for (int j = 0; j < QuantityOfRounds; j++) {
- //Начало шифрования
- if (j < QuantityOfRounds - 1) {
- leftBlock = leftBlock ^ f(key[j] ^ rightBlock , key[j]);
- int z = leftBlock;
- leftBlock = rightBlock; // меняем местами
- rightBlock = z;
- }
- else {
- leftBlock = leftBlock ^ f(key[j] ^ rightBlock , key[j]); //последний раунд без обмена
- }
- }
- input[i] = leftBlock; //присваиваем элементам массива наши части
- input[i+ 1] = rightBlock;
- }
- return input;
- }
- public static void PrintMyBlocks(int[] blocks){
- for (int s : blocks ) {
- System.out.print(Integer.toBinaryString(s));
- }
- System.out.println("\n");
- }
- public static int[] FeistDEcode(int[] input, int[] key) {
- for (int i = 0; i < input.length - 1; i += 2) {
- //Разбиваем на левый и правый блок
- int leftBlock = input[i];
- int rightBlock = input[i + 1];
- int k = key.length - 1;
- for (int j = 0; j < QuantityOfRounds; j++) {
- //Начало шифрования
- if (j < QuantityOfRounds - 1) {
- leftBlock = leftBlock ^ f(key[k] ^ rightBlock , key[j]);
- int z = leftBlock;
- leftBlock = rightBlock; // меняем местами
- rightBlock = z;
- }
- else {
- leftBlock = leftBlock ^ f(key[k] ^ rightBlock , key[j]); //последний раунд без обмена
- }
- }
- input[i] = leftBlock;
- input[i+ 1] = rightBlock;
- }
- return input;
- }
- public static int left(int key , int i ){
- return (key << i) | (key >> 32-i);
- }
- public static int right(int key, int i ){
- return (key >> i) | (key << 32-i);
- }
- private static int f(int x, int k) {
- return ((x << 4)) ^ ((k << 6));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement