凯撒密码及C语言python实现

我的总密码学Wiki:https://www.yuque.com/tidesec/grghkw
在这里插入图片描述

0x01 恺撒密码定义

凯撒密码(Caesar cipher)又被称为恺撒加密、恺撒变换、变换加密

提到凯撒密码应该没有人不知道吧,凯撒密码的明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文,简称一种替换密码

也可以认为维吉尼亚密码是一种多凯撒密码的组合
在这里插入图片描述

恺撒密码名称

偏移量为10:Avocat(A→K)
偏移量为13:ROT13
偏移量为-5:Cassis (K 6)
偏移量为-6:Cassette (K 7)

0x02 ROT13

ROT13(回转13位,rotate by 13 places,有时中间加了个连字符称作ROT-13)
是一种简易的替换式密码,它是一种在英文网络论坛用作隐藏八卦(spoiler)、妙句、谜题解答以及某些脏话的工具,目的是逃过版主或管理员的匆匆一瞥

ROT13被描述成“杂志字谜上下颠倒解答的Usenet点对点体”,也是过去在古罗马开发的凯撒加密的一种变体
例如:
明文:HELLO
密文:URYYB

0x03 凯撒密码原理

首先要确定凯撒密码是有密钥的,凯撒密码的密钥表示字符串每位的移动位数,例如密钥为 5,表示字符串中的每位都左移动五位

例如:
密钥为3
明文字母表:
在这里插入图片描述
密文字母表:
在这里插入图片描述
明文:PLAINTEXT
对应密文:SODLQWHAW

加密算法
在这里插入图片描述

解密算法
在这里插入图片描述

0x04 C语言实现凯撒密码

# -*- coding = utf-8 -*-
# @Time : 2022/2/245 16:26 下午
# @Author : lmn
# @File : Caesar.c
# @Software : CLion

#include <stdio.h>
#include <assert.h>
#define TEXT 100
int CHOOSE()
{
    printf("*******************************\n");
    printf("*** 1. 加密  2. 解密  0. 退出****\n");
    printf("*******************************\n");
    int a = 0;
    scanf("%d",&a);
    return a;
}
//初始化
void InitVirginia(char* text){
    int j = 0;
    for (j = 0; j < TEXT; j++) {
        text[j] = ' ';
    }
}
//加密
int ENCODE(char* plaintext, int key){
    assert(plaintext != NULL);

    //计算 plaintext 元素个数
    int sz = 0;
    for(sz=0;plaintext[sz]!='\0';sz++);
    int i = 0;
    for(i = 0 ; i < sz ; i++)
    {
        if(plaintext[i] >= 'A' && plaintext[i] <= 'Z')
        {
            plaintext[i] = ((plaintext[i]-'A')+key)%26+'A';
        }
        else if(plaintext[i] >= 'a' && plaintext[i] <= 'z')
        {
            plaintext[i] = ((plaintext[i]-'a')+key)%26+'a';
        }
    }
    printf("\n加密后为:%s\n\n",plaintext);
    return 0;
}
//解密
int DECODE(char* ciphertext, int key){
    assert(ciphertext != NULL);
    int sz = 0;
    for(sz=0;ciphertext[sz]!='\0';sz++);
    int i = 0;
    for(i = 0 ; i < sz ; i++)
    {
        if(ciphertext[i] >= 'A' && ciphertext[i] <= 'Z')
        {
            ciphertext[i] = ((ciphertext[i]-'A')-key)%26+'A';
        }
        else if(ciphertext[i] >= 'a' && ciphertext[i] <= 'z')
        {
            ciphertext[i] = ((ciphertext[i]-'a')-key)%26+'a';
        }
    }
    printf("\n加密后为:%s\n\n",ciphertext);

}

int main()
{
    char text[TEXT] = {0};
    char ciphertext[TEXT] = {0};
    int key = 0;
    // 1.选择进行的操作
    int a = 1;
    while(a)
    {
        a = CHOOSE();
        if (a == 0)
            break;
        switch (a) {
            case 1:
                //加密
                InitVirginia(text);
                printf("请输入明文:>");
                scanf("%s",text);
                printf("\n请输入密钥:>");
                scanf("%d",&key);
                ENCODE(text,key);
                break;
            case 2:
                //解密
                InitVirginia(text);
                printf("请输入密文:>");
                scanf("%s",text);
                printf("\n请输入密钥:>");
                scanf("%d",&key);
                DECODE(text, key);
                break;

            default:
                printf("输入有误请重新输入!\n\n");
                //CHOOSE();
        }
    }
    return 0;
}

0x05 python实现凯撒密码

# -*- coding = utf-8 -*-
# @Time : 2022/2/25 9:03 上午
# @Author : lmn
# @File : Caesar.py
# @Software : PyCharm

def encrypt():
    plaintext = input("明文:>")
    keyNumber = int(input("密钥:>"))
    str_list = list(plaintext)
    i = 0
    while len(plaintext) > i:
        if not str_list[i].isalpha():
            str_list[i] = str_list[i]
        else:
            word = "A" if str_list[i].isupper() else "a"
            str_list[i] = chr((ord(str_list[i]) - ord(word) + keyNumber) % 26 + ord(word))
        i = i + 1
    print("明文为:>", ''.join(str_list))


def decrypt():
    ciphertest = input("明文:>")
    keyNumber = int(input("密钥:>"))
    str_list = list(ciphertest)
    i = 0
    while len(ciphertest) > i:
        if not str_list[i].isalpha():
            str_list[i] = str_list[i]
        else:
            word = "A" if str_list[i].isupper() else "a"
            str_list[i] = chr((ord(str_list[i]) - ord(word) - keyNumber) % 26 + ord(word))
        i = i + 1
    print("明文为:>", ''.join(str_list))


def default():
    print("请重新输入:>")


if __name__ == '__main__':
    choise = 1
    while(choise):
        print("*******************************")
        print("*** 1. 加密  2. 解密  0. 退出****")
        print("*******************************")
        choice = input("请输入您想执行哪个操作:>")
        if choice == '0':
            break
        switch = {'1': encrypt,
                  '2': decrypt,
                  }
        switch.get(choice, default)()
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lmn_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值