fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <cstdint>
  5. #include <algorithm>
  6.  
  7. //this is not URI_Encode.
  8. //this is like a URI_encode.not same.
  9.  
  10. std::string ItoA(std::int64_t N, const std::string& Ch) {
  11. std::string R;
  12. bool F = N >= 0 ? true : false;
  13. while (N != 0) {
  14. R += Ch[N % Ch.size()];
  15. N /= Ch.size();//radix divide
  16. }
  17. if (!F) R += '-';
  18.  
  19. std::reverse(R.begin(), R.end());
  20.  
  21. return R;
  22.  
  23.  
  24. }
  25. std::intmax_t AtoI(const std::string& S, const std::string& Ch) {
  26. std::uintmax_t R=0;
  27. bool F = S[0] != '-'? true : false;
  28. for(auto &o:S){
  29. if (o == '-')continue;
  30. auto it = std::find(Ch.begin(), Ch.end(),o);
  31. R *= Ch.size();//radix multiple.
  32. R += std::distance(Ch.begin(), it);
  33. }
  34. if (!F)R *= -1;
  35.  
  36. return R;
  37.  
  38.  
  39. }
  40.  
  41. std::string ReversibleHash_Encode(const std::string& S,std::string C) {
  42. std::string R;
  43. for (auto& o : S) {
  44. R += '%';
  45. R += ItoA(static_cast<std::uint8_t>(o), C);
  46. }
  47. return R;
  48. }
  49. std::string ReversibleHash_Decode(const std::string& S,std::string C) {
  50. std::string R;
  51. std::string V;
  52. bool F = true;
  53. for (auto& o : S) {
  54. std::uint8_t N = o;
  55. if (o != '%') {
  56. F = false;
  57. V += o;
  58. }
  59. else {
  60. if (F)continue;
  61. R += AtoI(V.data(),C);
  62. V.clear();
  63. }
  64. }
  65. if (V.size())R += AtoI(V.data(), C);
  66. return R;
  67. }
  68. int main() {
  69. {
  70. std::string R;
  71. std::string S;
  72.  
  73. S = "ウィキペディア";
  74.  
  75. R = ReversibleHash_Encode(S, "0123456789");
  76.  
  77. std::cout << R << std::endl;
  78.  
  79. R = ReversibleHash_Decode(R, "0123456789");
  80.  
  81. std::cout << R << std::endl;
  82. }
  83. {
  84. //dictionary Crypt.
  85. std::string R;
  86. std::string S;
  87. std::string Ch = "abcdefghijklnmopqrstuvwxyz0123456789";
  88. S = "ウィキペディア";
  89.  
  90. R = ReversibleHash_Encode(S, Ch);
  91.  
  92. std::cout << R << std::endl;
  93.  
  94. R = ReversibleHash_Decode(R, Ch);
  95.  
  96. std::cout << R << std::endl;
  97. }
  98. return 0;
  99. }
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
%227%130%166%227%130%163%227%130%173%227%131%154%227%131%135%227%130%163%227%130%162
ウィキペディア
%gl%dw%ew%gl%dw%et%gl%dw%e3%gl%dx%ek%gl%dx%d1%gl%dw%et%gl%dw%es
ウィキペディア