单表置换密码

在单表置换密码中,密钥是由字母与空格组成的 如shana

在没有密钥作用前,置换表如下

abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ

在密钥的作用下,置换表将发生变化,具体如下

将密钥填入置换表,如果遇到重复的字符则忽略,接着按原表顺序填充,忽略重复字符,如下表

abcdefghijklmnopqrstuvwxyz
SHANBCDEFGIJKLMOPQRTUVWXYZ

首先将SHAN填入表中,因为A已经在前面出现,所以忽略,接着将除去S H A N四个字符的字母表按顺序填充


具体C语言代码实现如下,这个代码个人感觉写的不够简洁,以后有机会再修改

思路就是读取密钥后填充进空的置换表,然后 再把原字符表填充到置换表中构成密码表


#include <stdio.h>
#include <stdlib.h>

int main()
{
	//置换表
	char List[27] = { 0 };
	//字母表
	char Alpha[26] = { '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' };
	//密钥
	char Key[101] = { 0 };
	//明文
	char Message[101] = { 0 };
	//密文
	char CipherText[101] = { 0 };
	int Num = 0;

	//获取密钥
	printf("请输入密钥(100字符以内): ");
	gets(Key);

	//构造置换表
	for (int i = 0; Key[i]; ++i)
	{
		if (Key[i] == ' ')
			continue;
		for (int j = 0; j < 26; ++j)
		{
			if (Alpha[j] == Key[i])
			{
				Alpha[j] = 0;
				List[Num++] = Key[i];
			}
		}
	}
	for (int i = 0; i < 26; ++i)
	{
		if (!Alpha[i])
			continue;
		List[Num++] = Alpha[i];
	}

	//获取明文
	printf("请输入明文(100字符以内): ");
	gets(Message);

	//加密
	for (int i = 0; Message[i]; ++i)
	{
		if (Message[i] == ' ')
			CipherText[i] = ' ';
		else
			CipherText[i] = List[Message[i] - 'a'];
	}

	//打印密文
	printf("加密后密文: %s\n", CipherText);

	system("PAUSE");
	return 0;
}


  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的单表置换密码的Java代码实现: ```java public class MonoalphabeticCipher { private String key; public MonoalphabeticCipher(String key) { this.key = key; } public String encrypt(String plaintext) { StringBuilder ciphertext = new StringBuilder(); for (char c : plaintext.toCharArray()) { if (Character.isLetter(c)) { int position = Character.toUpperCase(c) - 'A'; ciphertext.append(Character.isUpperCase(c) ? Character.toUpperCase(key.charAt(position)) : Character.toLowerCase(key.charAt(position))); } else { ciphertext.append(c); } } return ciphertext.toString(); } public String decrypt(String ciphertext) { StringBuilder plaintext = new StringBuilder(); for (char c : ciphertext.toCharArray()) { if (Character.isLetter(c)) { int position = key.toUpperCase().indexOf(Character.toUpperCase(c)); plaintext.append(Character.isUpperCase(c) ? Character.toUpperCase((char) ('A' + position)) : Character.toLowerCase((char) ('a' + position))); } else { plaintext.append(c); } } return plaintext.toString(); } } ``` 使用方法: ```java MonoalphabeticCipher cipher = new MonoalphabeticCipher("qwertyuiopasdfghjklzxcvbnm"); String plaintext = "Hello, world!"; String ciphertext = cipher.encrypt(plaintext); String decryptedPlaintext = cipher.decrypt(ciphertext); System.out.println(ciphertext); // "Ittqj, itlja!" System.out.println(decryptedPlaintext); // "Hello, world!" ``` 其中,构造函数中的参数`key`是一个由26个字母组成的字符串,表示明文字母到密文字母的映射关系。`encrypt`方法将明文字符串转换为密文字符串,`decrypt`方法将密文字符串转换为明文字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值