Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int side=6;
  5. const int alph=side*side;
  6. const int ZZZ=25;
  7.  
  8. void db(string a)
  9. {
  10. for(int i=0;i<alph;i++)
  11. {
  12. cout<<a[i];
  13. if(i%side==5)
  14. {
  15. cout<<endl;
  16. }
  17. }
  18. }
  19.  
  20. string shift(string a, char c)
  21. {
  22. for(int i=0;i<a.length();i++)
  23. {
  24. a[i]+=c;
  25. }
  26. return a;
  27. }
  28.  
  29. string redukcja(string klucz)
  30. {
  31. bool tab[alph]={};
  32. string out="";
  33. for(int i=0;i<klucz.length();i++)
  34. {
  35. char current=klucz[i];
  36. if(tab[current]==0)
  37. {
  38. tab[current]=1;
  39. out+=current;
  40. }
  41. }
  42.  
  43. for(int i=0;i<alph;i++)
  44. {
  45. if(tab[i]==0)
  46. {
  47. out+=i;
  48. }
  49. }
  50.  
  51. return out;
  52. }
  53.  
  54. string szyfrowanie(string msg, string key)
  55. {
  56. key=redukcja(key);
  57.  
  58. int crdx[alph];
  59. int crdy[alph];
  60. char vals[side][side];
  61.  
  62. for(int i=0;i<alph;i++)
  63. {
  64. int tx=i%side;
  65. int ty=i/side;
  66. crdx[i]=tx;
  67. crdy[i]=ty;
  68. vals[tx][ty]=key[i];
  69. }
  70.  
  71. string out="";
  72. if(msg.length()%2==1)
  73. {
  74. msg+=ZZZ;
  75. }
  76. for(int i=0;i<msg.length();i+=2)
  77. {
  78. char a=msg[i];
  79. char b=msg[i+1];
  80. if(a==b)
  81. {
  82. b=ZZZ;
  83. }
  84. int x1=crdx[a];
  85. int x2=crdx[b];
  86. int y1=crdy[a];
  87. int y2=crdy[b];
  88.  
  89. if(crdy[a]==crdy[b]) //wiersz
  90. {
  91. x1=(x1+1)%side;
  92. x2=(x2+1)%side;
  93. out+=vals[x1][y1];
  94. out+=vals[x2][y2];
  95. }
  96. else
  97. if(crdx[a]==crdx[b])
  98. {
  99. y1=(y1+1)%side;
  100. y2=(y2+1)%side;
  101. out+=vals[x1][y1];
  102. out+=vals[x2][y2];
  103. }
  104. else
  105. {
  106. out+=vals[x2][y1];
  107. out+=vals[x1][y2];
  108. }
  109. }
  110.  
  111. return out;
  112. }
  113.  
  114. int main()
  115. {
  116. string key,msg;
  117. cin>>key>>msg;
  118. key=shift(key,-'A');
  119. msg=shift(msg,-'A');
  120.  
  121. string ooo=szyfrowanie(msg,key);
  122. ooo=shift(ooo,'A');
  123. key=shift(key,'A');
  124. cout<<ooo<<endl;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement