-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShiftChiper.cpp
More file actions
76 lines (64 loc) · 1.53 KB
/
ShiftChiper.cpp
File metadata and controls
76 lines (64 loc) · 1.53 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
/*
Nama : Mohammad Faris Arie Prasetyo
NPM : 140810190056
Kelas : Kriptografi A
Deskripsi : Enkripsi Deskripsi Shift Chiper
*/
#include <iostream>
#include <limits.h>
#include <stdio.h>
#include <string.h>
using namespace std;
void Enkripsi(char text[], int key){
char plaintext;
for (int i = 0; text[i] != '\0'; ++i){
plaintext = text[i];
if (plaintext >= 'a' && plaintext <= 'z'){
plaintext = plaintext + key;
if (plaintext > 'z') {
plaintext = plaintext - 'z' + 'a' - 1;
}
text[i] = plaintext;
}
else if (plaintext >= 'A' && plaintext <= 'Z'){
plaintext = plaintext + key;
if (plaintext > 'Z') {
plaintext = plaintext - 'Z' + 'A' - 1;
}
text[i] = plaintext;
}
}
printf("\nHasil Enkripsi : %s", text);
}
void Deskripsi(char text[], int key){
char ciphertext;
for (int i = 0; text[i] != '\0'; ++i){
ciphertext = text[i];
if (ciphertext >= 'a' && ciphertext <= 'z'){
ciphertext = ciphertext - key;
if (ciphertext < 'a') {
ciphertext = ciphertext + 'z' - 'a' + 1;
}
text[i] = ciphertext;
}
else if (ciphertext >= 'A' && ciphertext <= 'Z'){
ciphertext = ciphertext - key;
if (ciphertext < 'A') {
ciphertext = ciphertext + 'Z' - 'A' + 1;
}
text[i] = ciphertext;
}
}
printf("\nHasil Deskripsi : %s", text);
}
int main() {
int key, length;
char text[100];
cout << "Masukkan Text : ";
cin.getline(text,100);
cout << "Masukkan Key : ";
cin >> key;
length = strlen(text);
Enkripsi(text, key);
Deskripsi(text, key);
}