Created
March 22, 2017 04:30
-
-
Save M-ZubairAhmed/6dd079c0ccbc44ad07577abef20203c1 to your computer and use it in GitHub Desktop.
Algorithm for improved version of Caesar encryption with variable key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This method is for encription of text. | |
* @param s The input text which is to be encrypted | |
* @param shift The initial key value to be executed at the first | |
* letter of the text. | |
* @param incremental The number by which the value of key is increased. | |
* @return The encrypted text is returned. | |
**/ | |
public static String movingShift(String s, int shift, int incremental){ | |
//Character array is created to store the input text | |
char[] inStrToCharArr = new char[s.length()]; | |
for (int p = 0 ; p < s.length() ; p++){ | |
int flushV = 0, flushVInc = 0; | |
//flushV int variable stores the ASCII values of individual text. | |
flushV = (int)s.charAt(p); | |
/* | |
Cap or Non cap letter is checked against ASCII values. | |
After identifying the type of letter, it is added to the letter. | |
If the new letter exceeds the ASCII limit of alphabets, it is | |
bought back by arithmatics. | |
*/ | |
if (flushV >= 65 && flushV <=90){ | |
flushVInc = flushV + shift; | |
if(flushVInc > 90) { | |
while (flushVInc > 90) { | |
flushVInc = 64 + (flushVInc - 90); | |
} | |
inStrToCharArr[p] = (char) flushVInc; | |
} | |
else {inStrToCharArr[p] = (char)flushVInc;}} | |
else if (flushV >= 97 && flushV <=122){ | |
flushVInc = flushV + shift; | |
if(flushVInc > 122) { | |
while (flushVInc > 122) { | |
flushVInc = 96 + (flushVInc - 122); | |
} | |
//ASCII values are converted back to characters | |
inStrToCharArr[p] = (char) flushVInc; | |
} | |
else {inStrToCharArr[p] = (char)flushVInc;}} | |
else {inStrToCharArr[p] = s.charAt(p);} | |
//The key is now incremented to new key value. | |
shift += incremental; | |
} | |
//After coming out of the loop the the new string is returned. | |
String codedString = new String(inStrToCharArr); | |
return codedString; | |
} |
Cat & Dog gives incorrect output
msg = Cat & Dog
shift = 3
inc = 2
gives output : Ffa & Sfz
correct output : Ffa & mZT
@Dhavalj24 feel free to update the program, its very old i have context of this
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Caesar Improved Cipher with Incremental key
Objective
This version of Caesar cipher, makes use of variable key, which increases by fixed amount at every.
Methodology
This algorithm takes in the intended string, initial key value and incremental key.
Encoding process is as follows