c语言vigenere密码
In this tutorial you will learn about vigenere cipher in C and C++ for encryption and decryption.
在本教程中,您将学习C和C ++中用于加密和解密的vigenere密码。
Vigenere Cipher is kind of polyalphabetic substitution method. It is used for encryption of alphabetic text. For encryption and decryption Vigenere Cipher Table is used in which alphabets from A to Z are written in 26 rows.
Vigenere密码是一种多字母替换方法。 它用于加密字母文本。 为了进行加密和解密,使用了Vigenere密码表,其中从A到Z的字母写在26行中。
Also Read: Caesar Cipher in C and C++ [Encryption & Decryption]
另请阅读: C和C ++中的Caesar密码[加密和解密]
Also Read: Hill Cipher in C and C++ (Encryption and Decryption)
另请阅读: C和C ++中的Hill Cipher(加密和解密)
Vigenere密码加密 (Vigenere Cipher Encryption)
Message Text: THECRAZYPROGRAMMER
消息文本: THECRAZYPROGRAMMER
Key: HELLO
钥匙: HELLO
Here we have to obtain a new key by repeating the given key till its length become equal to original message length.
在这里,我们必须通过重复给定密钥直到其长度等于原始消息长度来获得新密钥。
New Generated Key: HELLOHELLOHELLOHEL
新生成的密钥: HELLOHELLOHELLOHEL
For encryption take first letter of message and new key i.e. T and H. Take the alphabet in Vigenere Cipher Table where T row and H column coincides i.e. A.
对于加密,请使用消息的首字母和新密钥,即T和H。在Vigenere密码表中使用字母,其中T行和H列重合,即A。
Repeat the same process for all remaining alphabets in message text. Finally the encrypted message text is:
对消息文本中的所有剩余字母重复相同的过程。 最后,加密的消息文本为:
Encrypted Message: ALPNFHDJAFVKCLATIC
加密的消息: ALPNFHDJAFVKCLATIC
The algorithm can be expressed in algebraic form as given below. The cipher text can be generated by below equation.
该算法可以以代数形式表示,如下所示。 密文可以通过以下等式生成。
Ei = (Pi + Ki) mod 26
E i =(P i + K i )mod 26
Here P is plain text and K is key.
这里P是纯文本,K是密钥。
Vigenere密码解密 (Vigenere Cipher Decryption)
Encrypted Message: ALPNFHDJAFVKCLATIC
加密的消息: ALPNFHDJAFVKCLATIC
Key: HELLO
钥匙: HELLO
New Generated Key: HELLOHELLOHELLOHEL
新生成的密钥: HELLOHELLOHELLOHEL
Take first alphabet of encrypted message and generated key i.e. A and H. Analyze Vigenere Cipher Table, look for alphabet A in column H, the corresponding row will be the first alphabet of original message i.e. T.
取加密消息的第一个字母和生成的密钥,即A和H。分析Vigenere密码表,在H列中查找字母A,相应的行将是原始消息的第一个字母,即T。
Repeat the same process for all the alphabets in encrypted message.
对加密消息中的所有字母重复相同的过程。
Original Message: THECRAZYPROGRAMMER
原始消息: THECRAZYPROGRAMMER
Above process can be represented in algebraic form by following equation.
上述过程可以通过以下等式以代数形式表示。
Pi = (Ei – Ki + 26) mod 26
i =(E i – K i + 26)mod 26
We will use above algebraic equations in the program.
我们将在程序中使用上述代数方程。
Vigenere密码程序 (Program for Vigenere Cipher in C)
#include<stdio.h>
#include<string.h>
int main(){
char msg[] = "THECRAZYPROGRAMMER";
char key[] = "HELLO";
int msgLen = strlen(msg), keyLen = strlen(key), i, j;
char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];
//generating new key
for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == keyLen)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '\0';
//encryption
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
encryptedMsg[i] = '\0';
//decryption
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
decryptedMsg[i] = '\0';
printf("Original Message: %s", msg);
printf("\nKey: %s", key);
printf("\nNew Generated Key: %s", newKey);
printf("\nEncrypted Message: %s", encryptedMsg);
printf("\nDecrypted Message: %s", decryptedMsg);
return 0;
}
Output
输出量
Original Message: THECRAZYPROGRAMMER Key: HELLO New Generated Key: HELLOHELLOHELLOHEL Encrypted Message: ALPNFHDJAFVKCLATIC Decrypted Message: THECRAZYPROGRAMMER
原始消息:THECRAZYPROGRAMMER 密钥:HELLO 新生成的密钥:HELLOHELLOHELLOHEL 加密消息:ALPNFHDJAFVKCLATIC 解密消息:THECRAZYPROGRAMMER
C ++中的Vigenere密码程序 (Program for Vigenere Cipher in C++)
#include<iostream>
#include<string.h>
using namespace std;
int main(){
char msg[] = "THECRAZYPROGRAMMER";
char key[] = "HELLO";
int msgLen = strlen(msg), keyLen = strlen(key), i, j;
char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];
//generating new key
for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == keyLen)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '\0';
//encryption
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';
encryptedMsg[i] = '\0';
//decryption
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';
decryptedMsg[i] = '\0';
cout<<"Original Message: "<<msg;
cout<<"\nKey: "<<key;
cout<<"\nNew Generated Key: "<<newKey;
cout<<"\nEncrypted Message: "<<encryptedMsg;
cout<<"\nDecrypted Message: "<<decryptedMsg;
return 0;
}
Comment below if you have queries or found anything incorrect in above tutorial for vigenere cipher in C and C++.
如果您有疑问或在上述教程中对C和C ++中的vigenere密码有任何不正确的地方,请在下面评论。
翻译自: https://www.thecrazyprogrammer.com/2017/08/vigenere-cipher-c-c.html
c语言vigenere密码
本教程介绍了C和C++中用于加密和解密的Vigenere密码。Vigenere密码是一种多字母替换方法,用于加密字母文本。通过重复密钥来适应原文长度,然后利用Vigenere密码表进行加密和解密。文章提供了加密和解密的详细步骤,并附带C和C++程序示例。

475

被折叠的 条评论
为什么被折叠?



