A
A
Arti-Jack2016-11-06 22:12:43
Java
Arti-Jack, 2016-11-06 22:12:43

What is the correct way to change the encryption algorithm based on XOR?

Good day.
I have an implementation of an encryption algorithm:

String mesage = "Привет мир";
        byte[] m = Calculate.encrypt(mesage, "тостер");
        for (byte i : m) System.out.print(i);
        System.out.println();
        System.out.println(Calculate.decrypt(m, "тостер"));

public class Calculate {


    public static byte[] encrypt(String text, String keyWord)
    {
        byte[] arr = text.getBytes();
        byte[] keyarr = keyWord.getBytes();
        byte[] result = new byte[arr.length];
        for(int i = 0; i< arr.length; i++)
        {
            result[i] = (byte) (arr[i] ^ keyarr[i % keyarr.length]);
        }
        return result;
    }


    public static String decrypt(byte[] text, String keyWord)
    {
        byte[] result  = new byte[text.length];
        byte[] keyarr = keyWord.getBytes();
        for(int i = 0; i < text.length;i++)
        {
            result[i] = (byte) (text[i] ^ keyarr[i% keyarr.length]);
        }
        return new String(result);
    }

}

In general, the algorithm is working. But there is one significant problem that is associated with the array m.
f636dbf574fc41bd89fdc61b96731442.png
For example, if I write an application that can encrypt and decrypt words, I can only encrypt words with this algorithm. This is due to the fact that when I try to decrypt the message, I do not have the encrypted message itself in the correct format. That is, I need some way to bring the text (with the TextField "enter encoded message") into such a format so that it can be correctly pushed into the m array . How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-11-07
@Arti-Jack

The most normal option is to encode the ciphertext in base64 .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question