Unusual string insertion question

Hi everybody,

I'm trying an experiment with "encoding" a text file, and I was wondering, is there a specific way to insert a random character, say, "&", into random places in a text string a random number of times? I've heard of Math.random, but I don't think that would do what I'm looking for, can anyone help me out?

Thanks,

Jezzica85

[370 byte] By [jezzica85a] at [2007-11-27 11:18:38]
# 1

What do you mean by "encoding"?

As for "insert a random character into random places in a text string a random number of times," there isn't one single method that would do that. You need to break it down into pieces.

Generate random numbers.

Map a number to a character.

Insert a character at a spot in a String.

You could use Math.random to generate the random number, but java.util.Random is easier and gives better distribution.

jverda at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks jverd,

What I mean by encoding is basically just transforming the file to human-readable gibberish, so people can't just decide to look at it, unless they have my decryption program. It's just an experiment thanks to being bored on the weekend, really.

Thanks,

Jezzica85

jezzica85a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 3

Okay, well, you should have enough to get started now. Just so you know, though, unless you insert lots of random letters (rather than characters like '&'), it's not really going to make it hard to read.

jverda at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 4

use this encryption code i posted once - or at least use it as a springboard

http://forum.java.sun.com/thread.jspa?threadID=5148833&tstart=315

TuringPesta at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 5

If you use a random pattern to encrypt the file how will you decrypt it? You will not be able to generate the same random pattern to decrypt the file.

You could take a look at the decimal value of all characters and add or subtract some numbers from it before writing it to a file.

chardec dec+10

A65K (75)

B66L (76)

C67M (77)

_helloWorld_a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 6

> If you use a random pattern to encrypt the file how

> will you decrypt it? You will not be able to generate

> the same random pattern to decrypt the file.

You will if you use the same seed to java.util.Random.

> You could take a look at the decimal value of all

> characters and add or subtract some numbers from it

> before writing it to a file.

That's known as a Caesar cipher. It's one of the oldest and simplest encryption schemes, and one of the easiest to detect and break.

jverda at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 7

> > If you use a random pattern to encrypt the file

> how

> > will you decrypt it? You will not be able to

> generate

> > the same random pattern to decrypt the file.

>

> You will if you use the same seed to

> java.util.Random.

>

Ok, shows how little I know about Random.

> > You could take a look at the decimal value of all

> > characters and add or subtract some numbers from

> it

> > before writing it to a file.

>

> That's known as a Caesar cipher. It's one of the

> oldest and simplest encryption schemes, and one of

> the easiest to detect and break.

Yep I know it is a pretty simple encryption but I thought this was more of a simple experiment than anything else.

OP, I have never used the javax.crypto packages but if I wanted something decent I would give them a look.

_helloWorld_a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 8

> That's known as a Caesar cipher. It's one of the

> oldest and simplest encryption schemes, and one of

> the easiest to detect and break.

As in IBM = HAL?

_helloWorld_a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 9

Yep, ROT13. It's main advantages are if you want to keep your little sister from reading your notes, but not much else. ROT47 is a little more confusing, but not by much.

But I agree that using some ROT with an element of randomness by way of a deterministic seed would be somewhat hard for most to break.

petes1234a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 10

Wow! I didn't expect so many replies, this is cool! Just for the heck of it, when I'm done writing this, could I post a sample of encrypted text and see how hard it is to break?

Jezzica85

Message was edited by:

jezzica85

jezzica85a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 11

> Wow! I didn't expect so many replies, this is cool!

> Just for the heck of it, when I'm done writing this,

> could I post a sample of encrypted text and see how

> hard it is to break?

> Jezzica85

>

> Message was edited by:

> jezzica85

you can try I guess, not sure if anyone will try and hax0r it though.

_helloWorld_a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 12

Hmm, good point.

Secrecy with ciphers is good anyway, hahaha....

Ahem. Not enough caffeine today. ;)

jezzica85a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 13

Cipher.java

import java.util.*;

