-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAffineEncrypter.cpp
54 lines (39 loc) · 957 Bytes
/
AffineEncrypter.cpp
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
#include <iostream>
#include <time.h>
#include <string>
#include <math.h>
#include "AffineEncrypter.h"
#include <cstdlib>
using namespace std;
int AffineEncrypter :: gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
void AffineEncrypter :: initKey() {
key.second = rand() % KEY_SIZE;
key.first = 1 + rand() % 128;
while(gcd(KEY_SIZE, key.first) != 1)
key.first = 1 + rand() % 128;
}
AffineEncrypter :: AffineEncrypter() : KEY_SIZE(128) {
initKey();
}
string AffineEncrypter :: encrypt(const string &msg) {
string encrypted = "";
int a = key.first,
b = key.second;
for (char ch : msg)
encrypted += cInt(((a * iChar(ch)) + b) % KEY_SIZE);
return encrypted;
}
AffineEncrypter :: affineKeyType AffineEncrypter :: getKey() {
return key;
}