We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Creating Encrypted Messages
Page Index Toggle Pages: 1
Creating Encrypted Messages (Read 830 times)
Creating Encrypted Messages
Mar 8th, 2008, 12:02am
 
hello,

i am attempting to create a program that will assign random letters to create an encrypted phrase. for example, i want to assign the number 3 to represent "A" and G to represent "N" so if "an" was written in the code it would be displayed in text as "3G". everytime a letter appears, the same code letter/number should be displayed so that a user could attempt to decode an entire sentence.

i have been working with arrays in trying to do this, but it has proven challenging especially to make it so that the same symbol is not assigned to numerous letters (like if 3 represented "A" and "B")

can anybody offer up some tips as to how i can create this code?

thank you!
Re: Creating Encrypted Messages
Reply #1 - Mar 8th, 2008, 9:49am
 
hi, create two arrays containing all the letters:
char[] plain = new char[] {'a','b','c',.....};
char[] encrypt = new char[] {'a','b','c',.....};

now for the encrypt-array, loop through every letter, find a second, random entry and switch these 2 positions:

for every entry in array 'encrypt'
  find a second entry-nr using random
  switch positions with the random entry

so, for the example above, this would be:
first entry-nr = 0, content = 'a'
random entry-nr = 2, content = 'c'
new entry 0 = 'c'
new entry 2 = 'a'
results in encrypt = {'c', 'b', 'a', .....}



Re: Creating Encrypted Messages
Reply #2 - Mar 8th, 2008, 11:43pm
 
thanks for responding.

i understand the logic you're explaining, but what would this look like exactly in code?

eventually i need to get to the point where i type a sentence like "the great brown fox jumped" and have it displayed in the processing canvas as "3ml atlg3 et2sj riq hzkolw" but this is really pushing my knowledge to the test in terms of generating this properly and then displaying it also.
Re: Creating Encrypted Messages
Reply #3 - Mar 9th, 2008, 10:45pm
 
can anybody offer some help with this?
Re: Creating Encrypted Messages
Reply #4 - Mar 9th, 2008, 10:59pm
 
You could do something like:
Code:
String source="abcdefghijklmnopqrstuvwxyz0123456789";
String enc="";
for(int i=0;i<26;i++)
{
int pos=(int)random(source.length());
enc+=source.charAt(pos);
source=source.substring(0,pos)+source.substring(pos+1,source.length());
println("enc:"+enc);
println("source:"+source);
}

String plaintext="the quick brown fox jumps over the lazy dog"; //lowercase
String encrypted="";
for(int i=0;i<plaintext.length();i++)
{
char c=plaintext.charAt(i);
if(c!=' ')
{
int pos=(int)c-(int)'a';
encrypted+=enc.charAt(pos);
}
else
encrypted+=" ";
}
println(plaintext);
println(encrypted);
Re: Creating Encrypted Messages
Reply #5 - Mar 9th, 2008, 11:40pm
 
you could also look at the characters as ascii values:

Quote:
PFont font;
char[] plain = new char[128];      // ascii = 0-127... unicode 0-65k, but an array with 65k entries is probably not so cool
char[] encrypted = new char[128];

// temp Strings
String secretMessage = "This is a very secret message!";
String encryptedMessage;

void setup() {
 size(1024,200);
 font = createFont("Courier", 12);
 textFont(font);
 stroke(255,100);
 for (int i=0; i<128; i++) {
   plain[i] = (char) i;    // cast the number of 'i' into a char. so 65 results in 'A'.
   encrypted[i] = (char) i;
 }

 // pre-encrypt all letters
 encrypted = shuffleArray(encrypted);
}

void draw() {
 background(0);
 text(secretMessage, 5, 20);
 encryptedMessage = encryptString(secretMessage);
 text(encryptedMessage, 5, 50);
 float spacing;
 for (int i=0; i<encrypted.length; i++) {
   spacing = 5+7.3*i;
   text(plain[i], spacing, 110);
   text(encrypted[i], spacing, 140);
   line( spacing, 90, spacing, 150);
 }
}

char encryptLetter(char letter) {
 return encrypted[(int)letter];
}

// encripts a string (calls encryptChar() for every character in the string
// and returns an encrypted string
String encryptString(String string) {
 String encryptedString = "";
 for (int i=0; i<string.length(); i++ ) {
   encryptedString+=encryptLetter(string.charAt(i));
 }
 return encryptedString;
}


