易位加密

编程入门 行业动态 更新时间:2024-10-23 19:31:42

易位加密

易位加密

易位加密

#include <stdio.h>
#include <string.h>//测试原文:pleasetransferonemilliondollarstomyswissbankaccoundsixtwotwoabcd
//测试密钥:MEGABUCK
//测试密文:afllsksoselawaiatoossctclnmomantesilyndwrnntsowdpaedobuoeriricxb //打印加密表 
void print(char table[9][8]){printf("加密表:\n");for(int i=0;i<9;i++){printf("\t\t");for(int j=0;j<8;j++)printf("%c ",table[i][j]);printf("\n");}
}//密钥插入排序算法 
void insert_sort(char sorted_key[8],int length)
{int i,j,temp;for(i=1;i<length;i++){temp = sorted_key[i];for(j=i-1;j>=0;j--){if(temp<sorted_key[j]){sorted_key[j+1] = sorted_key[j];}else{break;}}sorted_key[j+1] = temp;}sorted_key[length]='\0';
}//加密 
void encrypt(char content_array[64], char key[8], char encrypted_content_array[64])
{char sorted_key[8];strcpy(sorted_key,key);insert_sort(sorted_key,8);//加密表初始化 char table[9][8] = {{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'}};//密钥填充 for(int i=0;i<8;i++){table[0][i] = key[i];}//print(table);//加密内容填充 int line=1,column=0;while(*content_array!='\0'){if(column!=0 && column%8==0){column = 0;line++;}table[line][column] = *content_array;column++;content_array++;	}print(table);//保存加密后的内容 int index = 0;for(int i=0;i<8;i++){//忽略重复key if(i!=0 && sorted_key[i]==sorted_key[i-1]){continue;}for(int j=0;j<8;j++){if(sorted_key[i]==table[0][j]){	for(int line=1;line<9;line++){encrypted_content_array[index] = table[line][j];index++;}}	}}encrypted_content_array[index] = '\0';
}//解密 
void decrypt(char encrypted_content_array[64], char key[8], char content_array[64])
{char sorted_key[8];strcpy(sorted_key,key);insert_sort(sorted_key,8);//加密表初始化 char table[9][8] = {{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'},{'*','*','*','*','*','*','*','*'}};//密钥填充 for(int i=0;i<8;i++){table[0][i] = key[i];}//print(table);int index = 0;for(int i=0;i<8;i++){//忽略重复key if(i!=0 && sorted_key[i]==sorted_key[i-1]){continue;}for(int j=0;j<8;j++){if(table[0][j]==sorted_key[i]){for(int line=1;line<9;line++){table[line][j] = encrypted_content_array[index];index++;}}	}}print(table);index = 0;for(int line=1;line<9;line++)for(int column=0;column<8;column++){content_array[index] = table[line][column];index++;}content_array[index] = '\0';
}//加密功能 
void start_encrypt()
{char encrypted_content_array[64];char key[8];char content_array[64];printf("(加密)请输入[原文]:");scanf("%s",content_array);printf("(加密)请输入[密钥]:");scanf("%s",key);encrypt(content_array,key,encrypted_content_array);printf("#密文#\n");printf("%s\n\n",encrypted_content_array);	
}//解密功能 
void start_decrypt()
{char encrypted_content_array[64];char key[8];char content_array[64]; printf("(解密)请输入[密文]:");scanf("%s",encrypted_content_array);printf("(解密)请输入[密钥]:");scanf("%s",key);decrypt(encrypted_content_array,key,content_array);printf("#原文#\n");printf("%s\n\n",content_array);	
}//帮助功能 
void help()
{printf("使用帮助:\n");printf("	密钥:8位ASCII字符\n");printf("	密文:64位以内ASCII字符\n");printf("	保留字符:符号 * (用来填充空白字符)\n");printf("	提示:原文中若有空格,可以使用下划线代替。\n");
}int main()
{printf("********************************\n");printf("*           易位加密           *\n");printf("********************************\n");printf("1.加密\n");printf("2.解密\n");printf("3.帮助\n");int choice;while(1){printf(">>>");scanf("%d",&choice);if(choice==1){start_encrypt();}else if(choice==2){start_decrypt();}else if(choice==3){help();}else{printf("输入有误!加密请输入1,解密请输入2,帮助请输入3。\n");}}return 0;} 

