-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassic_caesar.cpp
65 lines (58 loc) · 1.34 KB
/
classic_caesar.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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "classic_caesar.h"
Classic_Caesar::Classic_Caesar(){
}
void Classic_Caesar::setKey(QString Key) {
this->key = Key.toInt();
}
QString Classic_Caesar::CaesarChar_Encryption(QString s, int key)
{
QString newS;
for(QChar qch : s)
{
char ch = qch.toLatin1();
if(ch>=65 && ch<=90)
{
ch = 'A' + (ch - 'A' + key)%26;
}
else if(ch>=97 && ch<=122)
{
ch = 'a' + (ch - 'a' + key)%26;
}
else if(ch>=48 && ch<=57)
{
ch = 48 + (ch - 48 + key)%10;
}
newS += QChar(ch);
}
return newS;
}
QString Classic_Caesar::CaesarChar_Decryption(QString s, int key)
{
QString newS;
for(QChar qch : s)
{
char ch = qch.toLatin1();
if(ch>=65 && ch<=90)
{
ch = 'A'+(ch-'A'-key+26)%26;
}
else if(ch>=97 && ch<=122)
{
ch = 'a' + (ch - 'a' - key + 26)%26;
}
else if(ch>=48 && ch<=57)
{
ch = 48 + (ch - 48 - key + 10)%10;
}
newS += QChar(ch);
}
return newS;
}
bool Classic_Caesar::EncryptionText() {
this->CipherText = CaesarChar_Encryption(PlainText, this->key);
return true;
}
bool Classic_Caesar::DecryptionText(){
this->PlainText = CaesarChar_Decryption(CipherText, key);
return true;
}