Skip to main content
gamedev.net
🔒 Locked

[java] Creating a file encryption algorithm

Started by Tokiko 4 replies 750+ views
Original Post
Tokiko
Tokiko
I have been learning java in my grade 11 programming class, and I recently started to use file input and output using command prompt. My teacher wants me for an exercise to create my own simple way of encrypting a file and decrypting a file. I haven't really messed around with it yet, so i'm just looking for some basic techniques or tips. I haven't learned really advanced stuff, I just finished the basic crap like loops etc. Is there a simple way to mix up a string and decode it without using any libraries? I dont need direct solutions just some advice, I want to do this on my own :) Thanks
Koroljov
Koroljov
The encryption used by caesar: get all the chars in the string and add some number to it.

Say the number is 2:
a->c
b->d

Make sure you start at 0 again when you run out of the range of a char.

(use String.charAt(someIndex) to get the chars and String.length() to get the number of chars)

A more complex encryption can be found here: matrix multiplication and encryption.
Why do my programs never work on other computers?
CaptainJester
CaptainJester
In between Koroljov's methods, you could use exclusive or(XOR). If you use a fixed byte, you XOR with each byte to encrypt and then XOR the same value with each encrypted byte to get back the original. The following program illustrates this:
public class TestApp {    public static void main(String args[]) {        int a=127;        int b=50;        System.out.println("a = " + a);        a = a ^ b;        System.out.println("a = " + a);        a = a ^ b;        System.out.println("a = " + a);    }}


Alternatively you could randomly generate the encryption byte and store it somewhere in the file(at the start) then on decryption you could read it in to decrypt. This byte would just look like another piece of encrypted data, so it would be hard to figure out what the key was by looking at multiple files.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Wrathnut
Wrathnut
This is probably a little late, but I saw this in my math book the other day and I thought it was kind of cool.
If you creat an invertable matrix, say a 3x3 matrix, and then break your string into even chunks and convert it to ASCII, in this case every 3 chars.
You can then encrypt your string by matrix multiplying your string chunk(vector) to your invertable matrix.
To decode it just multiply your encoded string chunk by the inverse of your invertable matrix.

Like this:

Encoding

Unicode string Encoded string

| 1,-2, 2|
[13, 5, 5] |-1, 1, 3|= [13,-26,21]
| 1,-1,-4|


Decoding
Encoded string Unicode string

|-1,-10,-8|
[13,-26,21] |-1, -6,-5|= [13, 5, 5]
| 0, -1,-1|

(I suck at ascii graphics sorry)

[Edited by - Wrathnut on October 31, 2004 8:48:27 PM]
seanw
seanw
Quote:
Original post by Koroljov
The encryption used by caesar: get all the chars in the string and add some number to it.


Off-topic, but I was reading a book that mentioned Caesar encryption and how it was used to stop Caesar's enemies from reading intercepted messages. It mentioned that today it would be trivial to crack, but back then it was more than sufficient since his enemies were illiterate. :)
caesar4
caesar4
u culd try sandorf's cypher
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.