C语言实现易位加密

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define CONTENT_MAX_SIZE 4096
#define DECRYPTED_CONTENT_MAX_SIZE 4096
#define KEY_MAX_SIZE 4096
char INIT_CHAR = '*';//初始化表 
char** init_table(char * key, int LINES, int COLUMNS)
{char** array = (char **)malloc(LINES * sizeof(char *));for (int i = 0; i < LINES; i++){// char ptr[LINES][COLUMNS]array[i] = (char *)malloc(COLUMNS * sizeof(char));}//密钥填充strcpy(array[0],key);//生成默认表字符for(int i=1;i<LINES;i++){for(int j=0;j<COLUMNS;j++)array[i][j] = INIT_CHAR;}return array;
}//打印加密表
void print_table(char** array, int LINES, int COLUMNS)
{printf("加密表:\n");for(int i=0;i<LINES;i++){printf("\t");for(int j=0;j<COLUMNS;j++)printf("%c ",array[i][j]);printf("\n");}
}//密钥插入排序算法 
void insert_sort(char* sorted_key,int length)
{int i,j,temp;for(i=1;i<length;i++){temp = sorted_key[i];for(j=i-1;j>=0;j--){if(temp<sorted_key[j]){sorted_key[j+1] = sorted_key[j];}else{break;}}sorted_key[j+1] = temp;}sorted_key[length]='\0';
}//加密功能 
void encrypt()
{char *content = (char *)malloc(CONTENT_MAX_SIZE*sizeof(char));char *encrypted_content;char temp_key_space[KEY_MAX_SIZE];printf("(加密)请输入[原文]:");gets(content);printf("(加密)请输入[密钥]:");gets(temp_key_space);int key_count = strlen(temp_key_space);encrypted_content = (char *)malloc(strlen(content)*sizeof(char));//数组的行列数 int COLUMNS = key_count; int LINES = ceil((float)strlen(content)/COLUMNS);//密钥处理 char *key = (char *)malloc((key_count)*sizeof(char));char *sorted_key = (char *)malloc((key_count)*sizeof(char));strcpy(key, temp_key_space);strcpy(sorted_key,key);insert_sort(sorted_key,key_count);//初始化加密表 char** table = init_table(key,LINES+1,COLUMNS);//LINES+1 添加一行保存密钥 int line=1,column=0;while(*content!='\0'){if(column!=0 && column%COLUMNS==0){column = 0;line++;}table[line][column] = *content;column++;content++;	}print_table(table,LINES+1,COLUMNS);//保存加密后的内容 int index = 0;for(int i=0;i<COLUMNS;i++){//忽略重复key if(i!=0 && sorted_key[i]==sorted_key[i-1]){continue;}for(int j=0;j<COLUMNS;j++){if(sorted_key[i]==table[0][j]){	for(int line=1;line<LINES+1;line++){encrypted_content[index] = table[line][j];index++;}}	}}encrypted_content[index] = '\0';printf("#密文#\n%s\n",encrypted_content);
}//解密功能 
void decrypt()
{char *encrypted_content = (char *)malloc(DECRYPTED_CONTENT_MAX_SIZE*sizeof(char));char *content;char temp_key_space[KEY_MAX_SIZE];int key_count;printf("(解密)请输入[密文]:");gets(encrypted_content);printf("(解密)请输入[密钥]:");gets(temp_key_space);key_count = strlen(temp_key_space);content = (char *)malloc(strlen(encrypted_content)*sizeof(char));//数组的行列数 int COLUMNS = key_count; int LINES = ceil((float)strlen(encrypted_content)/COLUMNS);//密钥处理 char *key = (char *)malloc((key_count)*sizeof(char));char *sorted_key = (char *)malloc((key_count)*sizeof(char));strcpy(key, temp_key_space);strcpy(sorted_key,key);insert_sort(sorted_key,key_count);//初始化加密表  LINES+1 添加一行保存密钥 char** table = init_table(key,LINES+1,COLUMNS);//print_table(table,LINES+1,COLUMNS);int index = 0;for(int i=0;i<COLUMNS;i++){//忽略重复key if(i!=0 && sorted_key[i]==sorted_key[i-1]){continue;}for(int j=0;j<COLUMNS;j++){if(table[0][j]==sorted_key[i]){for(int line=1;line<LINES+1;line++){table[line][j] = encrypted_content[index];index++;}}	}}print_table(table,LINES+1,COLUMNS);index = 0;for(int line=1;line<LINES+1;line++){for(int column=0;column<COLUMNS;column++){content[index] = table[line][column];index++;}}content[index] = '\0';//过滤填充字符*for(int i=index-1;i>=0;i--){if(content[i]==INIT_CHAR&&content[i-1]!=INIT_CHAR){content[i]='\0';break;}}printf("#原文#\n%s\n",content);	
}//设置 
void setup()
{printf("(设置)请输入自定义填充字符:"); scanf("%c",&INIT_CHAR);printf("设置成功!\n",INIT_CHAR);
}//帮助功能 
void help()
{printf("使用帮助:\n");printf("	密钥:任意ASCII字符\n");printf("	密文:任意ASCII字符\n");printf("	保留字符:当前使用填充符号 %c\n",INIT_CHAR);
}//主函数 
int main()
{printf("********************************\n");printf("*           易位加密           *\n");printf("********************************\n");printf("1.加密\n");printf("2.解密\n");printf("3.设置\n");printf("0.帮助\n");int choice;while(1){printf(">>>");scanf("%d",&choice);getchar();if(choice==1){encrypt();}else if(choice==2){decrypt();}else if(choice==3){setup();}else if(choice==0){help();}else{printf("输入有误!加密请输入1,解密请输入2,设置请输入3,帮助请输入0。\n");}}return 0;} 

python实现易位加密

import math
import copy# 打印加密表
def print_table(table):print("加密表:")for i in range(len(table)):print("\t\t",end="")for j in range(len(table[i])):print(table[i][j],end=" ")print()# 加密
def encrypt(content, key) -> str:COLUMN = len(key)LINE = math.ceil(len(content) / len(key))key_list = []table = []table_default_line = []for i in range(COLUMN):table_default_line.append("*")for k in key:key_list.append(k)# 密钥填充table.append(key_list)# 初始化二维数组for j in range(LINE):table.append(copy.deepcopy(table_default_line))# 密钥排序sorted_key = sorted(key_list)# 内容填充line = 1column = 0for s in content:if column != 0 and column % COLUMN == 0:column = 0line += 1table[line][column] = scolumn += 1# print(table)# 按大小排序好密钥次序 依次加密表的每列字符encrypted_content_list = []for i in range(0, len(sorted_key)):if i != 0 and sorted_key[i] == sorted_key[i-1]:continuefor j in range(0, len(sorted_key)):if sorted_key[i] == table[0][j]:for line in range(1, len(table)):encrypted_content_list.append(table[line][j])print_table(table)return "".join(encrypted_content_list).replace("*"," ")# 解密
def decrypt(encrypted_content, key) -> str:COLUMN = len(key)LINE = math.ceil(len(encrypted_content) / len(key))key_list = []table = []table_default_line = []for i in range(COLUMN):table_default_line.append("*")for k in key:key_list.append(k)# 密钥填充table.append(key_list)# 初始化二维数组for j in range(LINE):table.append(copy.deepcopy(table_default_line))# 密钥排序sorted_key = sorted(key_list)# 内容填充index = 0for i in range(0,len(sorted_key)):if i != 0 and sorted_key[i] == sorted_key[i-1]:continuefor j in range(0, len(sorted_key)):if table[0][j] == sorted_key[i]:for line in range(1, len(table)):table[line][j] = encrypted_content[index]index += 1if index == len(encrypted_content):break# print(table)content_list = []for line in range(1,LINE+1):for column in range(0, COLUMN):content_list.append(table[line][column])return "".join(content_list).replace("*","")# 开始加密
def start_encrypt():content = input("(加密)请输入[原文]:")key = input("(加密)请输入[密钥]:")encrypted_content = encrypt(content, key)print("#密文#")print(encrypted_content)# 开始解密
def start_decrypt():encrypted_content = input("(解密)请输入[密文]:")key = input("(解密)请输入[密钥]:")content = decrypt(encrypted_content, key)print("#原文#")print(content)# 帮助
def help():print("使用帮助:")print("	密钥:8位ASCII字符")print("	密文:64位以内ASCII字符")print("	保留字符:符号 * (用来填充空白字符)")def main():print("********************************")print("*           易位加密            *")print("********************************")print("1.加密")print("2.解密")print("3.帮助")while True:choice = input(">>>")if choice == "1":start_encrypt()elif choice == "2":start_decrypt()elif choice == "3":help()else:print("输入有误!加密请输入1,解密请输入2,帮助请输入3。")
main()

更多推荐

易位加密

本文发布于:2024-03-12 05:36:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1730812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!