c语言 修改密码源码,Playfair密码C语言源代码.doc

Playfair密码C语言源代码

//PLAYFAIR.H头文件

#ifndef _PLAY_FAIR_H_

#define _PLAY_FAIR_H_

void cst_playfair_table (char *in_key);

void prnt_playfair_table ();

int playfair_encrypt (char *plain_txt, char *cipher_txt);

int playfair_decrypt (char *cipher_txt, char *plain_txt);

#endif

//PLAYFAIR.C源文件

#include "playfair.h"

#include

#include

#include

char playfair_table[5][5];

void get_x_y (char c, int *x, int *y)

{

int i, j;

for (i = 0; i < 5; ++i)

{

for (j = 0; j < 5; ++j)

{

if (playfair_table[i][j] == c)

{

*x = i; *y = j;

return;

}

}

}

}

char get_char_x_y (int x, int y)

{

if (x < 0)

x += 5;

if (y < 0)

y += 5;

return playfair_table[x % 5][y % 5];

}

void cst_playfair_table (char *in_key)

{

int i = 0, j, k;

int char_count = 0, left_ch_index = 0;

char ch, left_chs[26] = {0}, key[26] = {0};

int key_len;

strcpy (key, in_key);

key_len = strlen (key);

while (1)

{

if (key[i] == ' ')

{

for (j = i; j < key_len - 1; ++j)

key[j] = key[j + 1];

key_len--;

}

if (i == key_len)

break;

i++;

}

for (i = 0; i < 26; ++i)

{

ch = 'a' + i;

j = 0;

char_count = 0;

while (1)

{

if (key[j] == ch)

{

char_count++;

if (char_count > 1)

{

for (k = j; k < key_len -1; ++k)

key[k] = key[k + 1];

key_len--;

}

else

j++;

}

else

j++;

if (j == key_len)

break;

}

if (char_count == 0)

left_chs[left_ch_index++] = ch;

}

for (i = 0; i < left_ch_index; ++i)

key[key_len++] = left_chs[i];

for (i = 0; i < 26; ++i)

{

if (key[i] == 'z')

{

for (j = i; j < 25; ++j)

key[j] = key[j + 1];

}

}

for (i = 0; i < 5; ++i)

{

for (j = 0; j < 5; ++j)

playfair_table[i][j] = key[5 * i + j];

}

}

void prnt_playfair_table ()

{

int i, j;

printf ("Playfair Table:\n");

for (i = 0; i < 5; ++i)

{

printf ("\t");

for (j = 0; j < 5; ++j)

{

printf ("%c ", playfair_table[i][j]);

}

puts("\n");

}

}

int playfair_encrypt (char *plain_txt, char *cipher_txt)

{

in

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值