Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
Created March 22, 2017 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M-ZubairAhmed/6dd079c0ccbc44ad07577abef20203c1 to your computer and use it in GitHub Desktop.
Save M-ZubairAhmed/6dd079c0ccbc44ad07577abef20203c1 to your computer and use it in GitHub Desktop.
Algorithm for improved version of Caesar encryption with variable key
/**
* 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;
}
@M-ZubairAhmed
Copy link
Author

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

  1. String is converted to char array
  2. Each character is increased by the initial key value though ASCII code
  3. key for next character is increased by incremental key
  4. Next character is increased by new key which is sum of incremental key and initial key
  5. Encoded string is returned to main

@Dhavalj24
Copy link

Cat & Dog gives incorrect output

msg = Cat & Dog
shift = 3
inc = 2

gives output : Ffa & Sfz
correct output : Ffa & mZT

@M-ZubairAhmed
Copy link
Author

@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