写一个维吉尼亚加密和解码的C语言程序,具体要求如下。不要用GOTO, 不要有MAGIC NUMBER。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写一个维吉尼亚加密和解码的C语言程序,具体要求如下。不要用GOTO, 不要有MAGIC NUMBER。相关的知识,希望对你有一定的参考价值。

Write program to encrypt and decrypt a message using Vigenère binary cipher:
ci = mi mod kj
mi = ci mod kj
where mdenotes a clear text message to encrypt (mi– a byte/character of the message),
c – encrypted message
(ci– a byte/character of the cipher),
k – the cryptographic key
(kj– a byte/character of the key), i =
1, 2, ... and j = 1, 2, ..., n (n
denotes the length of the key). In case the length of the message exceeds the
length of the key, key is used again from the beginning to encrypt remaining
part of the message.

Program shall take three parameters. The first one shall be the cryptographic key. The second parameter shall passes to the program the path to the input file
that contains either clear text message, in case of ciphering, or the encrypted
message, in case of deciphering. Third parameter shall passes to the program
path to the output file were either encrypted message or clear text message is
to be written to as the result of ciphering or deciphering respectively.

Program shall read from and write to user-defined streams. Messages shall be read and written in binary mode.
Program shall be developed in this way that in future can be possible to replace the Vigenère ciphering algorithm with some other.
The length and content of the key is up to the user; longer key is always better. Program is to be considered as correct if decrypted cipher will be same
like the text message used for encryption.

一定不要有GOTO语句,也不要MAGIC NUMBER存在。 用最简单的语言和最明显的逻辑写,带上COMMENT。发到ryanhuzhehao@hotmail.com

参考技术A #include <stdio.h>
#define MINCHAR 32
#define CHARSUM 94
int encode(char* key, char* source, char* dest);
int decode(char* key, char* source, char* dest);
char table[CHARSUM][CHARSUM];
int main()

int i, j;
char key[256];
char source[256];
char destination[256];
int operation;

FILE *fp;
/* Initial the Vigenere table */
for(i = 0; i < CHARSUM; i++)
for(j = 0; j < CHARSUM; j++)
table[i][j] = MINCHAR + (i + j) % CHARSUM;

printf("please choose one operation code:\\n");
printf("1. Encode; 2. Decode; Others. Exit.\\n");
scanf("%d", &operation);
fflush(stdin);
switch (operation)

case 1:
printf("please input the key code:\\n");
gets(key);
fflush(stdin);
printf("please input the source code you want to encode:\\n");
gets(source);
fflush(stdin);
encode(key, source, destination);
printf("after encode is: \\n");
printf("%s\\n", destination);
fp=fopen("key.txt", "w");
fprintf(fp, "%s", key);
fclose(fp);
fp=fopen("source.txt", "w");
fprintf(fp, "%s", source);
fclose(fp);
fp=fopen("destination.txt", "w");
fprintf( fp, "%s",destination);
fclose(fp);
break;
case 2:
printf("please input the key code:\\n");
gets(key);
fflush(stdin);
printf("please input the source code you want to decode:\\n");
gets(source);
fflush(stdin);
decode(key, source, destination);
printf("after decode is: \\n");
printf("%s\\n", destination);
fp=fopen("key.txt", "w");
fprintf(fp, "%s", key);
fclose(fp);
fp=fopen("source.txt", "w");
fprintf(fp, "%s", source);
fclose(fp);
fp=fopen("destination.txt", "w");
fprintf(fp, "%s", destination);
fclose(fp);
break;
default:
return 0;

return 0;


int encode(char* key, char* source, char* dest)

char* tempSource = source;
char* tempKey = key;
char* tempDest = dest;
do

*tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR];
tempDest++;
if (!(*(++tempKey)))
tempKey = key;
while(*tempSource++);
dest[strlen(source)] = \'\\0\';
return 1;

int decode(char* key, char* source, char* dest)

char* tempSource = source;
char* tempKey = key;
char* tempDest = dest;
char offset;
do

offset = (*tempSource) - (*tempKey);
offset = offset >= 0 ? offset : offset + CHARSUM;
*tempDest = MINCHAR + offset;
tempDest++;
if (!(*(++tempKey)))
tempKey = key;
while(*++tempSource);
dest[strlen(source)] = \'\\0\';
return 1;

追问

哥们,留个联系方式

追答

你可以在百度发私信, 或者百度 Hi

追问

哥们,私信你了,关注一下,我还在等你的回答

py3实现维吉尼亚加解密

1、  熟悉和掌握替代加密算法的原理及其一般过程;

 

