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
带开环升压转换器和逆变器的太阳能光伏系统 太阳能光伏系统驱动开环升压转换器和SPWM逆变器提供波形稳定、设计简单的交流电的模型 Simulink模型展示了一个完整的基于太阳能光伏的直流到交流电力转换系统,该系统由简单、透明、易于理解的模块构建而成。该系统从配置为提供真实直流输出电压的光伏阵列开始,然后由开环DC-DC升压转换器进行处理。升压转换器将光伏电压提高到适合为单相全桥逆变器供电的稳定直流链路电平。 逆变器使用正弦PWM(SPWM)开关来产生干净的交流输出波形,使该模型成为研究直流-交流转换基本操作的理想选择。该设计避免了闭环和MPPT的复杂性,使用户能够专注于光伏接口、升压转换和逆变器开关的核心概念。 此模型包含的主要功能: •太阳能光伏阵列在标准条件下产生~200V电压 •具有固定占空比操作的开环升压转换器 •直流链路电容器,用于平滑和稳定转换器输出 •单相全桥SPWM逆变器 •交流负载,用于观察实际输出行为 •显示光伏电压、升压输出、直流链路电压、逆变器交流波形和负载电流的组织良好的范围 •完全可编辑的结构,适合分析、实验和扩展 该模型旨在为太阳能直流-交流转换提供一个干净高效的仿真框架。布局简单明了,允许用户快速了解信号流,检查各个阶段,并根据需要修改参数。 系统架构有意保持模块化,因此可以轻松扩展,例如通过添加MPPT、动态负载行为、闭环升压控制或并网逆变器概念。该模型为进一步开发或整合到更大的可再生能源模拟中奠定了坚实的基础。
### 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、付费专栏及课程。

余额充值