Commit 45382c29 authored by Thundy's avatar Thundy

It's finished

parent e56f0d16
public class AssessmentPartThree {
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher)
......@@ -8,6 +7,7 @@ public class AssessmentPartThree {
// - so a becomes b, b becomes c, etc
// If a value of -2 is chosen a becomes y, b becomes z and so on
public char enryptedCharacter(char theChar, int theOffset)
{
// 05 - encryptedCharacter
......@@ -17,7 +17,143 @@ public class AssessmentPartThree {
// Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged
return 'a';
char encryptedCharacter='a';
int charNo = 0;
//initializing the variables used
switch (theChar)
//switch tree converts the character to an integer number. 1-26 for lowercase and 27-52 for uppercase
{
case 'a':charNo=1;break; case 'A':charNo=27;break;
case 'b':charNo=2;break; case 'B':charNo=28;break;
case 'c':charNo=3;break; case 'C':charNo=29;break;
case 'd':charNo=4;break; case 'D':charNo=30;break;
case 'e':charNo=5;break; case 'E':charNo=31;break;
case 'f':charNo=6;break; case 'F':charNo=32;break;
case 'g':charNo=7;break; case 'G':charNo=33;break;
case 'h':charNo=8;break; case 'H':charNo=34;break;
case 'i':charNo=9;break; case 'I':charNo=35;break;
case 'j':charNo=10;break; case 'J':charNo=36;break;
case 'k':charNo=11;break; case 'K':charNo=37;break;
case 'l':charNo=12;break; case 'L':charNo=38;break;
case 'm':charNo=13;break; case 'M':charNo=39;break;
case 'n':charNo=14;break; case 'N':charNo=40;break;
case 'o':charNo=15;break; case 'O':charNo=41;break;
case 'p':charNo=16;break; case 'P':charNo=42;break;
case 'q':charNo=17;break; case 'Q':charNo=43;break;
case 'r':charNo=18;break; case 'R':charNo=44;break;
case 's':charNo=19;break; case 'S':charNo=45;break;
case 't':charNo=20;break; case 'T':charNo=46;break;
case 'u':charNo=21;break; case 'U':charNo=47;break;
case 'v':charNo=22;break; case 'V':charNo=48;break;
case 'w':charNo=23;break; case 'W':charNo=49;break;
case 'x':charNo=24;break; case 'X':charNo=50;break;
case 'y':charNo=25;break; case 'Y':charNo=51;break;
case 'z':charNo=26;break; case 'Z':charNo=52;break;
default:charNo=0;break;
//default is charNo=0, this goes into the next 'if' statement
}
if (charNo==0)
{
encryptedCharacter = theChar;
System.out.println("***INVALID CHARACTER*** "+encryptedCharacter);
return encryptedCharacter;
//catches if the character is not a letter, and then returns the input value.
}
else if (charNo<27)
//catches if the character is lower case
{
charNo=charNo+theOffset;
//applies the offset to the character number
if (charNo>26)
//wrapping function, catches if the character number goes "off scale" past 'z'
{
charNo=charNo-26;
//wraps the characater number back to the start
}
else if (charNo<0)
{
charNo=charNo+26;
}
}
else
{
//handles uppercase characters
charNo=charNo+theOffset;
if (charNo>52)
//wrapping function for uppercase characters, performs the same function as line 74
{
charNo=charNo-26;
}
else if (charNo<27)
{
charNo=charNo+26;
}
}
switch (charNo)
//takes the new offset character number, and translates it to its corresponding letter
{
case 1: encryptedCharacter = 'a';break;
case 2: encryptedCharacter = 'b';break;
case 3: encryptedCharacter = 'c';break;
case 4: encryptedCharacter = 'd';break;
case 5: encryptedCharacter = 'e';break;
case 6: encryptedCharacter = 'f';break;
case 7: encryptedCharacter = 'g';break;
case 8: encryptedCharacter = 'h';break;
case 9: encryptedCharacter = 'i';break;
case 10: encryptedCharacter = 'j';break;
case 11: encryptedCharacter = 'k';break;
case 12: encryptedCharacter = 'l';break;
case 13: encryptedCharacter = 'm';break;
case 14: encryptedCharacter = 'n';break;
case 15: encryptedCharacter = 'o';break;
case 16: encryptedCharacter = 'p';break;
case 17: encryptedCharacter = 'q';break;
case 18: encryptedCharacter = 'r';break;
case 19: encryptedCharacter = 's';break;
case 20: encryptedCharacter = 't';break;
case 21: encryptedCharacter = 'u';break;
case 22: encryptedCharacter = 'v';break;
case 23: encryptedCharacter = 'w';break;
case 24: encryptedCharacter = 'x';break;
case 25: encryptedCharacter = 'y';break;
case 26: encryptedCharacter = 'z';break;
case 27: encryptedCharacter = 'A';break;
case 28: encryptedCharacter = 'B';break;
case 29: encryptedCharacter = 'C';break;
case 30: encryptedCharacter = 'D';break;
case 31: encryptedCharacter = 'E';break;
case 32: encryptedCharacter = 'F';break;
case 33: encryptedCharacter = 'G';break;
case 34: encryptedCharacter = 'H';break;
case 35: encryptedCharacter = 'I';break;
case 36: encryptedCharacter = 'J';break;
case 37: encryptedCharacter = 'K';break;
case 38: encryptedCharacter = 'L';break;
case 39: encryptedCharacter = 'M';break;
case 40: encryptedCharacter = 'N';break;
case 41: encryptedCharacter = 'O';break;
case 42: encryptedCharacter = 'P';break;
case 43: encryptedCharacter = 'Q';break;
case 44: encryptedCharacter = 'R';break;
case 45: encryptedCharacter = 'S';break;
case 46: encryptedCharacter = 'T';break;
case 47: encryptedCharacter = 'U';break;
case 48: encryptedCharacter = 'V';break;
case 49: encryptedCharacter = 'W';break;
case 50: encryptedCharacter = 'X';break;
case 51: encryptedCharacter = 'Y';break;
case 52: encryptedCharacter = 'Z';break;
}
System.out.println("Plain character: "+theChar+" Cypher character: "+encryptedCharacter);
return encryptedCharacter;
//returns the final result
}
public String encryptedString(String theMessage, int theOffset)
......@@ -26,7 +162,22 @@ public class AssessmentPartThree {
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
return "Encryptred message";
char charCon='a';
char message [] = new char [theMessage.length()];
// sets up a new char array for holding the individual chars from theMessage
for (int i=0; i<theMessage.length();i++)
{
char theChar=theMessage.charAt(i);
charCon=enryptedCharacter(theChar,theOffset);
// feeds theChar and theOffset through encryptedCharacter method
message[i]=charCon;
// loop iterates through each character in the message and runs it through encryptedCharacter method
}
String encMessage = new String(message);
//converts the char array to a string
System.out.println("Plain Text: "+theMessage+" Cypher Text: "+encMessage);
return encMessage;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment