vigenere密码算法c语言
时间: 2023-10-12 13:13:07 浏览: 113
以下是 Vigenere 密码算法的 C 语言实现代码示例:
```c
#include <stdio.h>
#include <string.h>
void vigenere_encrypt(char plain_text[], char key[]) {
int len_plain_text = strlen(plain_text);
int len_key = strlen(key);
for(int i = 0, j = 0; i < len_plain_text; i++, j++) {
if(j >= len_key) {
j = 0;
}
plain_text[i] = ((plain_text[i] + key[j]) % 26) + 'A';
}
}
void vigenere_decrypt(char cipher_text[], char key[]) {
int len_cipher_text = strlen(cipher_text);
int len_key = strlen(key);
for(int i = 0, j = 0; i < len_cipher_text; i++, j++) {
if(j >= len_key) {
j = 0;
}
cipher_text[i] = ((cipher_text[i] - key[j] + 26) % 26) + 'A';
}
}
int main() {
char plain_text[100];
char key[100];
char cipher_text[100];
printf("Enter plain text: ");
scanf("%s", plain_text);
printf("Enter key: ");
scanf("%s", key);
// Encrypt the plain text
vigenere_encrypt(plain_text, key);
printf("Encrypted text: %s\n", plain_text);
// Decrypt the cipher text
vigenere_decrypt(plain_text, key);
printf("Decrypted text: %s\n", plain_text);
return 0;
}
```
注意:此代码只用于学习和理解 Vigenere 密码算法,不应用于实际安全需求。
阅读全文