DotheUntwist_1006

这个太简单了,没什么好说的

#include <stdio.h>
#include <string.h>
#define STRING_LEN_MAX 71

char g_ciphertext[STRING_LEN_MAX];
char g_plaintext[STRING_LEN_MAX];
char g_Code2CharTable[] = {'_', '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', '.'};
// c[i] = (p[ki % n] - i) % 28;
//	=>
//	 ki % n = j => i = (j + xn)/k, 
//		(j + xn)%k == 0
//		0<(j + xn)/k<n
//	=> 
//	p[j] = c[(j + xn)/k] + 28*y + (j + xn)/k
//		0<c[(j + xn)/k] + 28*y + (j + xn)/k<28
void TwistChar2Code(char* str, int iStringLen)
{
	for (int i = 0; i < iStringLen; i++)
	{
		switch (str[i])
		{
		case '_':
			str[i] = 0;
			break;
		case 'a':
			str[i] = 1;
			break;
		case 'b':
			str[i] = 2;
			break;
		case 'c':
			str[i] = 3;
			break;
		case 'd':
			str[i] = 4;
			break;
		case 'e':
			str[i] = 5;
			break;
		case 'f':
			str[i] = 6;
			break;
		case 'g':
			str[i] = 7;
			break;
		case 'h':
			str[i] = 8;
			break;
		case 'i':
			str[i] = 9;
			break;
		case 'j':
			str[i] = 10;
			break;
		case 'k':
			str[i] = 11;
			break;
		case 'l':
			str[i] = 12;
			break;
		case 'm':
			str[i] = 13;
			break;
		case 'n':
			str[i] = 14;
			break;
		case 'o':
			str[i] = 15;
			break;
		case 'p':
			str[i] = 16;
			break;
		case 'q':
			str[i] = 17;
			break;
		case 'r':
			str[i] = 18;
			break;
		case 's':
			str[i] = 19;
			break;
		case 't':
			str[i] = 20;
			break;
		case 'u':
			str[i] = 21;
			break;
		case 'v':
			str[i] = 22;
			break;
		case 'w':
			str[i] = 23;
			break;
		case 'x':
			str[i] = 24;
			break;
		case 'y':
			str[i] = 25;
			break;
		case 'z':
			str[i] = 26;
			break;
		case '.':
			str[i] = 27;
			break;
		}
	}
}

void TwistCode2Char(char* str, int iStringLen)
{
	for (int i = 0; i < iStringLen; i++)
	{
		str[i] = g_Code2CharTable[str[i]];
	}
}

void TwistingDecription(char* strCiphertext, int iStringLen, char* strPlaintext, int iKey)
{
	TwistChar2Code(strCiphertext, iStringLen);
	for (int i = 0; i < iStringLen; i++)
	{
		// 遍历有效x
		int y = 0;
		int x = 0;
		for (x = (-i/iStringLen); x<(iKey-i/iStringLen); x++ )
		{
			if((i + x*iStringLen)%iKey != 0)
			{
				continue;
			}

			// 寻找有效y
			y = (-g_ciphertext[(i+x*iStringLen)/iKey]-(i+x*iStringLen)/iKey)/28;
			break;
		}
		strPlaintext[i] = strCiphertext[(i + x*iStringLen)/iKey] + 28*y + (i + x*iStringLen)/iKey;
	}
	TwistCode2Char(strPlaintext, iStringLen);
	strPlaintext[iStringLen] = 0;
}

int main()
{
	int iKey;
	while (scanf("%d %s", &iKey, g_ciphertext) != EOF && iKey != 0)
	{
		TwistingDecription(g_ciphertext, strlen(g_ciphertext), g_plaintext, iKey);
		printf("%s\n", g_plaintext);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值