Guest User

Untitled

a guest
Dec 16th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int mul_inverse(int x){
  5. if(x<0){
  6. x= ((x%26)+26)%26;
  7. }
  8. for(int i=0; i<26; i++){
  9. if ((x*i)%26==1){
  10. return i;
  11. }
  12. }
  13. return -1;
  14. }
  15. int determinant(int** mat, int n){
  16. if(n==0){
  17. return 0;
  18. }
  19. if(n==1){
  20. return mat[0][0];
  21. }
  22. if (n==2){
  23. return mat[0][0]*mat[1][1]- mat[0][1]*mat[1][0];
  24. }
  25. int det=0;
  26. int** sub_mat= new int*[n-1];
  27. for(int i=0;i<n-1; i++){
  28. sub_mat[i]= new int[n-1];
  29. }
  30. for(int i=0; i<n; i++){
  31. int row=0, col;
  32. for(int j=1; j<n; j++){
  33. col=0;
  34. for(int k=0; k<n; k++){
  35. if (k==i){
  36. continue;
  37. }
  38. sub_mat[row][col]= mat[j][k];
  39. col++;
  40. }
  41. row++;
  42. }
  43. det+= pow(-1, i)* mat[0][i]* determinant(sub_mat, n-1);
  44. }
  45. for(int i=0; i<n-1; i++){
  46. delete [] sub_mat[i];
  47. }
  48. delete [] sub_mat;
  49. return det;
  50. }
  51. int cofactor(int** mat, int n, int r, int c){
  52. int** sub_mat= new int*[n-1];
  53. for(int i=0;i<n-1; i++){
  54. sub_mat[i]= new int[n-1];
  55. }
  56. int row=0, col;
  57. for(int i=0; i<n ; i++){
  58. if(i==r){
  59. continue;
  60. }
  61. col= 0;
  62. for(int j=0; j<n ;j++){
  63. if(j==c){
  64. continue;
  65. }
  66. sub_mat[row][col]= mat[i][j];
  67. col++;
  68. }
  69. row++;
  70. }
  71. int det= determinant(sub_mat, n-1);
  72. for(int i=0; i<n-1; i++){
  73. delete [] sub_mat[i];
  74. }
  75. delete [] sub_mat;
  76. return det;
  77. }
  78. int** inverse(int** mat, int n){
  79. int det= determinant(mat, n);
  80. int det_inv= mul_inverse(det);
  81. if(det_inv==-1){
  82. cout<<"Inverse of matrix does not exist\n";
  83. return NULL;
  84. }
  85. int** inv= new int*[n];
  86. for(int i=0;i<n; i++){
  87. inv[i]= new int[n];
  88. }
  89. for(int i=0; i<n; i++){
  90. for(int j=0; j<n ;j++){
  91. inv[j][i]= ((det_inv*(int)pow(-1, i+j)* cofactor(mat, n, i, j))%26+ 26)%26;
  92. }
  93. }
  94. return inv;
  95. }
  96. int** mat_mul(int** mat1, int** mat2, int m, int n){
  97. int** mat3= new int*[m];
  98. for(int i=0; i<m; i++){
  99. mat3[i]= new int[n];
  100. for(int j=0; j<n; j++){
  101. mat3[i][j]= 0;
  102. }
  103. }
  104. for(int i=0; i<m; i++){
  105. for(int j=0; j<n; j++){
  106. for(int k=0; k<n; k++){
  107. mat3[i][j]= (mat3[i][j]+ mat1[i][k]*mat2[k][j])%26;
  108. }
  109. }
  110. }
  111. return mat3;
  112. }
  113.  
  114. string encrypt(string plain_text, int** key, int n){
  115. int sz= plain_text.size();
  116. int m= sz/n + (sz%n?1:0);
  117. int** p_mat= new int*[m];
  118. int count=0;
  119. for(int i=0; i<m ;i++){
  120. p_mat[i]= new int[n];
  121. for(int j=0; j<n; j++){
  122. if(count>=sz){
  123. p_mat[i][j]= 25; //for z
  124. continue;
  125. }
  126. if (isupper(plain_text[count])){
  127. p_mat[i][j]= plain_text[count]-65;
  128. }else{
  129. p_mat[i][j]= plain_text[count]-97;
  130. }
  131. count++;
  132. }
  133. }
  134. int** c_mat= mat_mul(p_mat, key, m, n);
  135. count=0;
  136. string cipher_text="";
  137. for(int i=0; i<m ;i++){
  138. for(int j=0;j<n; j++){
  139. if (isupper(plain_text[count])){
  140. cipher_text+= char(c_mat[i][j]+65);
  141. }else{
  142. cipher_text+= char(c_mat[i][j]+97);
  143. }
  144. count++;
  145. }
  146. }
  147. for(int i=0; i<m; i++){
  148. delete [] c_mat[i];
  149. delete [] p_mat[i];
  150. }
  151. delete [] c_mat;
  152. delete [] p_mat;
  153. return cipher_text;
  154. }
  155.  
  156. string decrypt(string cipher_text, int** key, int n, int p_sz){
  157. int** inv= inverse(key, n);
  158. if(inv== NULL){
  159. return "";
  160. }
  161. cout<<"Inverse of key is: \n";
  162. for(int i=0; i<n; i++){
  163. for(int j=0; j<n ;j++){
  164. cout<<inv[i][j]<<" ";
  165. }
  166. cout<<endl;
  167. }
  168. int sz= cipher_text.size();
  169. int m= sz/n + (sz%n?1:0);
  170. int** c_mat= new int*[m];
  171. int count=0;
  172. for(int i=0; i<m ;i++){
  173. c_mat[i]= new int[n];
  174. for(int j=0; j<n; j++){
  175. if(count>=sz){
  176. c_mat[i][j]= 25; //for z
  177. continue;
  178. }
  179. if (isupper(cipher_text[count])){
  180. c_mat[i][j]= cipher_text[count]-65;
  181. }else{
  182. c_mat[i][j]= cipher_text[count]-97;
  183. }
  184. count++;
  185. }
  186. }
  187. int** p_mat= mat_mul(c_mat, inv, m, n);
  188. count=0;
  189. string plain_text="";
  190. for(int i=0; i<m ;i++){
  191. for(int j=0;j<n; j++){
  192. if(count>=p_sz){
  193. break;
  194. }
  195. if (isupper(cipher_text[count])){
  196. plain_text+= char(p_mat[i][j]+65);
  197. }else{
  198. plain_text+= char(p_mat[i][j]+97);
  199. }
  200. count++;
  201. }
  202. }
  203. for(int i=0; i<m; i++){
  204. delete [] c_mat[i];
  205. delete [] p_mat[i];
  206. }
  207. for(int i=0; i<n; i++){
  208. delete [] inv[i];
  209. }
  210. delete [] inv;
  211. delete [] c_mat;
  212. delete [] p_mat;
  213. return plain_text;
  214. }
  215.  
  216. int main(){
  217. string plain_text;
  218. cout<<"Enter plain text: ";
  219. getline(cin, plain_text);
  220. int n;
  221. cout<<"Enter key size: ";
  222. cin>>n;
  223. cout<<"Enter key: \n";
  224. int** mat= new int*[n];
  225. for(int i=0;i<n; i++){
  226. mat[i]= new int[n];
  227. }
  228. for(int i=0; i<n; i++){
  229. for(int j=0; j<n; j++){
  230. cin>>mat[i][j];
  231. }
  232. }
  233.  
  234. cout<<"\nEncryption:\n";
  235. string cipher_text= encrypt(plain_text, mat, n);
  236. cout<<"Cipher: "<<cipher_text<<endl;
  237.  
  238. cout<<"\nDecryption:\n";
  239. string p_text= decrypt(cipher_text, mat, n, plain_text.size());
  240. cout<<"Plain Text: "<<p_text<<endl;
  241. for(int i=0; i<n; i++){
  242. delete [] mat[i];
  243. }
  244. delete [] mat;
  245. }
Add Comment
Please, Sign In to add comment