Mapping Cipher

题目: 输入字母表中所映射的密文, 再将你输入的原文按照对应的密文所转换后输出。(加密)

        Mapping Cipher
You are given an alphabetic mapping table to create cipher text. The table is defined
by a string using 26 lower-case alphabets, such as
incpfvztsmbexowhlgkqjyrdau
In the string all characters are different. Using this table to generate cipher text, it
performs the following mapping scheme:
a -> i, A->I
b -> n, B->N
c -> c, C->C
d -> p, D->P
. . . . . . .
y -> a, Y->A
z -> u, Z->U
Any non-alphabetic character are kept unchanged.
Apply the scheme to plain text:
There are 2 dogs and 3 cats.
It rains dogs & cats!
Obtain the following cipher text:
Qtfgf igf 2 pwzk iop 3 ciqk.
Sq gisok pwzk & ciqk!

Input
There are several test cases. Each test case starts with a line of mapping string
described above. Following are plain text enclosed by two lines of string "***".
Your task is to output the cypher text encrypted using the given mapping string. There
is a blank line after each case. Finally, you will reach a line of string "###" which
signals the end of test. Each line in the test case is no more than 1000 characters. Each
line in test cases is no more than 1000 characters.

Output
The cipher text of each test case is prefixed with a cipher text number as shown in the
sample output. Place a blank line between cases.

Sample Input
incpfvztsmbexowhlgkqjyrdau
***
There are 2 dogs and 3 cats.
It rains dogs & cats!
***
xowhlgkqjyrdauincpfvztsmbe
***
There are 2 dogs and 3 cats.
It rains dogs & cats!
***
###

Sample Output
Cypher 1:
Qtfgf igf 2 pwzk iop 3 ciqk.
Sq gisok pwzk & ciqk!
Cypher 2:
Vqlpl xpl 2 hikf xuh 3 wxvf.
Jv pxjuf hikf & wxvf!

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

int main()
{
	int flag=1, test=1;
	
	while(1)
	{
		char arr[26]={0}; //存取字母映射的密文
		char z[10]={0}; //存取密文后的***
		char str[1001]={0}; //存取输入的原文
		
		gets(arr);
		if(arr[0]=='#' && arr[1]=='#' && arr[2]=='#') break;
		
		gets(z);
		
		if(flag) flag=0;
		else printf("\n");
		
		printf("Cypher %d:\n",test++);
		
		while(gets(str))
		{
			if(str[0]=='*' && str[1]=='*' && str[2]=='*') break;
			
			for(int i=0; i<strlen(str); i++)
			{
				if(str[i]>='a' && str[i]<='z') printf("%c",arr[str[i]-'a']);
				else if(str[i]>='A' && str[i]<='Z') printf("%c",arr[str[i]-'A']+'A'-'a');
				else printf("%c",str[i]);
			}
			
			printf("\n");
		}
		
		scanf("\n");
			
	}
    
    return 0;
}



