现代密码学第一次上机作业

#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cout<<“Please input plaintext:”;
getline(cin,str);
int m = 0,n = str.length();
for(int i = 0; i < n; i ++)
{
if(int(str[i]) >= ‘A’ && int(str[i]) <= ‘Z’)
str[m++] = char(int(str[i]) - ‘A’ + ‘a’);
else if(int(str[i]) >= ‘a’ && int(str[i]) <= ‘z’)
str[m++] = str[i];
else
continue;
}
n = m;
str = str.substr(0,n);
cout<<str<<endl;
for(int i = 0; i < n; i ++)
{
m = int (str[i]);
m = (m - ‘a’ + 3)%26;
m += ‘A’;
str[i] = char(m);
}
cout<<“The cinphertext are:”<<str<<endl;
return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
int k,b;
cout<<“放射密码的参数k,b:”;
cin>>k>>b;
char s[2];
gets(s);
cout<<"Please input the plaintext: ";
getline(cin,str);
int n,j = 0,len = str.length();
for(int i = 0; i < len; i++)
{
n = int(str[i]);
if(n >= ‘A’ && n <= ‘Z’)
{
n -= ‘A’;
n = (k * n + b) % 26;
n += ‘A’;
str[j++] = char(n);
}

    else if(n >= 'a' && n <= 'z')
    {
        n -= 'a';
        n = (k * n + b) % 26;
        n += 'A';
        str[j++] = char(n);
    }

    else
        continue;
}
str = str.substr(0,j);
cout<<"The cinphertext are:"<<str<<endl;
return 0;

}

#include<bits/stdc++.h>
using namespace std;
int main()
{
string key,plaintext;
cout<<"Please input the key: ";
cin>>key;
char s[2];
gets(s); ///Eat the “Enter”.
cout<<"Please input the plaintext: ";
getline(cin,plaintext); ///Function getline() allows input with spaces.
int Klen,Plen; ///Klen and Plen denote the length of key and plaintext, respectively.
Klen = key.length();
Plen = plaintext.length();
for(int i = 0; i < Klen; i ++)
key[i] = tolower(key[i]);

///使明文仅保留26个字母并使之成为小写字母
for(int i = 0, j = 0; i < Plen; i ++)
{
    if(int(plaintext[i]) >= 'A' && int(plaintext[i]) <= 'Z')
        plaintext[j++] = tolower(plaintext[i]);
    else if(int(plaintext[i]) >= 'a' && int(plaintext[i]) <= 'z')
        plaintext[j++] = plaintext[i];
    else
        continue;
    if(i == Plen - 1)
        Plen = j;
}
plaintext = plaintext.substr(0,Plen);


for(int i = 0; i < Plen; i ++) ///Encryption!
{
    plaintext[i] = char( ( ( int(plaintext[i]) + int(key[(i % Klen)]) - 2 * 'a') % 26 ) + 'A');
}
cout<<"The cinphertext are: "<<plaintext<<endl;
return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值