Skip to content

Instantly share code, notes, and snippets.

Created June 24, 2012 07:24
Show Gist options
  • Save anonymous/2982194 to your computer and use it in GitHub Desktop.
Save anonymous/2982194 to your computer and use it in GitHub Desktop.
Glitched Rail Fence Cipher Encryption
#include <iostream>
#include <string>
using namespace std;
string plaint;
int strleng;
int a=0;
int b=0;
void encrypt(){
//stores entered text in string.
cout << "ENTER TEXT (must be more than 4 chars):" << endl << ">";
getline(cin, plaint);
cout << endl;
strleng = plaint.length();
cout << "String Length: " << strleng << endl;
if(strleng>4){
//creates 2 char arrays that have a size of half the entered text.
char mychars[strleng/2];
char mychars2[strleng/2];
//encrypts the text. Sets text of first array to the even positioned characters and text of second to odd positioned chars.
for(int i=0;i<=strleng;i++){
if(i % 2 == 0){ //If i is even.
mychars[a] = plaint[i];
a++;
}else{
mychars2[b] = plaint[i];
b++;
}
}
cout << "CIPHERTEXT: " << mychars << mychars2 << endl; //outputs the ciphertext, combines the arrays.
}else{
cout << "Invalid Input." << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment