1036[Crypto Columns]

Description

The columnar encryption scheme scrambles the letters in a message (or plaintext) using a keyword as illustrated in the following example: Suppose BATBOY is the keyword and our message is MEET ME BY THE OLD OAK TREE. Since the keyword has 6 letters, we write the message (ignoring spacing and punctuation) in a grid with 6 columns, padding with random extra letters as needed:

MEETME
BYTHEO
LDOAKT
REENTH

Here, we've padded the message with NTH. Now the message is printed out by columns, but the columns are printed in the order determined by the letters in the keyword. Since A is the letter of the keyword that comes first in the alphabet, column 2 is printed first. The next letter, B, occurs twice. In the case of a tie like this we print the columns leftmost first, so we print column 1, then column 4. This continues, printing the remaining columns in order 5, 3 and finally 6. So, the order the columns of the grid are printed would be 2, 1, 4, 5, 3, 6, in this case. This output is called the ciphertext, which in this example would be EYDEMBLRTHANMEKTETOEEOTH. Your job will be to recover the plaintext when given the keyword and the ciphertext.

*****************************************************************

又是一道解密题目。首先有个Keyword。如例子中是BATBOY。这是一个六位单词,所以要把句子排成六列的二维数组,然后按BATBOY中的字母顺序按列输出。如果有相同字母则从左到右的顺序。

所以还比较简单

#include<iostream>
#include<string>
#include <algorithm>

using namespace std;

struct order
{
	char word;
	int num;
};

bool camp( order a, order b )
{
	return a.word < b.word;
};

int main()
{
	char A[10][10];
	int I, J;
	order O[10];
	string K, W;
	cin >> K;
	while( K != "THEEND" )
	{
		cin >> W;
		J = K.length();
		I = W.length();
		I = I / J;
//		cout << I << " " << J << endl;

		for(int j = 0; j < J; j++)
		{
			O[j].word = K[j];
			O[j].num = j;
		}
//		for(int j = 0; j < J; j++)
//			cout << O[j].word;
//		cout << endl;
		sort( O, O+J, camp );
//		for(int j = 0; j < J; j++)
//		{
//			cout << O[j].word << endl;
//			cout << O[j].num << endl;
//		}

		
		int count = 0;
		for(int j = 0; j < J; j++)
		{
			for(int i = 0; i < I; i++)
			{
				A[i][O[j].num] = W[count];
				count++;
			}
		}
		for(int i = 0; i < I; i++)
			for(int j = 0; j < J; j++)
				cout << A[i][j];
		cout << endl;
		cin >> K;
	}
	return 0;
}

备注为测试用的。检测I和J的值。

之前把数组A不小心设成int结果出来一堆数字吓死- -


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Node.js 中的 crypto 是一个内置模块,用于提供加密和解密功能。它支持各种加密算法和操作,包括哈希函数、对称加密和非对称加密。你可以使用 crypto 模块来实现数据的加密、解密、签名和验证等操作。 要使用 crypto 模块,你需要在你的代码中引入它,例如: ```javascript const crypto = require('crypto'); ``` 一些常见的 crypto 操作包括: 1. 哈希函数:crypto 模块提供了多个哈希函数,如 MD5、SHA-1、SHA-256 等。你可以使用这些函数对数据进行哈希处理,生成唯一的摘要。例如: ```javascript const hash = crypto.createHash('sha256'); hash.update('Hello, world!'); const digest = hash.digest('hex'); console.log(digest); // 输出生成的摘要 ``` 2. 对称加密:crypto 模块支持对称加密算法,如 AES、DES、3DES 等。你可以使用这些算法对数据进行加密和解密。例如: ```javascript const cipher = crypto.createCipher('aes192', 'password'); let encrypted = cipher.update('Hello, world!', 'utf8', 'hex'); encrypted += cipher.final('hex'); console.log(encrypted); // 输出加密后的数据 const decipher = crypto.createDecipher('aes192', 'password'); let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log(decrypted); // 输出解密后的数据 ``` 3. 非对称加密:crypto 模块还支持非对称加密算法,如 RSA。你可以使用这些算法生成公钥和私钥,进行加密和解密。例如: ```javascript const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem' } }); console.log(publicKey); // 输出生成的公钥 console.log(privateKey); // 输出生成的私钥 const encrypted = crypto.publicEncrypt(publicKey, Buffer.from('Hello, world!')); console.log(encrypted.toString('base64')); // 输出加密后的数据 const decrypted = crypto.privateDecrypt(privateKey, encrypted); console.log(decrypted.toString('utf8')); // 输出解密后的数据 ``` 这只是 crypto 模块的一小部分功能,你可以查阅 Node.js 文档以获取更详细的信息和使用方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值