简单的加密与解密的实现---仿射密码(c++使用string)

本文介绍了一个使用C++实现的简单字符串加密和解密程序。通过用户输入的明文及加密参数,程序利用特定算法进行加密,并能够逆向解密返回原始明文。该示例展示了C++ string类的便捷性和加密算法的基本原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用c++中string类,相比于使用数组,没有了数组长度的限制,而且操作跟加的方便

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
#include <iostream>
#include <string>
using  namespace  std;
string jiami(string str, int  k, int  b);
string jiemi(string pass, int  k, int  b);
int  canshu( int  k, int  b);
int  main()
{
     string str;      //明文
     string pass;     //密文
     string res;         //明文
     int  k,b;         //加密算法的参数
     cout<< "请输入明文:" ;
     cin>>str;
     cout<< "请输入加密算法的参数(k b):" ;
     cin>>k>>b;
 
     while (!canshu(k,b))
     {
         cout<< "参数不合法(参数应该与mod·26互为素数),请重新输入:" ;
         cin>>k>>b;
     }
     cout<< "加密函数为:f(x)=" <<k<< "x+" <<b<<endl;
     pass=jiami(str,k,b);
     cout<< "密文为:" <<pass<<endl;
     res=jiemi(pass,k,b);
     cout<< "解密后明文为:" <<res<<endl;
     return  0;
}
 
 
int  canshu( int  k, int  b)
{
     int  c,t,mod = 26;     //模 26
     if (k==1 || k%2==0)
         return  0;
     if (mod<k)
     {
         t=mod;
         mod=k;
         k=t;
     }
     while (c!=0)
     {
         c=mod%k;
         mod=k;
         k=c;
     }
     if (mod==1)
         return  1;
     else 
         return  0;
}
string jiami(string str, int  k, int  b)
{
     int  i;
     cout<< "\n输入的需要加密的明文为:" <<str<<endl;
     for (i=0;i<str.length();i++)
     {
         int  a;
         a=(str[i]-97)*k+b;     //加密
         if (a>25)     //对照为0-25     mod为26
         {
             do      //保证不超出域
             {
                 a=a-26;    
             } while (a>25);
             a=a+97;         //加密后得到的ASC码;
             str[i]=( char )a;
         }
         else
         {
             a=a+97;
             str[i]=( char )a;
         }
     }
     
     return  str;
}
 
 
string jiemi(string pass, int  k, int  b)
{
     int  i;
     for (i=0;i<pass.length();i++)                 //解密过程
     {
         int  d;
         d=(pass[i]-97)-b; 
         while (d%k!=0)
         {
             d=d+26;
         }
         d=d/k;
         pass[i]=( char )(d+97);
     }
 
     return  pass;
}





本文转自 nw01f 51CTO博客,原文链接:http://blog.51cto.com/dearch/1750314,如需转载请自行联系原作者
仿射密码(Affine Cipher)是一种古典密码,它是通过对明文中的每个字母进行数学变换来实现加密的。该密码加密算法是 $E(x) = (ax + b) \mod m$,其中 $a$ 和 $b$ 是密钥参数,$m$ 是字母表长度,$x$ 是明文中的字母在字母表中的位置,$E(x)$ 是密文中的字母在字母表中的位置。 解密算法是 $D(y) = a^{-1}(y - b) \mod m$,其中 $a^{-1}$ 是 $a$ 在模 $m$ 意义下的乘法逆元,$y$ 是密文中的字母在字母表中的位置,$D(y)$ 是明文中的字母在字母表中的位置。 下面是一个简单C++ 实现: ```cpp #include <iostream> #include <string> #include <cmath> using namespace std; int modInverse(int a, int m) { a = a % m; for (int x = 1; x < m; x++) { if ((a * x) % m == 1) { return x; } } return -1; } string encrypt(string plaintext, int a, int b, int m) { string ciphertext = ""; for (char c : plaintext) { if (isalpha(c)) { int x = toupper(c) - 'A'; int y = (a * x + b) % m; ciphertext += static_cast<char>(y + 'A'); } else { ciphertext += c; } } return ciphertext; } string decrypt(string ciphertext, int a, int b, int m) { string plaintext = ""; int a_inv = modInverse(a, m); for (char c : ciphertext) { if (isalpha(c)) { int y = toupper(c) - 'A'; int x = (a_inv * (y - b + m)) % m; plaintext += static_cast<char>(x + 'A'); } else { plaintext += c; } } return plaintext; } int main() { string plaintext = "HELLO WORLD"; int a = 5; int b = 8; int m = 26; string ciphertext = encrypt(plaintext, a, b, m); cout << "Ciphertext: " << ciphertext << endl; string decrypted = decrypt(ciphertext, a, b, m); cout << "Decrypted: " << decrypted << endl; return 0; } ``` 在上面的代码中,`modInverse` 函数是用来计算乘法逆元的,`encrypt` 函数用来加密明文,`decrypt` 函数用来解密密文。你可以将明文、密钥参数 $a$ 和 $b$、以及字母表长度 $m$ 作为函数的参数传入。在上面的例子中,我们将明文设置为 "HELLO WORLD",$a$ 设置为 5,$b$ 设置为 8,$m$ 设置为 26。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值