Commit 615a4fa7 authored by jackt's avatar jackt

Final draft

parent cccc02bd
public class AssessmentPartThree {
// The simplest form of encryption is the rotation cipher (also known as Caeser's Cipher)
// An offset value is chosen to encrypt a message. Each letter is replaced by the
// The simplest form of encryption is the rotation cipher (also known as
// Caeser's Cipher)
// An offset value is chosen to encrypt a message. Each letter is replaced by
// the
// letter that that number of places away from it.
// So if an offset value of 1 is chosen, each letter is replaced by the one after it
// So if an offset value of 1 is chosen, each letter is replaced by the one
// after it
// - 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
// Complete this method so that it returns the encrypted character for
// theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p'
// Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged
int ascii = (int)theChar; //converts password char to ascii code to ensure it is part of the allowed character range
int offsetVal = 0;
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <=122) // deduces if the character is between a-z or A-Z
public char enryptedCharacter(char theChar, int theOffset) {
// 05 - encryptedCharacter
// Complete this method so that it returns the encrypted character for
// theChar when and offset of theOffset is used
// So if theChar='m' and theOffset is 3 the method will return 'p'
// Lower case characters remain lower case, upper case remain upper case
// Any other characters are returned unchanged
int ascii = (int) theChar; // converts password char to ascii code to ensure it is part of the allowed
// character range
int offsetVal = 0;
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <= 122) // deduces if the character is between a-z or A-Z
{
if ((theOffset < 0 && (ascii <= 65 - theOffset) && (ascii >= 65 - theOffset))
|| (theOffset < 0 && (ascii <= 97 - theOffset) && (ascii >= 97 - theOffset))) // if the offset is
// negative, this
// accounts for the
// wraparound
{
if ((theOffset < 0 && (ascii <= 65 - theOffset) && (ascii >= 65 - theOffset))
|| (theOffset < 0 && (ascii <= 97 - theOffset) && (ascii >= 97 - theOffset))) // if the offset is negative, this accounts for the wraparound
{
offsetVal = ascii - 26 + theOffset;
theChar = (char)offsetVal;
return theChar;
}
if ((theOffset < 0 && !(ascii <= 65 - theOffset && ascii >= 65 - theOffset)
||(theOffset < 0 && !(ascii <= 97 - theOffset && ascii >= 97 - theOffset))))
{
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
return theChar;
}
if ((theOffset > 0 && ascii >= 90 - theOffset && ascii <= 90 - theOffset)
||(theOffset > 0 && ascii >= 122 - theOffset && ascii <= 122 - theOffset))
{
offsetVal = ascii - 26 + theOffset;
theChar = (char)offsetVal;
return theChar;
}
if ((theOffset > 0 && !(ascii >= 90 - theOffset && ascii <= 90 - theOffset)
||(theOffset > 0 && ! (ascii >= 122 - theOffset && ascii <= 122 - theOffset))))
{
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
return theChar;
}
offsetVal = ascii - 26 + theOffset; //determines the offset value to use
theChar = (char) offsetVal;
return theChar;
}
else
{
return theChar;
}
return theChar;
}
public String encryptedString(String theMessage, int theOffset)
{
// 06 - encryptedMessage
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
String message = "";
for (int length = 0; length < theMessage.length(); length++) // for loop to count up through the word length
{
char theChar = theMessage.charAt(length); // selects the letter at the current char placement
if ((theOffset < 0 && !(ascii <= 65 - theOffset && ascii >= 65 - theOffset) //for each if statement, if the character falls between the range, then the offset value is subtracted
|| (theOffset < 0 && !(ascii <= 97 - theOffset && ascii >= 97 - theOffset)))) { // inclusive to a wrap around (this goes for the following 3 if statements)
offsetVal = ascii + theOffset;
theChar = (char) offsetVal;
return theChar;
}
if ((theOffset > 0 && ascii >= 90 - theOffset && ascii <= 90 - theOffset)
|| (theOffset > 0 && ascii >= 122 - theOffset && ascii <= 122 - theOffset)) {
offsetVal = ascii - 26 + theOffset;
theChar = (char) offsetVal;
return theChar;
}
if ((theOffset > 0 && !(ascii >= 90 - theOffset && ascii <= 90 - theOffset)
|| (theOffset > 0 && !(ascii >= 122 - theOffset && ascii <= 122 - theOffset)))) {
offsetVal = ascii + theOffset;
theChar = (char) offsetVal;
return theChar;
}
}
else {
return theChar; //the character is returned if it is not within a-z
}
return theChar; // the encrypted answer is returned
}
public String encryptedString(String theMessage, int theOffset) {
// 06 - encryptedMessage
// Complete this method so that it uses encryptedCharacter to
// return the encrypted version of theMessage using theOffset
String message = "";
int a = theMessage.length();
for (int length = 0; length < a; length++) // for loop to count up through the word length
{
char theChar = theMessage.charAt(length); // selects the letter at the current char placement
String space = Character.toString(theChar);
if (space.equals(" "))
{
message = message + theChar;
continue;
a = theMessage.length();
if (space.equals(" ")) {
message = message + theChar;
continue;
}
int ascii = (int)theChar; //converts password char to ascii code to ensure it is part of the allowed character range
int ascii = (int) theChar; // converts password char to ascii code to ensure it is part of the allowed
// character range
int offsetVal = 0;
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <=122) // deduces if the character is between a-z or A-Z
if (ascii >= 65 && ascii <= 90 || ascii >= 97 && ascii <= 122) // deduces if the character is between a-z or
// A-Z
{
if ((theOffset < 0 && (ascii < 65 - theOffset) && (ascii >= 65))
|| (theOffset < 0 && (ascii < 97 - theOffset) && (ascii >= 97))) // if the offset is negative, this accounts for the wraparound
|| (theOffset < 0 && (ascii < 97 - theOffset) && (ascii >= 97))) // if the offset is negative,
// this accounts for the
// wraparound
{
offsetVal = ascii + 26 + theOffset;
theChar = (char)offsetVal;
theChar = (char) offsetVal;
message = message + theChar;
continue;
}
if ((theOffset < 0 && !(ascii < 65 - theOffset && ascii >= 65)
||(theOffset < 0 && !(ascii < 97 - theOffset && ascii >= 97))))
{
if ((theOffset < 0 && !(ascii < 65 - theOffset && ascii >= 65)
|| (theOffset < 0 && !(ascii < 97 - theOffset && ascii >= 97)))) {
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
theChar = (char) offsetVal;
message = message + theChar;
continue;
}
if ((theOffset > 0 && ascii > 90 - theOffset && ascii <= 90)
||(theOffset > 0 && ascii > 122 - theOffset && ascii <= 122))
{
if ((theOffset > 0 && ascii > 90 - theOffset && ascii <= 90)
|| (theOffset > 0 && ascii > 122 - theOffset && ascii <= 122)) {
offsetVal = ascii - 26 + theOffset;
theChar = (char)offsetVal;
theChar = (char) offsetVal;
message = message + theChar;
continue;
}
if ((theOffset > 0 && !(ascii > 90 - theOffset && ascii <= 90)
||(theOffset > 0 && ! (ascii > 122 - theOffset && ascii <= 122))))
{
if ((theOffset > 0 && !(ascii > 90 - theOffset && ascii <= 90)
|| (theOffset > 0 && !(ascii > 122 - theOffset && ascii <= 122)))) {
offsetVal = ascii + theOffset;
theChar = (char)offsetVal;
theChar = (char) offsetVal;
message = message + theChar;
continue;
}
if (!(ascii > 64 && ascii < 91) || !(ascii > 96 && ascii < 123))
{
message = message + theChar;
continue;
if (!(ascii > 64 && ascii < 91) || !(ascii > 96 && ascii < 123)) {
message = message + theChar;
continue;
}
theMessage = message;
}
theMessage = message;
}
return message;
}
{}
}
\ No newline at end of file
}
return message;
}
{
}
}
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