基于C语言的UTF-8中英文替换密码设计

本文介绍了作者在湖南大学密码学课程中完成的作业——基于C语言的UTF-8中英文替换密码设计。内容涵盖中文UTF-8的读取、加密解密程序框架设计、替换加密加强、性能测试等。通过调整加密算法,解决秘钥连续性问题,提高安全性。
摘要由CSDN通过智能技术生成

简要说明

本设计为湖南大学密码学的一次课程作业设计。非作业目的可随意引用。

由于本人初次接触密码学,本设计可能存在问题以及漏洞。若发现望指出。

GitHub : https://github.com/He11oLiu/SubstitutionCipher

中文utf-8 简单偏移替换密码

初次尝试

中文utf-8的读取

utf-8的格式

UTF-8编码规则:如果只有一个字节则其最高二进制位为0;如果是多字节,其第一个字节从最高位开始,连续的二进制位值为1的个数决定了其编码的位数,其余各字节均以10开头。

获取单个utf-8 编码的长度,注意当最高位为0情况。

int get_utf_8_len(char s){
    int i = 0x80,len = 0;
    while(s&i) {i=i>>1;len++;}
    return len==0?1:len;
}

byte数组中获取单个utf-8 字符

word_length = get_utf_8_len(word_byte[i]);
strncpy(word_utf_8,word_byte+i,word_length);

从3字节utf-8 字符中获取utf-8 编号

int get_utf_8_code(char *s){
    return (*s & 0x0F)<<12 | (*(s+1)&0x3F)<<6 |(*(s+2)&0x3F);
}

获取中文字符

常用中文显示范围

U+4e00 - U+9fa5

故利用utf_8_is_cn来判断

#define max_cn_utf_8 0x9fa5
#define min_cn_utf_8 0x4e00
#define cn_utf_8_size (max_cn_utf_8-min_cn_utf_8)
#define utf_8_is_cn(code) (code>=min_cn_utf_8 && code <max_cn_utf_8)

偏移加密测试

/**
 *  Caesar_cipher_encrpt
 *  简单凯撒加密测试,bias为偏移量
 *  常用中文大小为cn_utf_8_size
 */
void Caesar_cipher_encrpt(int_32U *plain_code,int_32U *cipher_code,int_32U bias){
    *cipher_code = ((*plain_code-min_cn_utf_8)+bias)%cn_utf_8_size + min_cn_utf_8;
}


/**
 *  Caesar_cipher_decrpt
 *  简单凯撒解密测试,bias为偏移量
 *  常用中文大小为cn_utf_8_size
 */
void Caesar_cipher_decrpt(int_32U *cipher_code,int_32U *plain_code,int_32U bias){
    *plain_code = ((*cipher_code+cn_utf_8_size-min_cn_utf_8)-bias)%cn_utf_8_size + min_cn_utf_8;
}

主函数中测试:

if(utf_8_is_cn(utf_8_code)){
    Caesar_cipher_encrpt(&utf_8_code,&cipher_code,100);
    get_utf_8_word(cipher_code, word_utf_8);
    printf("Encrpted: %s ",word_utf_8);
    Caesar_cipher_decrpt(&cipher_code,&utf_8_code,100);
    get_utf_8_word(utf_8_code, word_utf_8);
    printf("Decrpted: %s\n",word_utf_8);
}

测试结果

Encrpted: 
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值