caesar解密_C和C ++中的Caesar密码[加密和解密]

caesar解密

Get program for caesar cipher in C and C++ for encryption and decryption.

获取用于C和C ++的凯撒密码的程序以进行加密和解密。

What is Caesar Cipher?

什么是凯撒密码?

It is one of the simplest encryption technique in which each character in plain text is replaced by a character some fixed number of positions down to it.

它是最简单的加密技术之一,其中,将纯文本中的每个字符替换为某个固定位置的字符。

For example, if key is 3 then we have to replace character by another character that is 3 position down to it. Like A will be replaced by D, C will be replaced by F and so on.

例如,如果key是3,那么我们必须用另一个位于其3位置的字符替换该字符。 就像A将被D替换,C将被F替换,依此类推。

For decryption just follow the reverse of encryption process.

对于解密,只需遵循相反的加密过程即可。

Caesar Cipher in C and C++ [Encryption & Decryption]

Below I have shared program to implement caesar cipher in C and C++.

下面,我共享了一个程序来在C和C ++中实现凯撒密码。

Also Read: Vigenere Cipher in C and C++

另请阅读: C和C ++中的Vigenere Cipher

C语言中的Caesar密码程序 (Program for Caesar Cipher in C)

加密 (Encryption)

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
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s", message);
return 0;
}
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
#include<stdio.h>
int main ( )
{
char message [ 100 ] , ch ;
int i , key ;
printf ( "Enter a message to encrypt: " ) ;
gets ( message ) ;
printf ( "Enter key: " ) ;
scanf ( "%d" , & key ) ;
for ( i = 0 ; message [ i ] != '\0' ; ++ i ) {
ch = message [ i ] ;
if ( ch >= 'a' && ch <= 'z' ) {
ch = ch + key ;
if ( ch > 'z' ) {
ch = ch - 'z' + 'a' - 1 ;
}
message [ i ] = ch ;
}
else if ( ch >= 'A' && ch <= 'Z' ) {
ch = ch + key ;
if ( ch > 'Z' ) {
ch = ch - 'Z' + 'A' - 1 ;
}
message [ i ] = ch ;
}
}
printf ( "Encrypted message: %s" , message ) ;
return 0 ;
}

Output

输出量

Enter a message to encrypt: axzd Enter key: 4 Encrypted message: ebdh

输入要加密的消息:axzd 输入密钥:4 加密的消息:ebdh

解密方式 (Decryption)

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
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("Decrypted message: %s", message);
return 0;
}
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
#include<stdio.h>
int main ( )
{
char message [ 100 ] , ch ;
int i , key ;
printf ( "Enter a message to decrypt: " ) ;
gets ( message ) ;
printf ( "Enter key: " ) ;
scanf ( "%d" , & key ) ;
for ( i = 0 ; message [ i ] != '\0' ; ++ i ) {
ch = message [ i ] ;
if ( ch >= 'a' && ch <= 'z' ) {
ch = ch - key ;
if ( ch < 'a' ) {
ch = ch + 'z' - 'a' + 1 ;
}
message [ i ] = ch ;
}
else if ( ch >= 'A' && ch <= 'Z' ) {
ch = ch - key ;
if ( ch < 'A' ) {
ch = ch + 'Z' - 'A' + 1 ;
}
message [ i ] = ch ;
}
}
printf ( "Decrypted message: %s" , message ) ;
return 0 ;
}

Output

输出量

Enter a message to decrypt: ebdh Enter key: 4 Decrypted message: axzd

输入要解密的消息:ebdh 输入密钥:4 解密后的消息:axzd

C ++中的Caesar Cipher程序 (Program for Caesar Cipher in C++)

加密 (Encryption)

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
#include<iostream>
using namespace std;
int main()
{
char message[100], ch;
int i, key;
cout << "Enter a message to encrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
cout << "Encrypted message: " << message;
return 0;
}
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
#include<iostream>
using namespace std ;
int main ( )
{
char message [ 100 ] , ch ;
int i , key ;
cout << "Enter a message to encrypt: " ;
cin . getline ( message , 100 ) ;
cout << "Enter key: " ;
cin >> key ;
for ( i = 0 ; message [ i ] != '\0' ; ++ i ) {
ch = message [ i ] ;
if ( ch >= 'a' && ch <= 'z' ) {
ch = ch + key ;
if ( ch > 'z' ) {
ch = ch - 'z' + 'a' - 1 ;
}
message [ i ] = ch ;
}
else if ( ch >= 'A' && ch <= 'Z' ) {
ch = ch + key ;
if ( ch > 'Z' ) {
ch = ch - 'Z' + 'A' - 1 ;
}
message [ i ] = ch ;
}
}
cout << "Encrypted message: " << message ;
return 0 ;
}

Output

输出量

Enter a message to encrypt: asd zf Enter key: 3 Encrypted message: dvg ci

输入要加密的消息:asd zf 输入密钥:3 加密的消息:dvg ci

解密方式 (Decryption)

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
#include<iostream>
using namespace std;
int main()
{
char message[100], ch;
int i, key;
cout << "Enter a message to decrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'a'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
cout << "Decrypted message: " << message;
return 0;
}
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
#include<iostream>
using namespace std ;
int main ( )
{
char message [ 100 ] , ch ;
int i , key ;
cout << "Enter a message to decrypt: " ;
cin . getline ( message , 100 ) ;
cout << "Enter key: " ;
cin >> key ;
for ( i = 0 ; message [ i ] != '\0' ; ++ i ) {
ch = message [ i ] ;
if ( ch >= 'a' && ch <= 'z' ) {
ch = ch - key ;
if ( ch < 'a' ) {
ch = ch + 'z' - 'a' + 1 ;
}
message [ i ] = ch ;
}
else if ( ch >= 'A' && ch <= 'Z' ) {
ch = ch - key ;
if ( ch > 'a' ) {
ch = ch + 'Z' - 'A' + 1 ;
}
message [ i ] = ch ;
}
}
cout << "Decrypted message: " << message ;
return 0 ;
}

Output

输出量

Enter a message to decrypt: az GjK Enter key: 2 Decrypted message: yx EhI

输入要解密的消息:az GjK 输入密钥:2 解密后的消息:yx EhI

Comment below if you have doubts or found anything incorrect in above program for caesar cipher in C and C++.

如果您对上面的C和C ++凯撒密码程序存有疑问或发现任何不正确之处,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/11/caesar-cipher-c-c-encryption-decryption.html

caesar解密

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值