信息安全——移位密码算法(C++实现)

信息安全导论课程学习的实验一,移位密码算法C++的实现。 

        移位密码算法是较为简单的算法,只是简单的对明文进行指定位数的移位操作,C++语言实现也较为简单,不需要过多赘述。

        以下简单介绍了以下移位密码算法的原理:

【原理】

1) 算法原理

        a) 移位密码就是对26个字母进行移位操作,可以移动任意位数,这样就实现了对明文的加密,移位操作简单易行,因此,加密解密比较简单。

        b) 移位密码的基本思想:移位密码算法 c=m+k(mod 26),k可以使0<k<26的任意整数。加密算法:x=x+k(mod26),解密算法x=x-k(mod 26)。当K=3,时,为凯撒密码。

2) 算法参数

        移位密码算法主要有c、m、k 三个参数。c 为密文,m 是明文,k 为密钥。

3) 算法流程

        算法流程如下。如图所示

输入

        第一行输入表明是加密还是解密,0是加密,1是解密;

        第二行是加密或解密密钥,是0<k<26之间的一个整数;

        第三行是明文或密文。

输出

        输出是明文或密文。

以下是代码实现:

#include <iostream>
#include <string.h>
using namespace std;

//【实验目的】

//1) 学习移位密码的原理
//2) 学习移密码的实现
//【实验原理】
//1) 算法原理
//a) 移位密码就是对26个字母进行移位操作,可以移动任意位数,这样就实现了对明文的加密,移位操作简单易行,因此,加密解密比较简单。
//b) 移位密码的基本思想:移位密码算法 c = m + k(mod 26), k可以使0 < k < 26的任意整数。加密算法:x = x + k(mod26),解密算法x = x - k(mod 26)。当K = 3, 时,为凯撒密码。
//    2) 算法参数
//    移位密码算法主要有c、m、k 三个参数。c 为密文,m 是明文,k 为密钥。

// A:65 a:97
void Encrypt(int k, char* m) // 加密
{
    int len = strlen(m);
    char* a = new char[len];
    for (int i = 0; i < strlen(m); i++)
    {
        int l = (m[i] - 'a' + k);
        if (l < 0) // 移位 
        {
            a[i] = l + 26 + 'a' - 32;
        }
        else
        {
            a[i] = l % 26 + 'a' - 32;
        }
    }
    for (int i = 0; i < strlen(m); i++)
    {
        cout << a[i];
    }
    cout << '\n';
}

void Decrypt(int k, char* c) // 解密
{
    int len = strlen(c);
    char* b = new char[len];
    for (int i = 0; i < strlen(c); i++)
    {
        int l = (c[i] - 'A' - k);
        if (l < 0) // 移位 
        {
            b[i] = l + 26 + 'A' + 32;
        }
        else
        {
            b[i] = l % 26 + 'A' + 32;
        }
    }
    for (int i = 0; i < strlen(c); i++)
    {
        cout << b[i];
    }
    cout << '\n';
}



int main()
{
    int op, k;
    char str[1000];
    for (int i = 2; i > 0; i--)
    {
        cin >> op;
        switch (op)
        {
        case(0): // 加密
        {
            cin >> k;
            cin >> str;
            Encrypt(k, str);
            break;
        }
        case(1): // 解密
        {
            cin >> k;
            cin >> str;
            Decrypt(k, str);
            break;
        }
        }
    }

    return 0;
}

 

  • 5
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言编写,欢迎扔板砖 //移位算法 #include #include #define SIZE 50 int main() { //i 用于计数输入个数,j 为临时变量, plain 存放明文, cipher 存放密文,decryption存放解密后文本,fpp 为明文文件指针,fpc 为密文文件指针 int i,j; char plain[SIZE],cipher[SIZE],decryption[SIZE],ciphertext[SIZE]; FILE * fpp,* fpc,* fpd; //加密 //建立新的明文TXT文件 printf("Caesar algorithm\n"); if((fpp=fopen("plain.txt","w+"))==NULL) { printf("creat new plain file error!\n"); exit(0); } //输入明文 printf("input plain alphabet:\n"); i=0; scanf("%c",&plain[i]); while(plain[i]!='\n'&&i<SIZE) { i++; scanf("%c",&plain[i]); } printf("success input %d characters\n",i); //将明文转存到文件中 for(j=0;j<i;j++) { if(fwrite(&plain[j],sizeof(char),1,fpp)!=1) { printf("saving plain file error!\n"); exit(0); } } printf("success saving plain text!\n"); //加密 for(j=0;j<i;j++) { cipher[j]=plain[j]+3; if(cipher[j]99) { printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>122) { cipher[j]=cipher[j]%122+96; printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>90) { cipher[j]=cipher[j]%90+64; printf("cipher %d = %c\n",j,cipher[j]); } else { printf("cipher %d = %c\n",j,cipher[j]); } } //建立密文文件 if((fpc=fopen("cipher.txt","w+"))==NULL) { printf("create new cipher file error!"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&cipher[j],sizeof(char),1,fpc)!=1) { printf("saving cipher file error!"); exit(0); } } printf("success saving cipher file!"); printf("\n"); //解密 printf("input ciphertext alphabet:\n"); i=0; scanf("%c",&ciphertext[i]); while(ciphertext[i]!='\n'&&i<SIZE) { i++; scanf("%c",&ciphertext[i]); } for(j=0;j<i;j++) { decryption[j]=ciphertext[j]-3; if(decryption[j]90&&decryption[j]<97) { decryption[j]=123-(97-decryption[j]); printf("character %d = %c\n",j,decryption[j]); } else {

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值