Skip to content

Instantly share code, notes, and snippets.

@maekawatoshiki
Created February 3, 2015 09:00
Show Gist options
  • Save maekawatoshiki/cbf5e371048e0a7b303a to your computer and use it in GitHub Desktop.
Save maekawatoshiki/cbf5e371048e0a7b303a to your computer and use it in GitHub Desktop.
Encrypt amd Decrypt
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
char pass[1024] = "Password";
unsigned sb[12] = {
643728351, 9846391, 4550585,
92821797, 3900958, 9274773,
624294383, 6295738, 40041196,
697441253, 5648242 , 89595141
};
unsigned rot[8] = { 1, 1, 2, 3, 5, 7, 12, 17 };
char *Encrypt(char *str, int str_size)
{
int i=0,
pass_size = strlen(pass)+1;
for(i = 0; i <= str_size; i++)
{
if(i > pass_size)
{
str[i] ^= pass[i % pass_size] ^ sb[i % 12];
str[i] += pass[i % pass_size] << rot[i % 8];
}
else
str[i] ^= pass[i % pass_size];
}
return str;
}
char *Decrypt(char *str, int str_size)
{
int i=0,
pass_size = strlen(pass)+1;
for(i = 0; i <= str_size; i++)
{
if(i > pass_size)
{
str[i] -= pass[i % pass_size] << rot[i % 8];
str[i] ^= pass[i % pass_size] ^ sb[i % 12];
}
else
str[i] ^= pass[i % pass_size];
}
return str;
}
int main(int argc, char *argv[])
{
char *data;
char filename[1024] = { 0 };
int file_size = 0, i = 0;
cout << "Encrypt/Decrypt (e/d) -> ";
if(getc(stdin) == 'e')
{
getchar();
ifstream readf(argv[1], ios::in|ios::binary);
ofstream outf;
cout << "Password =>";
cin >> pass;
getchar();
cout << "output encrypt file name =>";
cin >> filename;
getchar();
outf.open(filename, std::ios_base::out | std::ios_base::binary);
file_size = readf.seekg(0, std::ios::end).tellg();
readf.seekg(0, std::ios::beg);
data = new char[file_size + 3]();
readf.read(data, file_size);
Encrypt(data, file_size);
outf.write(data, file_size);
return 0;
}
else
{
ifstream readf(argv[1], ios::in|ios::binary);
ofstream outf;
cout << "Password =>"; cin >> pass;
cout << "output decrypt file name =>"; cin >> filename;
outf.open(filename, std::ios_base::out | std::ios_base::binary);
file_size = readf.seekg(0, std::ios::end).tellg();
readf.seekg(0, std::ios::beg);
data = new char[file_size + 3]();
readf.read(data, file_size);
Decrypt(data, file_size);
outf.write(data, file_size);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment