Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdio.h>
- using namespace std;
- char rot(unsigned char c, int rot) { // detta är rotationsfunktionen. kan ta negativa eller positiva värden
- cout << "this is value of c: " << c << '\n';
- if (isalpha(c)) {
- if (islower(c)) {
- if (rot > 0) {
- if ((c + rot) > 122) { // om man "slår över" alltså att rotationen + värdet av c är över den tillåtna gränsen
- c += rot;
- c = c - 122; // gör -122 för att negera överslaget
- c += 97; // lägg till värdet som är början på lilla alfabetet
- cout << "c after rotation: " << c << '\n';
- }
- else {
- c += rot; // annars rotera vanligt
- cout << "c after rotation: " << c << '\n';
- }
- }
- if (rot < 0) { // om den är negativ
- if ((c + rot) < 97) { // om det går under alfabetet
- rot += (c - 97); // ta bort skillnaden från rotationen tills man kommer till början
- c = 122 + rot; //sista bokstaven minus den rotation som e kvar
- cout << "c after rotation: " << c << '\n';
- }
- else {
- c += rot; //annars rotera vanligt
- cout << "c after rotation: " << c << '\n';
- }
- }
- }
- //samma för stor bokstav fast med andra värden
- if (isupper(c)) {
- if (rot > 0) {
- if ((c + rot) > 90) {
- c += rot;
- c = c - 90;
- c += 65;
- }
- else {
- c += rot;
- }
- }
- if (rot < 0) {
- if ((c + rot) < 65) {
- rot += (c - 65);
- c = 90 + rot;
- }
- else {
- c += rot;
- }
- }
- }
- }
- c = c % 256;
- return c;
- }
- int main() {
- string inputstring;
- string krypteringsstring;
- string dekrypteradstring;
- cout << "insert string to encrypt: ";
- getline(cin, inputstring);
- int rotation = 13;
- for (int i = 0; i < inputstring.length(); i++) {
- if (i % 5 == 0 && i != 0) { //byt var femte
- if (i % 10 == 0) { // byt till 13 varannan
- rotation = 13;
- }
- else { //annars byt till 7
- rotation = 7;
- }
- }
- krypteringsstring += rot(inputstring.at(i), rotation);
- }
- //samma fast andra hållet
- cout << krypteringsstring << '\n';
- rotation = -13;
- for (int i = 0; i < krypteringsstring.length(); i++) {
- if (i % 5 == 0 && i != 0) {
- if (i % 10 == 0) {
- rotation = -13;
- }
- else {
- rotation = -7;
- }
- }
- dekrypteradstring += rot(krypteringsstring.at(i), rotation);
- }
- cout << dekrypteradstring;
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement