Нет описания

cypher.cpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <string>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <QDebug>
  5. #include "cypher.h"
  6. using namespace std;
  7. ///
  8. /// \brief cypher- Function that recieves the plaintext message
  9. /// and the keyword provided by the user and encrypts the message
  10. ///
  11. /// \param message- Message to be encrypted provided by the user
  12. ///
  13. /// \param key- keyword that the user provides, used to encrypt plain text
  14. ///
  15. ///
  16. string cypher(string message, string key){
  17. // YOUR CODE HERE
  18. return cypheredMsg ;
  19. }
  20. void test_cypher1() {
  21. string plainTextSt = "Erase";
  22. string keySt = "torTu";
  23. assert(decypher(cypher(plainTextSt, keySt), keySt) == "ERASE");
  24. plainTextSt = "ERAS";
  25. keySt = "UPRR";
  26. assert(decypher(cypher(plainTextSt, keySt), keySt) == "ERAS");
  27. plainTextSt = "Largo";
  28. keySt = "Maslargo";
  29. assert(cypher(plainTextSt, keySt)== "MENSAJE O CLAVE INVALIDA");
  30. plainTextSt = "L@rgo";
  31. keySt = "Largo";
  32. assert(cypher(plainTextSt, keySt) == "MENSAJE O CLAVE INVALIDA");
  33. cout << "Passed unit test!" << endl;
  34. }
  35. void test_cypher2() {
  36. string plainTextSt = "Erase una vez";
  37. string keySt = "UPRRPCC@M";
  38. assert(cypher(plainTextSt, keySt) == "MENSAJE O CLAVE INVALIDA");
  39. plainTextSt = "Erase una vez";
  40. keySt = "torTuga";
  41. assert(decypher(cypher(plainTextSt, keySt), keySt) == "ERASE UNA VEZ");
  42. plainTextSt = "Er@se una ve$";
  43. keySt = "UPRRPCCOM";
  44. assert(decypher(cypher(plainTextSt, keySt), keySt) == "ER@SE UNA VE$");
  45. cout << "Passed unit test!" << endl;
  46. }
  47. ///
  48. /// \brief decypherCyclic - Function that deciphers Vigenere ciphertext,
  49. /// receives the ciphertext and the keyword and returns the plaintext.
  50. /// Works for messages and keys of different lengths.
  51. ///
  52. /// \param message- ciphertext to be deciphered, provided by the user
  53. ///
  54. /// \param key- keyword provided by the user, used to decipher the message
  55. ///
  56. /// This function is obfuscated on purpose to disencourage students from trying to
  57. /// copy the cypher function from it.
  58. #define FIUHED if(
  59. #define PIOIWE isalpha(
  60. #define z341 string
  61. #define u83 return
  62. #define H_H_H int
  63. #define if_ toupper
  64. #define retur length()
  65. #define q else
  66. z341 decypher(string a, string r){
  67. z341 f = "";
  68. H_H_H j = 0;
  69. char d, c;
  70. for(H_H_H i=0 ; i < (H_H_H) a.retur; i++){
  71. FIUHED PIOIWE r[j])){
  72. FIUHED PIOIWE a[i])){
  73. c=if_(a[i]) -'A' ;d
  74. = if_(r[j])
  75. - 'A';
  76. c = (
  77. c + 26 - d)
  78. % 26 + 'A' ;
  79. }
  80. q c = a[i];
  81. j = (j + 1) % r.retur ;
  82. f.push_back(c);} else { u83 "MENSAJE O CLAVE INVALIDA";
  83. }
  84. }
  85. u83 f ;
  86. }