Advertisement
koprobo

szyfry

Jun 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include <fstream>
  4. #include<cstring>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void kodowanie(char *napis)
  10. {
  11.     int dl = strlen(napis); //wyznaczenie liczby znaków
  12.    
  13.     for(int i=0; i<dl-1; i+=2) //przesuwamy się o dwa znaki
  14.     //zamiana sąsiadujących znaków
  15.     {
  16.         char pom = napis[i];
  17.         napis[i] = napis[i+1]; //dlatego w pętli i<dl-1
  18.         napis[i+1] = pom;  
  19.     }
  20. }
  21.  
  22. int main()
  23. {
  24.     char napis[100];
  25.    
  26.     cout<<"Podaj napis do zaszyfrowania: ";
  27.     cin.getline(napis, 100);
  28.    
  29.     cout<<"Przed szyfrowaniem: ";
  30.     cout<<napis<<endl;
  31.    
  32.     //szyfrujemy
  33.     kodowanie(napis);
  34.    
  35.     cout<<"Szyfrogram: ";
  36.     cout<<napis<<endl;
  37.    
  38.     //deszyfrujemy
  39.     kodowanie(napis);
  40.    
  41.     cout<<"Tekst jawny: ";
  42.     cout<<napis<<endl;
  43.    
  44.     cin.get();
  45.     return 0;
  46. }
  47.  
  48.  //This function receives text and shift and
  49. // returns the encrypted text
  50.  
  51. //CEZAR
  52. string encrypt(string text, int s)
  53. {
  54.     string result = "";
  55.  
  56.     // traverse text
  57.     for (int i=0;i<text.length();i++)
  58.     {
  59.         // apply transformation to each character
  60.         // Encrypt Uppercase letters
  61.         if (isupper(text[i]))
  62.             result += char(int(text[i]+s-65)%26 +65);
  63.  
  64.     // Encrypt Lowercase letters
  65.     else
  66.         result += char(int(text[i]+s-97)%26 +97);
  67.     }
  68.  
  69.     // Return the resulting string
  70.     return result;
  71. }
  72.  
  73. // Driver program to test the above function
  74. int main()
  75. {
  76.     string text="ATTACKATONCE";
  77.     int s = 4;
  78.     cout << "Text : " << text;
  79.     cout << "\nShift: " << s;
  80.     cout << "\nCipher: " << encrypt(text, s);
  81.     system("pause");
  82.     return 0;
  83. }
  84. ////////XOR
  85. void encryptDecrypt(char inpString[])
  86. {
  87.     // Define XOR key
  88.     // Any character value will work
  89.     char xorKey = 'P';
  90.  
  91.     // calculate length of input string
  92.     int len = strlen(inpString);
  93.  
  94.     // perform XOR operation of key
  95.     // with every caracter in string
  96.     for (int i = 0; i < len; i++)
  97.     {
  98.         inpString[i] = inpString[i] ^ xorKey;
  99.         printf("%c",inpString[i]);
  100.     }
  101. }
  102.  
  103.  Driver program to test above function
  104. int main()
  105. {
  106.     char sampleString[] = "GeeksforGeeks";
  107.  
  108.     // Encrypt the string
  109.     printf("Encrypted String: ");
  110.     encryptDecrypt(sampleString);
  111.     printf("\n");
  112.  
  113.     // Decrypt the string
  114.     printf("Decrypted String: ");
  115.     encryptDecrypt(sampleString);
  116.   system("pause");
  117.     return 0;
  118. }
  119. string GetFilePathFromUser() {
  120.     string path;
  121.     cout << "Please enter the full file path: ";
  122.     cin >> path;
  123.     return path;
  124. }
  125. string GetFileData() {
  126.    
  127.     string filePath = GetFilePathFromUser();
  128.    
  129.     string txt;
  130.     string resultTxt;
  131.    
  132.    
  133.     ifstream inFile("a.txt", std::ios_base::binary);
  134.     inFile.open(filePath);
  135.     if (!inFile) {
  136.         cout << "Unable to open file \n";
  137.         exit(1); // terminate with error
  138.     }
  139.    
  140.     inFile >> resultTxt;
  141.    
  142.     while (inFile >> txt) {
  143.         resultTxt += txt;
  144.     }
  145.    
  146.     inFile.close();
  147.     return resultTxt;
  148. }
  149. string encrypt(string text, int s)
  150. {
  151.     string result = "";
  152.  
  153.      traverse text
  154.     for (int i=0;i<text.length();i++)
  155.     {
  156.          apply transformation to each character
  157.          Encrypt Uppercase letters
  158.         if (isupper(text[i]))
  159.             result += char(int(text[i]+s-65)%26 +65);
  160.  
  161.      Encrypt Lowercase letters
  162.     else
  163.         result += char(int(text[i]+s-97)%26 +97);
  164.     }
  165.  
  166.      Return the resulting string
  167.     return result; }
  168.  
  169. int main()
  170. { string txt = GetFileData();
  171. cout<<txt<<endl;
  172.   cout<<encrypt(txt,2);
  173.   system("pause");
  174.   return 0;}
  175.  
  176. rc4
  177.     #include <stdio.h>
  178. #include <string.h>
  179. #include <stdlib.h>
  180.  
  181. #define N 256   // 2^8
  182.  
  183. void swap(unsigned char *a, unsigned char *b) {
  184.     int tmp = *a;
  185.     *a = *b;
  186.     *b = tmp;
  187. }
  188.  
  189. int KSA(char *key, unsigned char *S) {
  190.  
  191.     int len = strlen(key);
  192.     int j = 0;
  193.  
  194.     for(int i = 0; i < N; i++)
  195.         S[i] = i;
  196.  
  197.     for(int i = 0; i < N; i++) {
  198.         j = (j + S[i] + key[i % len]) % N;
  199.  
  200.         swap(&S[i], &S[j]);
  201.     }
  202.  
  203.     return 0;
  204. }
  205.  
  206. int PRGA(unsigned char *S, char *plaintext, unsigned char *ciphertext) {
  207.  
  208.     int i = 0;
  209.     int j = 0;
  210.  
  211.     for(size_t n = 0, len = strlen(plaintext); n < len; n++) {
  212.         i = (i + 1) % N;
  213.         j = (j + S[i]) % N;
  214.  
  215.         swap(&S[i], &S[j]);
  216.         int rnd = S[(S[i] + S[j]) % N];
  217.  
  218.         ciphertext[n] = rnd ^ plaintext[n];
  219.  
  220.     }
  221.  
  222.     return 0;
  223. }
  224.  
  225. int RC4(char *key, char *plaintext, unsigned char *ciphertext) {
  226.  
  227.     unsigned char S[N];
  228.     KSA(key, S);
  229.  
  230.     PRGA(S, plaintext, ciphertext);
  231.  
  232.     return 0;
  233. }
  234.  
  235. int main(int argc, char *argv[]) {
  236.  
  237.     if(argc < 3) {
  238.         printf("Usage: %s <key> <plaintext>", argv[0]);
  239.         return -1;
  240.     }
  241.  
  242.     unsigned char *ciphertext = malloc(sizeof(int) * strlen(argv[2]));
  243.  
  244.     RC4(argv[1], argv[2], ciphertext);
  245.  
  246.     for(size_t i = 0, len = strlen(argv[2]); i < len; i++)
  247.         printf("%02hhX", ciphertext[i]);
  248.  
  249.     return 0;
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement