Advertisement
a53

ADFGVX

a53
Jul 20th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("adfgvx.in");
  4. ofstream fout("adfgvx.out");
  5. int c,messageLen,keyLen;
  6. char square[6][6];
  7. char Index[]={'A','D','F','G','V','X', '\0'};
  8. char key[27];
  9. char message[1000001];
  10. int main()
  11. {
  12. int i, j, k;
  13. fin>>c;
  14. fin.get();
  15. if(c==1)
  16. {
  17. for(i=0;i<6;++i)
  18. for(j=0;j<6;++j)
  19. fin>>square[i][j];
  20. fin>>message;
  21. fin>>key;
  22. messageLen=strlen(message);
  23. keyLen=strlen(key);
  24. char cipher1[2*messageLen];
  25. for(i=0;i<messageLen;++i)
  26. for(j=0;j<6;++j)
  27. for(k=0;k<6;++k)
  28. if(message[i]==square[j][k])
  29. cipher1[2*i]=Index[j],cipher1[2*i+1]=Index[k];
  30. cipher1[2*messageLen]='\0';
  31. int cipherLen=sizeof(cipher1);
  32. int rows;
  33. if(cipherLen%keyLen>0)
  34. rows=cipherLen/keyLen+1;
  35. else
  36. rows=cipherLen/keyLen;
  37. char A[rows][keyLen];
  38. i=0;j=0;
  39. for(k=0;k<cipherLen;++k)
  40. {
  41. if(k && k%keyLen==0)
  42. ++i,j=0;
  43. A[i][j++]=cipher1[k];
  44. }
  45. if(cipherLen%keyLen>0)
  46. for(j=cipherLen%keyLen; j<keyLen; ++j)
  47. A[rows-1][j]=' ';
  48. ++i;
  49. int pos=0;
  50. char B[rows][keyLen];
  51. for(char c='A';c<='Z';++c)
  52. for(j=0;j<keyLen;++j)
  53. if(key[j]==c)
  54. {
  55. for(i=0;i<rows;++i)
  56. B[i][pos]=A[i][j];
  57. ++pos;
  58. }
  59. for(j=0; j<keyLen; ++j)
  60. {
  61. for(i=0;i<rows;++i)
  62. if(B[i][j]!=' ')
  63. fout<<B[i][j];
  64. fout<<' ';
  65. }
  66. }
  67. else
  68. {
  69. for(i=0;i<6;++i)
  70. for(j=0;j<6;++j)
  71. fin>>square[i][j];
  72. fin.get();
  73. char Key[770];
  74. fin>>Key;
  75. j=0;
  76. int rows=strlen(Key)+1;
  77. char B[rows][26];
  78. while(!fin.eof())
  79. {
  80. for(i=0;i<(int)strlen(Key);++i)
  81. B[i][j]=Key[i];
  82. if((int)strlen(Key)<rows)
  83. {
  84. Key[rows-1]=' ',Key[rows]='\0';
  85. B[rows-1][j]=' ';
  86. }
  87. ++j;
  88. fin.get();
  89. fin>>Key;
  90. }
  91. int NrLit=0,NrCol=--j;
  92. for(j=0; j<NrCol; ++j)
  93. if(B[rows-1][j]>='A' && B[rows-1][j]<='Z')
  94. ++NrLit;
  95. if(NrLit==0)
  96. --rows;
  97. else
  98. for(j=0;j<NrCol;++j)
  99. if(B[rows-1][j]<'A' || B[rows-1][j]>'Z')
  100. B[rows-1][j]=' ';
  101. int keyLen=strlen(Key);
  102. char A[rows][keyLen];
  103. int pos=0;
  104. for(char c='A';c<='Z';++c)
  105. for(j=0;j<keyLen;++j)
  106. if(Key[j]==c)
  107. {
  108. for(i=0;i<rows;++i)
  109. A[i][j]=B[i][pos];
  110. ++pos;
  111. }
  112. k=0;
  113. int nrsp=0;
  114. char cipher[rows*keyLen];
  115. for(i=0;i<rows;++i)
  116. for(j=0;j<keyLen;++j)
  117. if(A[i][j]!=' ')
  118. cipher[k++]=A[i][j];
  119. else
  120. ++nrsp;
  121. cipher[k]='\0';
  122. int Lin=0,Col=0;
  123. for(i=0; i<k; i+=2)
  124. {
  125. for(j=0;j<6;++j)
  126. {
  127. if(Index[j]==cipher[i])
  128. Lin=j;
  129. if(Index[j]==cipher[i+1])
  130. Col=j;
  131. }
  132. fout<<square[Lin][Col];
  133. }
  134. }
  135. return 0;
  136. }
  137.  
  138. /**
  139. #include <fstream>
  140. using namespace std;
  141. string character[130],cheie,mesaj,criptmesaj,decriptmesaj,sir[10000];
  142. char invcharacter[2727];
  143. char matrix[1001][1001];
  144. int cer;
  145. int main()
  146. {
  147. ifstream cin("adfgvx.in");
  148. ofstream cout("adfgvx.out");
  149. cin>>cer;
  150. char ch;
  151. for(int i=1;i<=6;i++)
  152. for(int j=1;j<=6;j++)
  153. {
  154. cin>>ch;
  155. if(i==1)
  156. character[ch]+="A";
  157. else if(i==2)
  158. character[ch]+="D";
  159. else if(i==3)
  160. character[ch]+="F";
  161. else if(i==4)
  162. character[ch]+="G";
  163. else if(i==5)
  164. character[ch]+="V";
  165. else if(i==6)
  166. character[ch]+="X";
  167. if(j==1)
  168. character[ch]+="A";
  169. else if(j==2)
  170. character[ch]+="D";
  171. else if(j==3)
  172. character[ch]+="F";
  173. else if(j==4)
  174. character[ch]+="G";
  175. else if(j==5)
  176. character[ch]+="V";
  177. else if(j==6)
  178. character[ch]+="X";
  179. string cuv=character[ch];
  180. invcharacter[(cuv[0]-'A'+1)*100+cuv[1]-'A'+1]=ch;
  181. }
  182. if(cer==1)
  183. {
  184. cin>>mesaj>>cheie;
  185. for(int i=0;i<mesaj.size();i++)
  186. criptmesaj+=character[mesaj[i]];
  187. for(int i=0;i<criptmesaj.size();i++)
  188. {
  189. matrix[i/cheie.size()][i%cheie.size()]=criptmesaj[i];
  190. }
  191. for(char l='A';l<='Z';l++)
  192. {
  193. for(int j=0;j<cheie.size();j++)
  194. {
  195. if(cheie[j]==l)
  196. {
  197. for(int i=0;i<criptmesaj.size()/cheie.size()+1;i++)
  198. if(matrix[i][j]>='A' && matrix[i][j]<='Z')
  199. cout<<matrix[i][j];
  200. cout<<" ";
  201. }
  202. }
  203. }
  204. }
  205. else
  206. {
  207. int cnt=0;
  208. string s;
  209. while(cin>>s)
  210. sir[++cnt]=s;
  211. int poz=1,maxx=0;
  212. string length=sir[cnt];
  213. for(char ch='A';ch<='Z';ch++)
  214. {
  215. for(int i=0;i<length.size();i++)
  216. if(length[i]==ch)
  217. {
  218. string a=sir[poz];
  219. for(int k=0;k<a.size();k++)
  220. matrix[k][i]=a[k];
  221. poz++;
  222. int l=a.size();
  223. maxx=max(maxx,l);
  224. }
  225. }
  226. for(int i=0;i<maxx;i++)
  227. for(int j=0;j<length.size();j++)
  228. if(matrix[i][j]>='A' && matrix[i][j]<='Z')
  229. decriptmesaj+=matrix[i][j];
  230. for(int j=0;j<decriptmesaj.size();j+=2)
  231. cout<<invcharacter[(decriptmesaj[j]-'A'+1)*100+decriptmesaj[j+1]-'A'+1];
  232. }
  233. return 0;
  234. }
  235. */
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement