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;
}