c語言置換密碼
❶ 用c語言或者其他語言編寫替代密碼和置換密碼
給你,自己再稍微改造一下吧:
#include "stdio.h"
#include "conio.h"
main()
{
int k,i=0;
char a[100],b[100];
printf("qing shu ru ni de mi wen \n");
gets(a);
printf("qing shu ru mi shi \n");
scanf("%d",&k);
printf("\n");
do{
b[i]=(char)(a[i]+k);
if(b[i]>122){
b[i]=(char)(b[i]-26);
}
i++;
}while(a[i]!='\0');
puts(b);
getch();
}
❷ C語言設計一個用簡單的加密程序,即用字母替換的方式加密,程序運行中發現問題,求解釋。
原因就是char是1個位元組的,你不能超過127(hi,樓上的,不是128哦,是-128~127不要誤人子弟),你到後面的vwxyz已經溢出,所以是亂碼。
我的解決方法就很簡單,就是換成unsigned char 數組,這樣取值范圍增大到(0~255)就可以了,既簡單又不破壞原有的結構
還有
else if(str[i]<'a')
{
str[i]+=26;
}
這句話是廢話,可以刪掉
我修改過的版本
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void EncodeString(unsigned char *str,int key)
{
int length,i;//length為傳入字元串長度,i用作循環計數器
length=strlen(str);
for(i=0;i<length;i++)//對字元串中的每個字元依次進行加密
{
if(isupper(str[i]))//對大寫字母加密
{
str[i]+=key%26;
if(str[i]>'Z')
{
str[i]-=26;
}
}
else if(islower(str[i]))//對小寫字母加密
{
str[i]+=key%26;
if(str[i]>'z')
{
str[i]-=26;
}
}
}
}
void main()
{
unsigned char arr[50],buffer;//arr[50]用來接收字元串信息,buffer用來接收緩沖區中的回車
int key;//key為加密秘鑰
printf("This program encodes messages using a cyclic cipher.\n");
printf("To stop, enter 0 as the key.\n");
while(1)//程序一直運行,直到輸入密鑰0為止
{
printf("Enter the key: ");
scanf("%d",&key);
scanf("%c",&buffer);
if(0==key)
{
break;//輸入密鑰為0,則退出程序
}
printf("Enter a message: ");
scanf("%s",arr);
scanf("%c",&buffer);
EncodeString(arr,key);
printf("Encoded message: %s\n",arr);
}
}
❸ C語言程序。急!
在單表置換密碼中,密鑰是由字母與空格組成的 如shana
在沒有密鑰作用前,置換表如下
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
在密鑰的作用下,置換表將發生變化,具體如下
將密鑰填入置換表,如果遇到重復的字元則忽略,接著按原表順序填充,忽略重復字元,如下表
a b c d e f g h i j k l m n o p q r s t u v w x y z
S H A N B C D E F G I J K L M O P Q R T U V W X Y Z
首先將SHAN填入表中,因為A已經在前面出現,所以忽略,接著將除去S H A N四個字元的字母表按順序填充
C語言程序:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#defineMAX101 /*明文字元串的最大長度*/
char*encrypt(char*source,char*key);
intcontain(char*source,intn,charch);
voidmain()
{
char*source; /*明文*/
char*dest; /*密文*/
char*key; /*密鑰*/
source=(char*)malloc(sizeof(char)*MAX);
dest=(char*)malloc(sizeof(char)*MAX);
key=(char*)malloc(sizeof(char)*MAX);
printf("請輸入明文字元串:");
gets(source);
printf("請輸入密鑰:");
gets(key);
dest=encrypt(source,key);
printf("密文字元串:");
puts(dest);
}
/*加密明文(單表置換加密),返回密文*/
char*encrypt(char*source,char*key)
{
char*dest;
inti,j;
intlen1=strlen(source);
intlen2=strlen(key);
charch;
dest=(char*)malloc(sizeof(char)*MAX);
source=strupr(source);
key=strupr(key);
for(i=0;i<len1;i++)
{
dest[i]=NULL;
}
dest[i]='