-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypt.cpp
More file actions
115 lines (109 loc) · 3.55 KB
/
crypt.cpp
File metadata and controls
115 lines (109 loc) · 3.55 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
#include "crypt.h"
#include "QString"
#include "QTextCodec"
#include <QDebug>
//********************************************************************
//
// Class: Crypt
// Author: Fenja Harbke
//
// Purpose: Encrypt or Decrypt a text, using popular cryptographic algorithms
//
//********************************************************************
#define UNICODE 1
#define ASCII 0
#define ENCRYPT 1
#define DECRYPT 0
//********************************************************************
//
// Method: Constructor
// Parameter: QString* text, QString* key, int format,
// all needed information, to en- or decrypt a text
// Purpose: construction
//
//********************************************************************
Crypt::Crypt(QString* text, QString* key, int format)
:text(text),key(key),format(format)
{}
//********************************************************************
//
// Method: caesar
// Parameter: int mode,
// says, wheather a text should be en- od decrypted
// Purpose: changes text to a text, en-/decrypted with caesar-chiffre
//
//********************************************************************
void Crypt::caesar(int mode)
{
int k;
if (mode == ENCRYPT)k = key->at(0).toAscii();
else k = (key->at(0).toAscii())*-1; //DECRYPT
//qDebug()<<text->toUtf8();
if(format == UNICODE){
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
for(int i = 0; i<text->size();i++)
{
int l = text->at(i).unicode()+k;
text->replace(i,1,QChar(l).unicode());
}
}else{ //format == ASCII
for(int i = 0; i<text->size();i++)
{
int l = text->at(i).toAscii();
if (l >= 32 && l <= 126)
{
l -= ' ';
l += k;
//eliminate "strange letters" (DEL, ...)
while (l < 0) l += 94;
while (l > 94) l -= 94;
l += ' ';
}
text->replace(i,1, QChar(l).toAscii());
}
}
//qDebug()<<text->toUtf8();
}
//********************************************************************
//
// Method: vigenere
// Parameter: int mode,
// says, wheather a text should be en- od decrypted
// Purpose: changes text to a text, en-/decrypted with vigenere-chiffre
//
//********************************************************************
void Crypt::vigenere(int mode)
{
int k;
if( format == UNICODE){
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
for( int i = 0; i<text->size();i++)
{
int l = text->at(i).unicode();
if(mode == ENCRYPT){
k = key->at( i % key->size() ).toAscii();
}else{
k = (key->at( i % key->size() ).toAscii())*-1;
}
text->replace(i,1, QChar(l+k).unicode());
}
}else{ //format == ASCII
for( int i = 0; i<text->size(); i++)
{
int l = text->at(i).toAscii();
if( l >= 32 && l <= 126){
l-=' ';
if(mode == ENCRYPT){
k = key->at( i % key->size() ).toAscii();
}else{
k = (key->at( i % key->size() ).toAscii())*-1;
}
l+= k;
while (l<0) l += 94;
while(l > 94) l -= 94;
l+=' ';
}
text->replace(i,1, QChar(l).toAscii());
}
}
}