No Description

cypher.cpp 2.6KB

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