-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyEncryption.cpp
More file actions
176 lines (154 loc) · 5.41 KB
/
Copy pathmyEncryption.cpp
File metadata and controls
176 lines (154 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*********************************************************************
* Encryption Module
*
* Zaq Nelson
*
* A simple Encryption implementation for my own library's sake
* This will encrypt and decrypt an entire file
* The algorithm is a modified Vigenere Cipher
*
* NOTE*** Please do not use this for anything actually secure
* This is simply an academic exercise to practice cryptography
*
* Any en/decryption that happens WILL overwrite the file. Take
* caution as there is no recovery built in here
*
* Original implementation for use in Password Safe
**********************************************************************/
#include "myEncryption.h" // Header File
#include <fstream> // For Files
#include <string> // For strings and getline
#include <iostream> // For Errors
/**********************************************************************
* Normalize Key
* Takes length of a string to en/decrypt and the key, repeats the
* key to match the length of the string.
* Accepts the key, and length of the encryption string
**********************************************************************/
void normalizeKey(std::string & key, const int length)
{
// A temp variable for repeating purposes
std::string keyRepeat = key;
// Add the repeating key until it is at least as big as what is
// being encrypted
while (key.length() < length)
key += keyRepeat;
// Scrape off any excess
key.erase(length);
}
/********************************************************************
* Read File
* Open a file, return the string inside
* Accepts a filename
********************************************************************/
std::string readFile(const std::string & file)
{
// Return String including the file
std::string data;
// Get Line string
std::string line;
// Wrap the file items in a try-catch block in case there
// is a problem
try
{
// Open the file and read the contents
std::ifstream myFile;
myFile.open(file);
while(!myFile.eof())
{
std::getline(myFile, line);
data += line + "\n";
}
// Take care of the extra endline
data.erase(data.length() - 1);
myFile.close();
}
catch(const std::exception& e)
{
std::cout << e.what() << '\n';
}
// Return the contents of the file
return data;
}
/********************************************************************
* Write File
* Overwrite the file with the new data
* accepts the data string, and filename
********************************************************************/
void writeFile(const std::string & data, const std::string & file)
{
// Wrap the file items in a try-catch block in case there
// is a problem
try
{
// Open the file
std::ofstream myFile;
myFile.open(file, std::ofstream::trunc);
// trunc erases the data in the file, fresh to be overwritten
// Write the data and close
for(int i = 0; i < data.length(); i++)
myFile << data[i];
myFile.close();
}
catch(const std::exception& e)
{
std::cout << e.what() << '\n';
}
}
/********************************************************************
* Encrypt
* Function to encrypt a given file, using a modded Vigenere Cipher
* Accepts a key and a filename
********************************************************************/
void encrypt(std::string key, const std::string & file)
{
// Put the file contents into a string, prepare key
std::string data;
data = readFile(file);
normalizeKey(key, data.length());
// Encrypt the data according to a modded Vigenere Cipher:
// Add the char value from a repeating part of a key
// to the original value, encrypting it.
for(int i = 0; i < data.length(); i++)
data[i] += key[i];
// Write the encrypted data back into the file
writeFile(data, file);
}
/********************************************************************
* Decrypt
* Function to Decrypt a given file, using a modded Vigenere Cipher
* Accepts a key and a filename
********************************************************************/
void decrypt(std::string key, const std::string file)
{
// Put the file contents into a string, prepare key
std::string data;
data = readFile(file);
normalizeKey(key, data.length());
// Decrypt the data according to a modded Vigenere Cipher:
// Sub the char value from a repeating part of a key
// to the original value, decrypting it.
for(int i = 0; i < data.length(); i++)
data[i] -= key[i];
// Write the Decrypted data back into the file
writeFile(data, file);
}
// Unimplemented, meant for Debugging and Testing
/*
int main()
{
// Setup the test variables, key and file
std::string key = "IamAwesome";
std::string file = "test.txt";
// Prepare the test file and print results
writeFile("This\nshould work", file);
std::cout << "Written:\n" << readFile(file) << "\n";
// Run the encryption and print results
encrypt(key, file);
std::cout << "Encrypted:\n" << readFile(file) << "\n";
// Run the decryption and print results
decrypt(key, file);
std::cout << "Decrypted:\n" << readFile(file) << "\n";
return 0;
}
*/