2、掌握对称加密算法的基本方法:维吉尼亚密码

 

3、掌握使用一定的编码开发工具(对具体的开发平台和工具不作要求)。

 

Python3+pycharm

  1. 1.   维吉尼亚原理分析

Vigenenre 密码使用一个词组作为密钥,密钥中每一个字母用来确定一个代换表,

每一个密钥字母被用来加密一个明文字母,第一个密钥字母加密第一个明文字母,

第二个密钥字母加密第二个明文字母,等所有密钥字母使用完后,密钥再次循环使用,

于是加解密前需先将明密文按照密钥长度进行分组。

 

  1. 2.   算法分析

密码算法可表示如下:

设密钥 K = ( kl ; kZ kd )

明文 M = ( ml , mZ mn )

密文 C = ( cl ; cZ cn ) ;

加密变换为: ci = Ek(mi) = mi + k( mod 26 )

解密变换为: mi = Dk( ci ) = ci- ki ( mod 26 )

 

 

  1. 运行结果(程序清单见附录)

(1)    解密TSOGF MMEIS ZIDJH VVCBH ACLIE FQID

加密密钥key:COMPLETE

 技术分享图片

Recruit agents for ortopspy Secrets(招募奥拓间谍秘密的特工)

 

解密QWTBA RALXI JHKVB OR

加密密钥key:ESPIONAGE

 技术分享图片

 

 

(2)加密MEET ME AFTER SCHOOL 。单词“ESPIONAGE”做为密钥

技术分享图片 

 

 

先构思好算法和加解密原理,再进行代码的实现

附录

# -*- coding: utf-8 -*-
"""
Created on Nov 27 08:17:01 2018 at D704
@author: Kevil
"""
from string import ascii_lowercase as lowercase

# 加密
def VigenereEncrypto(p, key):
    p = get_trim_text(p)
    ptLen = len(p)
    keyLen = len(key)

    quotient = ptLen // keyLen  # 商
    remainder = ptLen % keyLen  # 余

    out = ""
    for i in range(0, quotient):
        for j in range(0, keyLen):
            c = int((ord(p[i * keyLen + j]) - ord(‘a‘) + ord(key[j]) - ord(‘a‘)) % 26 + ord(‘a‘))
            # global output
            out += chr(c)

    for i in range(0, remainder):
        c = int((ord(p[quotient * keyLen + i]) - ord(‘a‘) + ord(key[i]) - ord(‘a‘)) % 26 + ord(‘a‘))
        # global output
        out += chr(c)

    return out

# 解密
def VigenereDecrypto(output, key):
    ptLen = len(output)
    keyLen = len(key)

    quotient = ptLen // keyLen
    remainder = ptLen % keyLen

    inp = ""

    for i in range(0, quotient):
        for j in range(0, keyLen):
            c = int((ord(output[i * keyLen + j]) - ord(‘a‘) - (ord(key[j]) - ord(‘a‘))) % 26 + ord(‘a‘))
            # global input
            inp += chr(c)

    for i in range(0, remainder):
        c = int((ord(output[quotient * keyLen + i]) - ord(‘a‘) - (ord(key[i]) - ord(‘a‘))) % 26 + ord(‘a‘))
        # global input
        inp += chr(c)

    return inp

def get_trim_text(text):
    text = text.lower()
    trim_text = ‘‘
    for l in text:
        if lowercase.find(l) >= 0:
            trim_text += l
    return trim_text


if __name__ == ‘__main__‘:
    prompt = """
(1)加密
(2)解密        #解密时请勿输入空格键
(3)退出
请输入您要执行的口令: """
    while (True):
        choice = input(prompt)
        if choice == ‘1‘:
            p = input("请输入明文: ")
            k = input("请输入密钥: ")
            print("加密后的密文是: %s" % (VigenereEncrypto(p, k)))
        elif choice == ‘2‘:
            c = input("请输入密文: ")
            k = input("请输入密钥: ")
            print("解密后的明文是: %s" % (VigenereDecrypto(c, k)))
        elif choice == ‘3‘:
            break
        else:
            print("不存在该口令")











































































以上是关于写一个维吉尼亚加密和解码的C语言程序,具体要求如下。不要用GOTO, 不要有MAGIC NUMBER。的主要内容,如果未能解决你的问题,请参考以下文章

py3实现维吉尼亚加解密

简单的加密算法——维吉尼亚密码

《密码学》维吉尼亚密码。

维吉尼亚密码的Python实现

维吉尼亚密码 Python 2.0

密码那些事儿|(十)“钥匙”打开维吉尼亚的锁