-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplecrypto.cpp
More file actions
74 lines (72 loc) · 2.18 KB
/
simplecrypto.cpp
File metadata and controls
74 lines (72 loc) · 2.18 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
#include "simplecrypto.hpp"
std::string secureString(bool encode, char* input, char* key) {
std::string tempString="";
unsigned int keyLength=strlen(key);
unsigned int subtract=0;
char temp=0;
bool switchChar=false;
if((input==NULL) || (key==NULL)) {
return "";
}
if((strlen(input)==0) || (strlen(key)==0)) {
return "";
}
if(encode) {
for(unsigned int x=0; x<strlen(input); ++x) {
temp=input[x] ^ key[x % keyLength];
if(temp==0) {
tempString=tempString+(char)255;
tempString=tempString+(char)1;
}
else if(temp==255) {
tempString=tempString+(char)255;
tempString=tempString+(char)2;
}
else {
tempString=tempString+temp;
}
}
}
else {
for(unsigned int x=0; x<strlen(input); ++x) {
if(input[x]==255) {
switchChar=true;
++subtract;
}
else {
if(switchChar) {
if(input[x]==1) {
temp=(char)0 ^ key[(x-subtract) % keyLength];
}
else if(input[x]==2) {
temp=(char)255 ^ key[(x-subtract) % keyLength];
}
else {
temp='?';
}
tempString=tempString+temp;
}
else {
temp=input[x] ^ key[(x-subtract) % keyLength];
tempString=tempString+temp;
}
switchChar=false;
}
}
}
return tempString;
}
std::string makeSecureString_B64(bool encode, char* input, char* key) {
if((input==NULL) || (key==NULL)) {
return "";
}
if((strlen(input)==0) || (strlen(key)==0)) {
return "";
}
if(encode) {
return base64_encode((char*)secureString(encode,input,key).c_str());
}
else {
return secureString(encode,(char*)base64_decode(input).c_str(),key);
}
}