// shuffles an array
char[] shuffleArray(char[] array) {
 int pos;
 char temp;
 for (int i=1; i<array.length; i++){
   pos = (int)random(i);
   temp = array[pos];
   array[pos] =array[i];
   array[i] = temp;
 }
 return array;
}
Re: Creating Encrypted Messages
Reply #6 - Mar 10th, 2008, 9:58am
 
brilliant solutions guys, thank you! i've learned a lot looking at both of your programs.

i do have one more hurdle i am attempting to overcome. i'd like to allow the user to type the correct, decoded letter in to a series of blank spaces below the encrypted message.

for example:
U66UT2
A__AC_

the user would be able to select with their keyboard or mouse the blank they wish to fill in, and anywhere that the encryption "U" appears, the letter the user enters (in this case "A" for "U", or perhaps "T" for "6") would appear everytime that the "6" appears.. this way the user can keep better track of what letters they are usin to decode the message. my problems with this are:

1) allowing the user to highlight or select a specific blank and then type a letter only in that spot
2) having the letter they enter then show up every other time its encrypted letter shows up in the message

eventually once the user completes the puzzle correctly, i need to make the program recognize this and display a screen declaring the user has solved it, so i need to verify the letters that the user enters.
Re: Creating Encrypted Messages
Reply #7 - Mar 10th, 2008, 11:56am
 
hi,

Quote:
1) allowing the user to highlight or select a specific blank and then type a letter only in that spot

have a look at the key functions (check the reference). you'll basically have a "current letter" variable which holds the number of the current letter being guessed. change that number when the key is pressed (e.g. increase when up-arrow is pressed, etc) when drawing the decoded string, just put some kind of highlight at the active letter number.
Quote:
2) having the letter they enter then show up every other time its encrypted letter shows up in the message

you could have another char[] array "decodedLetters", that you fill up according to user input. then, when drawing the decoded string, lookup the letters in this array. if the place is still empty, draw a '_', and if not, draw the content at that spot. For example, if the user is at the spot where it would be a plaintext 'A', read/write the contents at array position 65.

i suggest you just start coding and come back as soon as you have specific code questions.
Re: Creating Encrypted Messages
Reply #8 - Mar 10th, 2008, 11:53pm
 
here is my code presently. i have the position of the cursor being tracked to an extent, but i am lost as to how to modify a specific character in the text, and how to limit it so the user is editing only the blank spaces i provide for them, excluding spaces, etc. and i have had no luck using arrays to track multiple instances of the same letter appearing. am i on the right track with what is below?


PFont font;
String source="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String enc="";


void setup() {
 size(600,400);
 background(0);
 smooth();
 font = loadFont("dialoginput.bold-24.vlw");
 textFont(font);
 
 current = 0;
 
for(int i=0;i<26;i++)
{
 int pos=(int)random(source.length());
 enc+=source.charAt(pos);
 source=source.substring(0,pos)+source.substring(pos+1,source.length());
 println("enc:"+enc);
 println("source:"+source);  
}


}



color fillVal = color(126);
float x = 200;
int current;
String letters = "";

void draw() {
 background(0);
 String plaintext="on the attack decades of negative adds"; //lowercase
 String encrypted="";
 for(int i=0;i<plaintext.length();i++)
 {
   char c=plaintext.charAt(i);
   if(c!=' ')
   {
     int pos=(int)c-(int)'a';
     encrypted+=enc.charAt(pos);
     }
     else
     encrypted+=" ";
  }    
 

fill(255);
text(encrypted,10,30);
text(plaintext,10,50);
     
fill(fillVal);
rect(x,175,25,25);

text(letters,200,200);
}

void keyPressed() {
 if (key == CODED) {
   if (keyCode == LEFT) {
     fillVal = 255;
     x -= 14;
     current = current-1;
   } else if (keyCode == RIGHT) {
     fillVal = 155;
     x += 14;
     current = current+1;
   }
 } else {
   fillVal = 126;
   x += 14;
   letters = letters + key;
   current = current+1;
   
   if (key == BACKSPACE || key == DELETE) {
  fillVal = 255;
     x -= 14;
     current = current-1;
   }
   
 }
       
   println(current);
   
}
Re: Creating Encrypted Messages
Reply #9 - Mar 11th, 2008, 7:34pm
 
i'm really stuck trying to get this letter input to function right, anyone have any ideas? my deadline is tomorrow so i'm starting to feel the heat.
Page Index Toggle Pages: 1