Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. using namespace std;
  5.  
  6. char rot(unsigned char c, int rot) { // detta är rotationsfunktionen. kan ta negativa eller positiva värden
  7. cout << "this is value of c: " << c << '\n';
  8. if (isalpha(c)) {
  9. if (islower(c)) {
  10. if (rot > 0) {
  11. if ((c + rot) > 122) { // om man "slår över" alltså att rotationen + värdet av c är över den tillåtna gränsen
  12. c += rot;
  13. c = c - 122; // gör -122 för att negera överslaget
  14. c += 97; // lägg till värdet som är början på lilla alfabetet
  15. cout << "c after rotation: " << c << '\n';
  16. }
  17. else {
  18. c += rot; // annars rotera vanligt
  19. cout << "c after rotation: " << c << '\n';
  20. }
  21. }
  22. if (rot < 0) { // om den är negativ
  23. if ((c + rot) < 97) { // om det går under alfabetet
  24. rot += (c - 97); // ta bort skillnaden från rotationen tills man kommer till början
  25. c = 122 + rot; //sista bokstaven minus den rotation som e kvar
  26. cout << "c after rotation: " << c << '\n';
  27. }
  28. else {
  29. c += rot; //annars rotera vanligt
  30. cout << "c after rotation: " << c << '\n';
  31. }
  32. }
  33. }
  34. //samma för stor bokstav fast med andra värden
  35. if (isupper(c)) {
  36. if (rot > 0) {
  37. if ((c + rot) > 90) {
  38. c += rot;
  39. c = c - 90;
  40. c += 65;
  41. }
  42. else {
  43. c += rot;
  44. }
  45. }
  46. if (rot < 0) {
  47. if ((c + rot) < 65) {
  48. rot += (c - 65);
  49. c = 90 + rot;
  50. }
  51. else {
  52. c += rot;
  53. }
  54. }
  55.  
  56. }
  57. }
  58. c = c % 256;
  59. return c;
  60. }
  61.  
  62. int main() {
  63.  
  64. string inputstring;
  65. string krypteringsstring;
  66. string dekrypteradstring;
  67. cout << "insert string to encrypt: ";
  68. getline(cin, inputstring);
  69.  
  70. int rotation = 13;
  71. for (int i = 0; i < inputstring.length(); i++) {
  72. if (i % 5 == 0 && i != 0) { //byt var femte
  73. if (i % 10 == 0) { // byt till 13 varannan
  74. rotation = 13;
  75. }
  76. else { //annars byt till 7
  77. rotation = 7;
  78. }
  79. }
  80. krypteringsstring += rot(inputstring.at(i), rotation);
  81. }
  82. //samma fast andra hållet
  83. cout << krypteringsstring << '\n';
  84. rotation = -13;
  85. for (int i = 0; i < krypteringsstring.length(); i++) {
  86. if (i % 5 == 0 && i != 0) {
  87. if (i % 10 == 0) {
  88. rotation = -13;
  89. }
  90. else {
  91. rotation = -7;
  92. }
  93. }
  94. dekrypteradstring += rot(krypteringsstring.at(i), rotation);
  95. }
  96. cout << dekrypteradstring;
  97. cin.get();
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement