凯撒密码用C 编写(编程实现凯撒密码的加密和解密)

2023-03-12 6:34:47 听风 思思

#include stdio.h

#include stdlib.h

#include string.h

const int MAX_N=200;

int main(int argc, char *argv[])

{

int i,j,p;

char text[MAX_N];

char alphabet[30];

char op[10];

while(1)

{

printf("1---输入密码表 2---退出\n");

gets(op);

if(strcmp(op,"1")==0)

{

printf("密码表:");

gets(alphabet);

while(1)

{

printf("1---加密 2---解密 3---返回\n");

gets(op);

if(strcmp(op,"1")==0 ||strcmp(op,"2")==0 )

{

printf("输入文本:");

gets(text);

for(i=0;text[i]!='\0';i++)

{

if((text[i]='a'text[i]='z') || (text[i]='A'text[i]='Z') )

{

if(strcmp(op,"1")==0)

{

p=text[i]='a'? (text[i]-'a'):(text[i]-'A');

text[i]=text[i]+ alphabet[p]-(p+'A');

}

else

{

for(j=0;;j++)

if(alphabet[j]==text[i]||alphabet[j]==(text[i]-('a'-'A')))

break;

text[i]= text[i]='a' ? (j+'a') :(j+'A');

}

}

}//for(i)

if(strcmp(op,"1")==0)

printf("加密后的文本为:" );

else

printf("解密后的文本为:");

printf("%s\n\n",text);

}

else if(strcmp(op,"3")==0)

{

printf("\n");

break;

}

else

{

printf("选择有误!请重新选择!\n");

}

}//while(1)

}

else if(strcmp(op,"2")==0)

{

exit(1);

}

else

{

printf("选择有误!请重新选择!\n");

}

}

return 0;

}

/*

输入样例

QWERTYUIOPASDFGHJKLZXCVBNM

Welcome to ZZSY2009!

输出样例

Vtsegdt zg MMLN2009!

*/

凯撒加密算法(最简单的对称加密)

凯撒密码是罗马扩张时期朱利斯• 凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令。它将字母表中的字母移动一定位置而实现加密。例如如果向右移动 2 位,则 字母 A 将变为 C,字母 B 将变为 D,…,字母 X 变成 Z,字母 Y 则变为 A,字母 Z 变为 B。

因此,假如有个明文字符串“Hello”用这种方法加密的话,将变为密文: “Jgnnq” 。而如果要解密,则只要将字母向相反方向移动同样位数即可。如密文“Jgnnq”每个字母左移两位 变为“Hello” 。这里,移动的位数“2”是加密和解密所用的密钥。

该程序既可用于加密又可用于解密。只要传入明文和偏移量即可加密,解密需要传入密文和负的偏移量就可以解密。

输出的结果:

凯撒密码由于加解密比较简单,密钥总共只有 26 个,攻击者得到密文后即使不知道密钥,也可一个一个地试过去,最多试 26 次就可以得到明文。

这里不光根据 offset 偏移进行加密,还加上了字符所在的下标进行混合加密。

输出的结果:

用java 编写一个凯撒加密和解密

import java.util.Scanner;

public class Caeser {

private String table; // 定义密钥字母表

private int key; // 定义密钥key

public Caeser(String table, int key) {

// 根据不同的字母表和不同的密钥生成一个新的凯撒算法,达到通用的目的

super();

this.table = table;

this.key = key;

}

public String encrypt(String from) {

//凯撒加密算法,传入明文字符串,返回一个密文字符串

String to = "";

for (int i = 0; i from.length(); i++) {

to += table.charAt((table.indexOf(from.charAt(i))+key)%table.length());

}

return to;

}

public static void main(String[] args) {

Caeser caeser = new Caeser("abcdefghijklmnopqrstuvwxyz", 3);

Scanner scanner = new Scanner(System.in);

System.out.println("请输入要加密的字符串");

String str =scanner.nextLine(); //输入字符串 security

String result = caeser.encrypt(str); //调用加密方法进行加密

System.out.print(result); // 可得结果 vhfxulwb

}

}

用C语言实现凯撒密码加密解密,急!

凯撒密码就是简单的加上一个数,'a'+3='d';'z'+3='c' 假设原文全是小写字母,那么 char plain[N]={...}; //明文 char cipher[N]={};//密文 int key=3; int i=0,temp; for(i=0;iN;i++) {if(plain[i]!=' ') {temp=plain[i]+key-'a'; temp=temp%26; cipher[i]=temp+'a'; } else cipher[i]=plain[i]; } 这样就完成了加密,密文数组里面就是对原文加密后的密文,key是密钥。

用C语言编程恺撒密码加密解密程序

#include stdio.h

#define isletter( c )    ( ((c)='a'(c)='z') || ((c)='A'(c)='Z') )

void Enc( const char *str, char *out, int key )

{

    int i = 0; 

    while( str[i] )

    {

        if ( isletter( str[i] ) )

        {

            out[i] = str[i] + key;

            if ( ! isletter( out[i])  )

                out[i] -= 26;

        }

        else

            out[i] = str[i];

        i++;

    }

    out[i] = 0;

}

void Denc( const char *str, char *out, int key )

{

    int i=0;

    while( str[i] )

    {

        if ( isletter( str[i] ) )

        {

            out[i] = str[i] - key;

            if ( ! isletter( out[i] ) )

                out[i] += 26;

        }

        else

            out[i] = str[i];

        i++;

    }

    out[i] = 0;

}

int main()

{

    char  out[100], out2[100];

    Enc( "THE QUICK BROWn fox jumps over THE LAZY DOG", out, 3 );

    printf( "%s\n", out );

    Denc( out, out2, 3 );

    printf( "%s\n", out2 );

}