# Need Help Reversing my Encryption Algorithm [closed]

So I wrote a simple encryption algorithm (Not very secure, just a starting point. I don't want criticism on it.) and I have been having trouble finding a way to decrypt the text. The algorithm is here (You may need an ASCII table to understand it):

Key = key
DecimalKey: k + e + y = 107 + 101 + 121 = 329 / 3(Length of actual key) = 110
Each Char: (charDec * keyLength) - DecimalKey

Example: p (112 * 3 = 336) - 110 = 226


Then I would subtract 127 from 226 so it will correspond to ASCII (this will be checked each time with an if statement).

My Java implementation of this is below (Hopefully code is allowed?):

public static String encrypt(String str, String key) {
String encrypted = "";
char[] strChars = new char[str.length()];
char[] keyChars = new char[key.length()];
int[] strDecs = new int[strChars.length];
int[] keyDecs = new int[keyChars.length];
int[] strPDecs = new int[strDecs.length];
int keyNum = 0;
for(int i = 0; i < str.length(); i++) {
strChars[i] = str.charAt(i);
}
for(int i = 0; i < key.length(); i++) {
keyChars[i] = key.charAt(i);
}
for(int i = 0; i < strDecs.length; i++) {
strDecs[i] = Integer.valueOf((char)strChars[i]);
}
for(int i = 0; i < keyDecs.length; i++) {
keyDecs[i] = Integer.valueOf((char)keyChars[i]);
}
for(int i : keyDecs) {
keyNum += i;
}
float fTempKeyNum = keyNum;
fTempKeyNum /= keyChars.length;
keyNum = Math.round(fTempKeyNum);
for(int i = 0; i < strDecs.length; i++) {
strPDecs[i] = (strDecs[i] * keyChars.length) - keyNum;
while(strPDecs[i] > 126) {
strPDecs[i] -= 127;
}
while(strPDecs[i] < 0) {
strPDecs[i] = 127 - Math.abs(strPDecs[i]);
}
if(strPDecs[i] < 32) {
strPDecs[i] = 126 - Math.abs(strPDecs[i]);
}
}
for(int i : strPDecs) {
encrypted += Character.toString((char) i);
}
return encrypted;
}


So can someone help me find a way to reverse this algorithm? Sorry if I got some terms wrong, as I am quite new to Cryptography. If more explanation is required I can probably post a bit more.

-

## closed as off-topic by Henrick Hellström, B-Con, e-sushi, John Deters, AFSMar 2 at 0:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Requests for analyzing or deciphering a block of data are off-topic here, as the results are rarely useful to anyone else." – Henrick Hellström, B-Con, e-sushi, John Deters, AFS
If this question can be reworded to fit the rules in the help center, please edit the question.

This is off topic, because any answer would be of little or no use to anyone else. If you want to read up on one common way of making encryption algorithms invertible, you could e.g. check out en.wikipedia.org/wiki/Feistel_cipher –  Henrick Hellström Mar 1 at 1:16

Unfortunately, you have created an un-invertible cipher, that is why you can't decrypt it.

Then I would subtract 127 from 226 so it will correspond to ASCII

Since you do charDec * keyLength this will not work when the key is longer (Imagine a 10 char key, you would get 1120 as your intermediate result, you won't get this back into ASCII range with your final subtraction).

So what you would need is to take your result modulo 256 (or modulo 127 if you want to stay in the ASCII range). But then your scheme will basically amount to a % b = c, where to decrypt you have to recompute a, from b and c. There is an infinite number of solutions. You cannot decrypt.

You could study the design and internal operations of an existing cipher like DES, to see how they achieve invertibility and confusion at the same time.

-
while(strPDecs[i] > 126) { strPDecs[i] -= 127; } It will keep subtracting 127 until the number is smaller then 127. –  MrLolEthan Mar 2 at 12:24
You are correct though. It is un-invertible. –  MrLolEthan Mar 2 at 12:26

Maybe there is some hope for decryption... if you use downcase with small keys.

The downcase alphabet ascii codes are $[97..122]$, so if you use a $N$-char key with average ascii $k_{avg}$, you can estimate that in most cases a message $m$ will give $$97*N-k_{avg} \leq m*N - k_{avg} \leq 122*N- k_{avg}$$

Hence the ciphertext $c = (m*N - k_{avg}) \mod_{127}$ should be decrypted if the equation for $x$ has unique solution: $$97 \leq m' = (c + 127*x + k_{avg})/N \leq 122$$ $$(97*N-k_{avg}-c)/127 \leq x \leq (122*N-k_{avg}-c)/127$$

You forced $c$ to be in $[0..126]$ and again assuming downcase only (or almost), we can say $97 \leq k_{avg} \leq 122$. So we get $97 \leq c+k_{avg}\leq 248$ and: $$\frac{97}{127}N - \frac{248}{127} \leq x \leq \frac{122}{127}N - \frac{97}{127}$$

If we want the integer solution for $x$ to be unique, we need: $$\left(\frac{122}{127}N - \frac{97}{127}\right) - \left(\frac{97}{127}N - \frac{248}{127}\right) < 2$$

Sadly, the positive solutions are only $1,2,3,4$. If you use N>4, maybe you won't recover anything from chiphertext.

Anyway, your function is not invertible. These restrictions could work for downcase text and passwords, but unexpected results may appear with punctuation, blank spaces, uppercase ...

-