I have been trying for so long to code an encryption/decryption algorithm using the modulus operator. Basically my code does a basic mathematical operation on given text and key then do a modulus 255 as in 255 ASCII characters. But I was never able to successfully reverse a modulus operator. I have finally programmed something that seems to work pretty well. It even encrypts then decrypts binary data to back to original.
I know it's never a wise idea to write your own encryption algorithm but I was just amazed with what I have come up with and would like to share it with critics to give me their opinions. Opinions on whether it is a viable encryption algorithm or really weak etc.
So here's the code. I appreciate any feedback.
By the way I called it MyCrypt. Pretty original name :)
public static byte[] encrypt(byte[] text, String key)
{
byte[] b = new byte[text.length];
int keyIntTotal = 0;
for (int p = 0; p < key.length(); p++)
{
keyIntTotal += key.charAt(p);
}
for (int i = 0; i < text.length; i++)
{
int alpha = text[i];
for (int j = 0; j < key.length(); j++)
{
alpha += (text.length + (int)key.charAt(j) * (i ^ j) * keyIntTotal) % 255;
}
b[i] = (byte) (alpha % 256);
}
return b;
}
public static byte[] decrypt(byte[] cipherText, String key)
{
byte[] b = new byte[cipherText.length];
int keyIntTotal = 0;
for (int p = 0; p < key.length(); p++)
{
keyIntTotal += key.charAt(p);
}
for (int i = 0; i < cipherText.length; i++)
{
int alpha = cipherText[i];
for (int j = 0; j < key.length(); j++)
{
alpha -= (cipherText.length + (int) key.charAt(j) * (i ^ j) * keyIntTotal) % 255;
}
alpha *= -1;
b[i] = (byte) ((256 - alpha) % 256);
}
return b;
}
-
\$\begingroup\$ There are 128 ASCII characters, not 255. Also, it appears to be much less efficient than most stream ciphers, as the work done is O(n·m) (where n is message length and m is key length) rather than the more usual O(n). \$\endgroup\$Toby Speight– Toby Speight2022年07月21日 06:55:52 +00:00Commented Jul 21, 2022 at 6:55
-
\$\begingroup\$ Well, 255 different bytes i meant.. Extended ASCII \$\endgroup\$Izzy Kiefer– Izzy Kiefer2022年07月21日 18:34:54 +00:00Commented Jul 21, 2022 at 18:34
1 Answer 1
whether it is a viable encryption algorithm
It is not.
Rewrite the encryption (pretty mechanically) as
int alpha = text[i];
int alpha_shift = 0;
for (int j = 0; j < key.length(); j++)
{
alpha_shift += (text.length + (int)key.charAt(j) * (i ^ j) * keyIntTotal) % 255;
}
b[i] = (byte) ((alpha + alpha_shit) % 256);
and observe that alpha_shift
only depends on the character's position i
. It means that an attacker doesn't need to know the key. It only need to ask the victim to encrypt few plaintexts. Very few, because there is no avalanche effect.
For a proper review, no naked loops. The shift computation shall be factored out into a function:
int alpha = text[i];
b[i] = (byte) ((alpha + alpha_shift(key, i)) % 256;
-
\$\begingroup\$ I appreciate your warnings and clarifications. I will consider your advise .Thank you so much. \$\endgroup\$Izzy Kiefer– Izzy Kiefer2022年07月28日 02:28:42 +00:00Commented Jul 28, 2022 at 2:28