Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- #include <fstream>
- #include<cstring>
- #include <string>
- using namespace std;
- void kodowanie(char *napis)
- {
- int dl = strlen(napis); //wyznaczenie liczby znaków
- for(int i=0; i<dl-1; i+=2) //przesuwamy się o dwa znaki
- //zamiana sąsiadujących znaków
- {
- char pom = napis[i];
- napis[i] = napis[i+1]; //dlatego w pętli i<dl-1
- napis[i+1] = pom;
- }
- }
- int main()
- {
- char napis[100];
- cout<<"Podaj napis do zaszyfrowania: ";
- cin.getline(napis, 100);
- cout<<"Przed szyfrowaniem: ";
- cout<<napis<<endl;
- //szyfrujemy
- kodowanie(napis);
- cout<<"Szyfrogram: ";
- cout<<napis<<endl;
- //deszyfrujemy
- kodowanie(napis);
- cout<<"Tekst jawny: ";
- cout<<napis<<endl;
- cin.get();
- return 0;
- }
- //This function receives text and shift and
- // returns the encrypted text
- //CEZAR
- string encrypt(string text, int s)
- {
- string result = "";
- // traverse text
- for (int i=0;i<text.length();i++)
- {
- // apply transformation to each character
- // Encrypt Uppercase letters
- if (isupper(text[i]))
- result += char(int(text[i]+s-65)%26 +65);
- // Encrypt Lowercase letters
- else
- result += char(int(text[i]+s-97)%26 +97);
- }
- // Return the resulting string
- return result;
- }
- // Driver program to test the above function
- int main()
- {
- string text="ATTACKATONCE";
- int s = 4;
- cout << "Text : " << text;
- cout << "\nShift: " << s;
- cout << "\nCipher: " << encrypt(text, s);
- system("pause");
- return 0;
- }
- ////////XOR
- void encryptDecrypt(char inpString[])
- {
- // Define XOR key
- // Any character value will work
- char xorKey = 'P';
- // calculate length of input string
- int len = strlen(inpString);
- // perform XOR operation of key
- // with every caracter in string
- for (int i = 0; i < len; i++)
- {
- inpString[i] = inpString[i] ^ xorKey;
- printf("%c",inpString[i]);
- }
- }
- Driver program to test above function
- int main()
- {
- char sampleString[] = "GeeksforGeeks";
- // Encrypt the string
- printf("Encrypted String: ");
- encryptDecrypt(sampleString);
- printf("\n");
- // Decrypt the string
- printf("Decrypted String: ");
- encryptDecrypt(sampleString);
- system("pause");
- return 0;
- }
- string GetFilePathFromUser() {
- string path;
- cout << "Please enter the full file path: ";
- cin >> path;
- return path;
- }
- string GetFileData() {
- string filePath = GetFilePathFromUser();
- string txt;
- string resultTxt;
- ifstream inFile("a.txt", std::ios_base::binary);
- inFile.open(filePath);
- if (!inFile) {
- cout << "Unable to open file \n";
- exit(1); // terminate with error
- }
- inFile >> resultTxt;
- while (inFile >> txt) {
- resultTxt += txt;
- }
- inFile.close();
- return resultTxt;
- }
- string encrypt(string text, int s)
- {
- string result = "";
- traverse text
- for (int i=0;i<text.length();i++)
- {
- apply transformation to each character
- Encrypt Uppercase letters
- if (isupper(text[i]))
- result += char(int(text[i]+s-65)%26 +65);
- Encrypt Lowercase letters
- else
- result += char(int(text[i]+s-97)%26 +97);
- }
- Return the resulting string
- return result; }
- int main()
- { string txt = GetFileData();
- cout<<txt<<endl;
- cout<<encrypt(txt,2);
- system("pause");
- return 0;}
- rc4
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define N 256 // 2^8
- void swap(unsigned char *a, unsigned char *b) {
- int tmp = *a;
- *a = *b;
- *b = tmp;
- }
- int KSA(char *key, unsigned char *S) {
- int len = strlen(key);
- int j = 0;
- for(int i = 0; i < N; i++)
- S[i] = i;
- for(int i = 0; i < N; i++) {
- j = (j + S[i] + key[i % len]) % N;
- swap(&S[i], &S[j]);
- }
- return 0;
- }
- int PRGA(unsigned char *S, char *plaintext, unsigned char *ciphertext) {
- int i = 0;
- int j = 0;
- for(size_t n = 0, len = strlen(plaintext); n < len; n++) {
- i = (i + 1) % N;
- j = (j + S[i]) % N;
- swap(&S[i], &S[j]);
- int rnd = S[(S[i] + S[j]) % N];
- ciphertext[n] = rnd ^ plaintext[n];
- }
- return 0;
- }
- int RC4(char *key, char *plaintext, unsigned char *ciphertext) {
- unsigned char S[N];
- KSA(key, S);
- PRGA(S, plaintext, ciphertext);
- return 0;
- }
- int main(int argc, char *argv[]) {
- if(argc < 3) {
- printf("Usage: %s <key> <plaintext>", argv[0]);
- return -1;
- }
- unsigned char *ciphertext = malloc(sizeof(int) * strlen(argv[2]));
- RC4(argv[1], argv[2], ciphertext);
- for(size_t i = 0, len = strlen(argv[2]); i < len; i++)
- printf("%02hhX", ciphertext[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement