Skip to content

Instantly share code, notes, and snippets.

@jesyspa
Forked from anonymous/Glitched Rail Fence Cipher Encryption
Created June 24, 2012 12:31
Show Gist options
  • Save jesyspa/2983091 to your computer and use it in GitHub Desktop.
Save jesyspa/2983091 to your computer and use it in GitHub Desktop.
Rail Fence Cipher Encryption
#include <iostream>
#include <string>
#include <stdexcept>
std::string encrypt(std::string const& plaintext) {
if (plaintext.length() <= 4)
throw std::runtime_error("Invalid string length.");
std::string first;
std::string second;
for(std::size_t i = 0; i < plaintext.length(); ++i) {
if (i % 2 == 0)
first += plaintext[i];
else
second += plaintext[i];
}
return first + second;
}
int main() {
std::string plaintext;
std::cout << "Enter four or more chars:\n>";
std::getline(std::cin, plaintext);
std::cout << '\n';
std::cout << "Ciphertext: " << encrypt(plaintext) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment