-
Notifications
You must be signed in to change notification settings - Fork 0
/
caesarafcipher.cpp
46 lines (42 loc) · 1.28 KB
/
caesarafcipher.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
#include "caesarafcipher.h"
#include "ui_caesarafcipher.h"
CaesarAfCipher::CaesarAfCipher(QWidget *parent, QString text) :
AbstractCipher(parent),
ui(new Ui::CaesarAfCipher)
{
ui->setupUi(this);
ui->textEdit->setText(text);
ui->firstKeyspinBox->setMaximum(CryptoHelper::alphabet.length());
ui->secondKeyspinBox->setMaximum(CryptoHelper::alphabet.length());
}
CaesarAfCipher::~CaesarAfCipher()
{
emit text(ui->textEdit->toPlainText());
delete ui;
}
int CaesarAfCipher::gcd(int a, int b)
{
if(a == b)
return a;
else if(a > b)
return gcd(a - b, b);
else
return gcd(b, a);
}
void CaesarAfCipher::encryptText()
{
QString text;
text = ui->textEdit->toPlainText();
if(gcd(ui->firstKeyspinBox->value(), CryptoHelper::alphabet.length()) == 1)
{
text = CryptoHelper::pre(text);
for(int i = 0; i < text.length(); i++)
text[i] = CryptoHelper::alphabet[(ui->firstKeyspinBox->value() * i +
ui->secondKeyspinBox->value()) % CryptoHelper::alphabet.length()];
emit results(CryptoHelper::pre(ui->textEdit->toPlainText()), text);
text = CryptoHelper::post(text);
emit encryptedText(text);
}
else
emit encryptedText(tr("'a' and 'm' don't coprime!"));
}