a53

Vigenere

a53
Oct 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. ifstream fin("vigenere.in");
  5. ofstream fout("vigenere.out");
  6. string text, cod;
  7. void encrypt_vigenere(const string& plaintext, const string& key)
  8. {
  9. string ciphertext;
  10. unsigned int i, j, ch;
  11. for(i = 0, j = 0; i < plaintext.length(); ++i, j++) {
  12. if(j >= key.length())
  13. j = 0;
  14. if(plaintext[i] >= 'A' && plaintext[i] <= 'Z')
  15. ch = ((plaintext[i] - 'A') + (key[j] - 'A')) % 26;
  16. else
  17. ch = ((plaintext[i] - 'a') + (key[j] - 'a')) % 26;
  18. if(isupper(ch))
  19. ciphertext += (char)(ch + 'A');
  20. else
  21. ciphertext += (char)(ch + 'a');
  22. }
  23. fout<< ciphertext;
  24. }
  25. int main()
  26. {
  27. fin>>text>>cod;
  28. encrypt_vigenere(text, cod);
  29. return 0;
  30. }
Add Comment
Please, Sign In to add comment