public abstract class Cipher {

public String encrypt(String s) {

StringBuffer result = new StringBuffer("");

StringTokenizer words = new StringTokenizer(s);

while (words.hasMoreTokens()) {

result.append(encode(words.nextToken()) + " ");

}

return result.toString();

}

public String decrypt(String s) {

StringBuffer result = new StringBuffer("");

StringTokenizer words = new StringTokenizer(s);

while (words.hasMoreTokens()) {

result.append(decode(words.nextToken())+ " ");

}

return result.toString();

}

public abstract String encode(String word);

public abstract String decode(String word);

}

Caesar.java

public class Caesar extends Cipher {

public String encode(String word) {

StringBuffer result = new StringBuffer();

for (int k = 0; k < word.length(); k++) {

char ch = word.charAt(k);

ch = (char)('a' + (ch -'a'+ 3) % 26);

result.append(ch);

}

return result.toString();

}

public String decode(String word) {

StringBuffer result = new StringBuffer();

for (int k = 0; k < word.length(); k++) {

char ch = word.charAt(k);

ch = (char)('a' + (ch - 'a' + 23) % 26);

result.append(ch);

}

return result.toString();

}

}

TestEncrypt.java

public class TestEncrypt {

public static void main(String argv[]) {

Caesar caesar = new Caesar();

//here's the message

String plain = "this is the secret message";

//encrypt the message

String secret = caesar.encrypt(plain);

System.out.println(" ********* Caesar Cipher Encryption *********");

System.out.println("PlainText: " + plain);

System.out.println("Encrypted: " + secret);

System.out.println("Decrypted: " + caesar.decrypt(secret));

}

}

Message was edited by:

fastmike

fastmikea at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 14

Thanks everybody,

I've got my cipher working, and I used a bit of everything you all suggested.

Jezzica85

jezzica85a at 2007-7-29 14:32:02 > top of Java-index,Java Essentials,Java Programming...
# 15

delightful.

was there anything wrong with the real encryption code i posted, lol?

http://forum.java.sun.com/thread.jspa?threadID=5148833&tstart=315

TuringPesta at 2007-7-29 14:32:07 > top of Java-index,Java Essentials,Java Programming...
# 16

> Hmm, good point.

> Secrecy with ciphers is good anyway, hahaha....

Depends what you mean.

Relying on the algorithm being secret is not good, and no "real" encryption scheme does it. Good encryption allows for the algorithm to be well known, and requires the key being kept secret.

jverda at 2007-7-29 14:32:07 > top of Java-index,Java Essentials,Java Programming...
# 17

Jezzica85 I know you finished your program, but look at this way i used before it can give you alittle exp.

public class EncodingProcess {

static String encodedText;

public EncodingProcess() {

}

public char[] encodeText(String text){

char c[] = new char[text.length()];

for (int counter = 0; counter < text.length(); counter++) {

int i = text.charAt(counter);

int i2 = i + 5;

c[counter] = (char)i2;

}

return c;

}

public String decodeText(char[] c){

String text = "";

for (int counter = 0; counter < c.length; counter++) {

int i = c[counter];

int i2 = i - 5;

text += ""+(char)i2;

}

return text;

}

}

believe me who ever opened my texts can't think of reading it at all....

Q.Najjar (^_^);

QussayNajjara at 2007-7-29 14:32:07 > top of Java-index,Java Essentials,Java Programming...
# 18

> Jezzica85 I know you finished your program, but look

> at this way i used before it can give you alittle

> exp.

> .................................

> .................................

> believe me who ever opened my texts can't think of

> reading it at all....

>

> Q.Najjar (^_^);

As a kid, I had a decoder ring that I got from a cereal box that would break this simple translation cipher. Even a simple XOR cipher would work better than this, though I think other suggestions that you use Des encryption are the way to go.

petes1234a at 2007-7-29 14:32:07 > top of Java-index,Java Essentials,Java Programming...
# 19

Using random substrings could also be a good way to cipher text...

Overkilla at 2007-7-29 14:32:07 > top of Java-index,Java Essentials,Java Programming...
# 20

> Cipher.java

fastmike, why are you such an *******? Handing someone a complete answer is NOT helping them. Grow up.

jverda at 2007-7-29 14:32:07 > top of Java-index,Java Essentials,Java Programming...