E
E
Egor Nameles2022-01-26 12:19:51
Java
Egor Nameles, 2022-01-26 12:19:51

How to fix errors in the Bit Encryption program?

According to the assignment, it is necessary to implement the modulo 2 gamma cipher. With this encryption method, the characters of the text and gamma are represented in binary form, and then each pair of binary digits is added modulo 2.

Only capital letters of the Russian alphabet are given. The binary character code must be taken according to Windows 1251. We start with the letter "A", that is, its decimal representation is 192, and binary is 1100 0000. We end with the symbol "I", the binary representation is 223, the decimal representation is 1101 1111.

Encryption example:
An example of encrypting the message "VOVA" using the key "YULIA". Since the length of the key is less than the length of the open message, it is cyclically repeated to generate the gamma. An example is shown in the picture.
61f1121b8eef7066674835.png

For me, the characters start with 1. I can’t change them so that they start with 192. Also, the program refuses to work without a space, I had to add it at the end, but it’s superfluous, I can’t get rid of it.
You also need to output the binary representation of the original text, the gamma (key) and the encrypted message.
Also, I can not make input from the keyboard in this program.

Tell me, please, how can I fix all these points in the program?

package com.company;

import java.util.Scanner;

public class Main {
    static String alfa = "АБВГДЕЖЗИКЛМНОПРСТУФХЦЧШЩЪЫьЭЮЯ ";

    public static void main(String[] args) {
        String ret;
        Scanner scanner = new Scanner(System.in);
        //String str = scanner.toString();
        String str = "ВОВА"; //sc.nextLine();
        //String key = scanner.toString();
        String key = "ЮЛЯ"; //sc.nextLine();
        //System.out.println("");
        System.out.print("Введите текст: ");
        System.out.println(str);
        System.out.print("Введите гамму (ключ): ");
        System.out.println(key);
        ret = cryptStr(str, key);
        System.out.print("Зашифрованный текст: ");
        System.out.println(ret);
        ret = cryptStr(ret, key);
        System.out.print("Расшифрованный текст: ");
        System.out.println(ret);
    }

    private static String cryptStr(String str, String key) {
        StringBuilder res = new StringBuilder();
        char[] codeKey = key.toCharArray(); // разложили ключ в байты
        for (int i = 0; i < codeKey.length; i++) codeKey[i] = (char) alfa.indexOf(codeKey[i]); // привели символы к номерам символов в alfa
        char[] codeStr = str.toCharArray(); // разложили строку в байты
        for (int i = 0; i < codeStr.length; i++) {
            codeStr[i] = (char) alfa.indexOf(codeStr[i]); // привели символ к номеру из alfa
            res.append(alfa.charAt(codeStr[i] ^ codeKey[i % codeKey.length])); // сохранили символ alfa с номером XOR кодов
        }
        return res.toString();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-01-26
@Akela_wolf

What for you are perverted with the line alpha?
Discover the String.getBytes() method and understand what character encoding is in general.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question