Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → Java question..
Java question..
2008-01-27, 5:33 PM #1
Is it possible to write a "decrypter" method for this class?

I came up with the concept for the encryption, but I'm completely puzzled on how to decrypt it..
Code:
//Reid wrote this so don't steal it
import javax.swing.JOptionPane;

public class EncTest {

    public static void main(String[] args) {
    	int[] a = {251,214,120,1,40,190,254,27};
    	int[] b = {212,232,54,135,244,15,179,80};
    	String test = JOptionPane.showInputDialog("Input a string.");
    	String key = JOptionPane.showInputDialog("Input a key.");
    	int key1 = checksum(key);
    	int key2 = checksum(b);
    	enctest(test,key1);
    }
    public static int checksum(String key){
    	int z = 0;
    	int y = 0;
    	byte[] theKey = key.getBytes();
    	for(int x = 0;x < key.length(); x++){
    		z += theKey[x];
    	}
    	y = z/256;
    	y = y*256;
    	return z-y;
    }
    public static int checksum(int[] bytes){
    	int z = 0;
    	int y = 0;
    	for(int x = 0;x < bytes.length; x++){
    		z += bytes[x];
    	}
    	y = z/256;
    	y = y*256;
    	return z-y;
    }
    public static void enctest(String enc, int key){
    	byte[] string = enc.getBytes();
    	for(int x = 0;x<enc.length();x++){
    		string[x] = new Integer(((int)string[x]*key)%256).byteValue();
    	}
    	String final1 = new String(string);
    	JOptionPane.showMessageDialog(null,final1);
    }
}
2008-01-27, 5:55 PM #2
How about you write your encryption method as pseudo code, the fact that it's Java and has a GUI is completely irrelevant to whether it can be decrypted or not.
Detty. Professional Expert.
Flickr Twitter
2008-01-27, 6:18 PM #3
Byte * Key % 256 -> Encrypted Byte
2008-01-27, 8:01 PM #4
There's no reverse function for the modulus operator, if that's what you're asking.
2008-01-27, 8:11 PM #5
You drink it.
2008-01-27, 8:52 PM #6
I know theres no reverse for the modulus function, but since every byte is different in the end result, wouldn't a decrypter still be feasible?
2008-01-27, 8:54 PM #7
[quote=Connection Problem]There's no reverse function for the modulus operator, if that's what you're asking.[/quote]

Unless of course, you save that information as well... if you think about it, you can identify modulus values by their "order"

a first order of mod 256 would be a number between 0 and 255 (I'm just considering positive numbers :p), a second order would be 256-511, etc
there are several ways of doing this, you can either make two variables for the encryption, one to hold the modded value, and the other to hold the orders. For example:
modded: 12,15,231,127
order: 0,4,0,1

would signify the values 12,1039,231,383

There are many other options as well, such as including the order as a decimal, ex:
modded: 12.0, 15.4, 231.0, 127.1

So long as you have some method of keeping track of the order and the modded value, you can undo a mod.
Sam: "Sir we can't call it 'The Enterprise'"
Jack: "Why not!"
2008-01-27, 8:56 PM #8
Alright, I'll look into that. Thanks for the advice.
2008-01-27, 9:05 PM #9
Actually, I just noticed a method that might work without storing the modulus data. I'll get back if I'm successful...
2008-01-28, 12:46 AM #10
Maybe Byte xor Key. :P
2008-01-28, 8:38 AM #11
I'm sure there are plenty of other ways besides storing the order, that's just the only one I could be bothered to think up last night ;)
Sam: "Sir we can't call it 'The Enterprise'"
Jack: "Why not!"
2008-01-28, 9:38 AM #12
Code:
    	//more of Reid's code. not complete, duh.
    	int keymod = key%256;
    	for(int i = 0;i<256;i++){
    		while(encrypted%keymod!=0){
    			encrypted += 256;
    		}
    		decrypted=encrypted/keymod;
    		System.out.println(i+" "+decrypted);
    	}


Win.
Code:
/**
 * @(#)enc.java
 *
 *
 * @author 
 * @version 1.00 2008/1/28
 */

public class enc {

    public static void main(String[] args) {
    	int[] encrypted = new int[256];
    	int[] decrypted = new int[256];
    	int key = 643;
    	for(int i = 0;i<256;i++){
    		encrypted=i*key%256;
    		System.out.println(i+" "+encrypted);
    	}
    	int keymod = key%256;
    	for(int i = 0;i<256;i++){
    		while(encrypted%keymod!=0){
    			encrypted += 256;
    		}
    		decrypted=encrypted/keymod;
    		System.out.println(i+" "+decrypted);
    	}
    }
    
}


Heres a fully testable version.

P.S. it doesn't like even keys, when I get the time I'll fix it.
2008-01-28, 4:30 PM #13
Sorry, I think I missed the part where it's a good idea to write your own encryption algorithm.

There are two types of people that attempt to write encryption algorithms: those that know very little number theory and those that know a lot of number theory. One of those types produces good algorithms, and also doesn't need to post questions to a internet forum asking how their new encryption scheme can be decrypted.

If you want to code an encryption routine, I suggest you download the algorithm of a recognised scheme and implement it rather than coming up with your own
2008-01-28, 4:37 PM #14
I was just writing this for my own enjoyment.

I don't intend to implement it into anything...
2008-01-28, 4:42 PM #15
Also, to your problem, you'll want to google for "chinese remainder theorem". It's been a while since i've done it, but i believe that's the way to solve modulo equations.

Your scheme is also hopelessly weak against frequency analysis.
2008-01-29, 2:21 AM #16
Originally posted by Reid:
P.S. it doesn't like even keys, when I get the time I'll fix it.


Now I'm a bit more awake, I can immediately see why this would be. Consider encrypting the byte 0x01 (1) and the byte 0x81 (129) with the even key 2n:

0x01: 1 * 2n= 2n
0x81: 129 * 2n = (128 * 2n) + (1 * 2n) = 256n + 2n

Now perform the modulo 256:
0x01: 2n % 256
0x81: (256n + 2n) % 256 = 2n % 256

I wasn't trying to rain on your parade when suggesting you not try and invent your own encryption algorithm. It really is really hard. If you're doing it for fun, I reckon there is sufficient programming challenge in implementing a known algorithm in a secure and efficient way. Try a google search for "FIPS 197" to get the spec of the AES algorithm.

↑ Up to the top!