将一下python代码进行逐句分析,并详细说出为什么这么做# 标准英文频率 std_freq = [ 8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.966, 7.507, 0.153, 0.747, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360, 0.150, 1.974, 0.074 ] # 将每个字母出现的频率进行,标准频率排名 std_rank = [4, 19, 0, 8, 14, 2, 18, 3, 5, 13, 11, 1, 15, 6, 17, 10, 20, 7, 9, 24, 22, 21, 25, 12, 16, 23] #密文中出现的字母及频率 def analyze_cipher(cipher): count = [0] * 26 total = 0 for char in cipher: if char.isalpha(): c = char.upper() count[ord(c) - ord('A')] += 1 total += 1 freq = [count[i] * 100.0 / total if total else 0 for i in range(26)] return count, freq #密文中出现字母频率的排名 def get_rank(freq): rank = [0] * 26 for i in range(26): for j in range(26): if freq[j] > freq[i]: rank[i] += 1 return rank #初始映射表 def init_mapping(cipher_rank): mapping = [None] * 26 used = [False] * 26 # 优先匹配高频字母(前6位:E,T,A,O,I,N) for i in range(6): for c in range(26): if cipher_rank[c] == i: for p in range(26): if std_rank[p] == i and not used[p]: mapping[c] = chr(ord('A') + p) used[p] = True break break # 填充剩余字母 for c in range(26): if mapping[c] is None: for p in range(26): if not used[p]: mapping[c] = chr(ord('A') + p) used[p] = True break return mapping #使用映射表进行解密 def decrypt(cipher, mapping): plain = [] for char in cipher: if char.isalpha(): c = char.upper() new_char = mapping[ord(c) - ord('A')] plain.append(new_char.lower() if char.islower() else new_char) else: plain.append(char) return ''.join(plain) #打印密文的频率和标准英文的频率 def print_analysis(count, freq, cipher_rank): print("\n===== 密文频率分析 =====") print("字母 | 出现次数 | 频率(%) | 排名") print("-------------------------") for i in range(26): print(f" {chr(ord('A') + i)} | {count[i]:5d} | {freq[i]:5.2f} | {cipher_rank[i]:2d}") print("\n===== 标准英文频率排名 =====") print("高频字母(前6位): E(12.7%) > T(9.1%) > A(8.2%) > O(7.5%) > I(7.0%) > N(6.7%)") #主函数 cipher = input("请输入密文: ") count, freq = analyze_cipher(cipher) cipher_rank = get_rank(freq) print_analysis(count, freq, cipher_rank) mapping = init_mapping(cipher_rank) plain = decrypt(cipher, mapping) while True: print(f"\n当前解密结果\n{plain}") cmd = input("\n映射表调整 (格式: 密文 明文,如 'X E';输入q退出): ") if cmd.lower() == 'q': break if len(cmd) >= 3 and cmd[0].isalpha() and cmd[2].isalpha(): c = ord(cmd[0].upper()) - ord('A') p = ord(cmd[2].upper()) - ord('A') # 处理冲突 for i in range(26): if mapping[i] == chr(ord('A') + p): for j in range(26): if chr(ord('A') + j) not in mapping: mapping[i] = chr(ord('A') + j) break break mapping[c] = chr(ord('A') + p) plain = decrypt(cipher, mapping) else: print("输入格式错误,请重试!") print(f"\n最终解密结果\n{plain}")
10-16
分析修改一下以下python代码并给出修改后的完整代码#include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> const float std_freq[26] = { # type: ignore 8.167, 1.492, 2.782, 4.253, 12.702, 2.228, 2.015, 6.966, 7.507, 0.153, 0.747, 4.025, 2.406, 6.749, 7.507, 1.929, 0.095, 5.987, 6.327, 9.056, 2.758, 0.978, 2.360, 0.150, 1.974, 0.074 }; void analyze_cipher(const char *cipher, int count[26], float freq[26]) { # type: ignore int total = 0; memset(count, 0, 26*sizeof(int)); //统计每个字母出现的次数// for (int i = 0; cipher[i]; i++) { if (isalpha(cipher[i])) { char c = toupper(cipher[i]); count[c-'A']++; total++; } } //计算每个字母出现的频率// for (int i = 0; i < 26; i++) { freq[i] = total ? (count[i] * 100.0f / total) : 0; } } void get_rank(const float freq[26], int rank[26]) { # type: ignore for (int i = 0; i < 26; i++) { rank[i] = 0; for (int j = 0; j < 26; j++) { if (freq[j] > freq[i]) rank[i]++; } } } void init_mapping(const int cipher_rank[26], char mapping[26]) { # type: ignore // 标准频率排名 int std_rank[26] = {4,19,0,8,14,2,18,3,5,13,11,1,15,6,17,10,20,7,9,24,22,21,25,12,16,23}; // 临时数组记录已使用的明文字母 int used[26] = {0}; // 优先匹配高频字母(前6位:E,T,A,O,I,N) for (int i = 0; i < 6; i++) { // 找到密文中排名第i的字母 for (int c = 0; c < 26; c++) { if (cipher_rank[c] == i) { // 匹配标准排名第i的字母 for (int p = 0; p < 26; p++) { if (std_rank[p] == i && !used[p]) { mapping[c] = 'A' + p; used[p] = 1; break; } } break; } } } //计算频率的排名// void get_rank(const float freq[26], int rank[26]) { for (int i = 0; i < 26; i++) { rank[i] = 0; for (int j = 0; j < 26; j++) { if (freq[j] > freq[i]) rank[i]++; } } } //填充剩余字母// for (int c = 0; c < 26; c++) { if (!mapping[c]) { // 未映射的密文字母 for (int p = 0; p < 26; p++) { if (!used[p]) { mapping[c] = 'A' + p; used[p] = 1; break; } } } } } //使用映射表解密// void decrypt(const char *cipher, const char mapping[26], char *plain) { for (int i = 0; cipher[i]; i++) { if (isalpha(cipher[i])) { char c = toupper(cipher[i]); plain[i] = islower(cipher[i]) ? tolower(mapping[c-'A']) : mapping[c-'A']; } else { plain[i] = cipher[i]; // 保留非字母字符 } } plain[strlen(cipher)] = '\0'; } //显示频率分析结果// void print_analysis(const int count[26], const float freq[26], const int rank[26]) { printf("\n===== 密文频率分析 =====\n"); printf("字母 | 出现次数 | 频率(%%) | 排名\n"); printf("-------------------------\n"); for (int i = 0; i < 26; i++) { printf(" %c | %5d | %5.2f | %2d\n", 'A'+i, count[i], freq[i], rank[i]); } printf("\n===== 标准英文频率排名 =====\n"); printf("高频字母(前6位): E(12.7%%) > T(9.1%%) > A(8.2%%) > O(7.5%%) > I(7.0%%) > N(6.7%%)\n"); } int main() { char cipher[10000], plain[10000]; int count[26], cipher_rank[26]; float freq[26]; char mapping[26] = {0}; // 密文→明文映射表(索引=密文'A'-'Z') // 输入密文// printf("请输入密文: "); fgets(cipher, sizeof(cipher), stdin); cipher[strcspn(cipher, "\n")] = '\0'; // 移除换行符 //频率分析// analyze_cipher(cipher, count, freq); get_rank(freq, cipher_rank); print_analysis(count, freq, cipher_rank); //生成初始映射并解密// init_mapping(cipher_rank, mapping); decrypt(cipher, mapping, plain); //交互式调整映射// char cmd[10]; while (1) { printf("\n===== 当前解密结果 =====\n%s\n", plain); printf("\n映射表调整 (格式: 密文 明文,如 'X E';输入q退出): "); fgets(cmd, sizeof(cmd), stdin); cmd[strcspn(cmd, "\n")] = '\0'; if (cmd[0] == 'q' || cmd[0] == 'Q') break; // 解析调整命令(如"X E"表示密文X→明文E)// if (strlen(cmd)>=3 && isalpha(cmd[0]) && isalpha(cmd[2])) { char c = toupper(cmd[0]) - 'A'; // 密文字母索引 char p = toupper(cmd[2]) - 'A'; // 明文字母索引 // 处理冲突:如果p已被其他字母映射,先释放 for (int i = 0; i < 26; i++) { if (mapping[i] == 'A' + p) { // 为被占用的字母分配新的未使用字母 for (int j = 0; j < 26; j++) { int used = 0; for (int k = 0; k < 26; k++) { if (mapping[k] == 'A' + j) { used = 1; break; } } if (!used) { mapping[i] = 'A' + j; break; } } break; } } //更新映射// mapping[c] = 'A' + p; decrypt(cipher, mapping, plain); // 重新解密 } else { printf("输入格式错误,请重试!\n"); } } //输出最终结果// printf("\n===== 最终解密结果 =====\n%s\n", plain); return 0; }
10-16
内容概要:本文设计了一种基于PLC的全自动洗衣机控制系统内容概要:本文设计了一种,采用三菱FX基于PLC的全自动洗衣机控制系统,采用3U-32MT型PLC作为三菱FX3U核心控制器,替代传统继-32MT电器控制方式,提升了型PLC作为系统的稳定性与自动化核心控制器,替代水平。系统具备传统继电器控制方式高/低水,实现洗衣机工作位选择、柔和过程的自动化控制/标准洗衣模式切换。系统具备高、暂停加衣、低水位选择、手动脱水及和柔和、标准两种蜂鸣提示等功能洗衣模式,支持,通过GX Works2软件编写梯形图程序,实现进洗衣过程中暂停添加水、洗涤、排水衣物,并增加了手动脱水功能和、脱水等工序蜂鸣器提示的自动循环控制功能,提升了使用的,并引入MCGS组便捷性与灵活性态软件实现人机交互界面监控。控制系统通过GX。硬件设计包括 Works2软件进行主电路、PLC接梯形图编程线与关键元,完成了启动、进水器件选型,软件、正反转洗涤部分完成I/O分配、排水、脱、逻辑流程规划水等工序的逻辑及各功能模块梯设计,并实现了大形图编程。循环与小循环的嵌; 适合人群:自动化套控制流程。此外、电气工程及相关,还利用MCGS组态软件构建专业本科学生,具备PL了人机交互C基础知识和梯界面,实现对洗衣机形图编程能力的运行状态的监控与操作。整体设计涵盖了初级工程技术人员。硬件选型、; 使用场景及目标:I/O分配、电路接线、程序逻辑设计及组①掌握PLC在态监控等多个方面家电自动化控制中的应用方法;②学习,体现了PLC在工业自动化控制中的高效全自动洗衣机控制系统的性与可靠性。;软硬件设计流程 适合人群:电气;③实践工程、自动化及相关MCGS组态软件与PLC的专业的本科生、初级通信与联调工程技术人员以及从事;④完成PLC控制系统开发毕业设计或工业的学习者;具备控制类项目开发参考一定PLC基础知识。; 阅读和梯形图建议:建议结合三菱编程能力的人员GX Works2仿真更为适宜。; 使用场景及目标:①应用于环境与MCGS组态平台进行程序高校毕业设计或调试与运行验证课程项目,帮助学生掌握PLC控制系统的设计,重点关注I/O分配逻辑、梯形图与实现方法;②为工业自动化领域互锁机制及循环控制结构的设计中类似家电控制系统的开发提供参考方案;③思路,深入理解PL通过实际案例理解C在实际工程项目PLC在电机中的应用全过程。控制、时间循环、互锁保护、手动干预等方面的应用逻辑。; 阅读建议:建议结合三菱GX Works2编程软件和MCGS组态软件同步实践,重点理解梯形图程序中各环节的时序逻辑与互锁机制,关注I/O分配与硬件接线的对应关系,并尝试在仿真环境中调试程序以加深对全自动洗衣机控制流程的理解。
### Cipher CTF Challenge Solutions and Explanations In the realm of Capture the Flag (CTF) competitions, cipher challenges often involve classical cryptography techniques that test participants' understanding of encryption methods from historical contexts to modern adaptations. Here are some detailed explanations regarding specific ciphers mentioned: #### Caesar Cipher Challenges The Caesar cipher is one of the simplest forms of substitution ciphers where each letter in the plaintext shifts a certain number of places down or up the alphabet[^1]. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. To solve such puzzles within CTFs: ```python def caesar_cipher(text, shift): result = "" for i in range(len(text)): char = text[i] if char.isalpha(): ascii_offset = ord('a') if char.islower() else ord('A') shifted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset) result += shifted_char else: result += char return result ``` This function can both encode and decode messages depending upon whether you add or subtract the `shift` value when calling it. #### Atbash Cipher Puzzles An interesting variant found frequently in these contests includes reversing alphabets entirely as seen through an Atbash cipher approach[^2], which maps 'A' to 'Z', 'B' to 'Y', etc., without any shifting involved but rather direct reversal mapping between characters at opposite ends of the English alphabet sequence. For handling this type of puzzle programmatically: ```python import string def atbash_cipher(message): letters = string.ascii_lowercase trans = str.maketrans(letters, letters[::-1]) return message.translate(trans) print(atbash_cipher("hello")) # svool ``` Such scripts provide efficient ways to tackle common cryptographic tasks encountered during CTF events while also serving educational purposes about traditional coding practices used historically before digital computers came into existence.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值