An example of why you should not roll your own crypto
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 rivejä
1.7 KiB

5 vuotta sitten
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <assert.h>
  7. std::string load_file(std::string filename, size_t end_cut = 0)
  8. {
  9. std::stringstream ret;
  10. std::ifstream file(filename);
  11. while(file.good())
  12. {
  13. ret<<(char)file.get();
  14. }
  15. auto rret = ret.str();
  16. rret.resize(rret.size()-end_cut);
  17. return rret;
  18. }
  19. int main(int argc, char** argv)
  20. {
  21. std::vector<std::string> params;
  22. for(int i=0;i<argc;i++)
  23. {
  24. params.push_back(std::string(argv[i]));
  25. }
  26. if(params.size()==2)
  27. {
  28. auto ciphertext = load_file(params[1],4);
  29. int jumper=0;
  30. bool is_okay = true;
  31. do{
  32. is_okay=true;
  33. for(size_t idx=0;idx<ciphertext.size();idx++)
  34. {
  35. if(!isgraph((char)(ciphertext[idx]-jumper+idx)))
  36. {
  37. is_okay=false;
  38. jumper++;
  39. }
  40. }
  41. }while((!is_okay) && jumper<=256);
  42. if(is_okay)
  43. {
  44. for(size_t idx=0;idx<ciphertext.size();idx++)
  45. {
  46. std::cout<<(char)(ciphertext[idx]-jumper+idx);
  47. }
  48. }else{
  49. std::cout<<"Unable to find a valid representation"<<std::endl;
  50. }
  51. }
  52. else if(params.size()==3)
  53. {
  54. auto ciphertext = load_file(params[1],4);
  55. auto zeroed = load_file(params[2],4);
  56. assert(ciphertext.size()==zeroed.size());
  57. for(size_t idx=0;idx<ciphertext.size();idx++)
  58. {
  59. std::cout<<(char)(ciphertext[idx]-zeroed[idx]);
  60. }
  61. }
  62. else
  63. {
  64. std::cout<<
  65. "codebreak 0.1.0\n"
  66. "usage:\n"
  67. "codebreak encrypted_file [0ed_ciphertext]"
  68. <<std::endl;
  69. }
  70. }