1. #include<bits/stdc++.h>
    2. using namespace std;
    3. static string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    4. //双表代替加密
    5. //第一种加密
    6. int first_change(char& letter)
    7. {
    8. int location;
    9. string key1 = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    10. //string alphabet = "abcdefghijklmnopqrstuvwxyz";
    11. if (letter != ' ')
    12. {
    13. location = alphabet.find_first_of(letter);
    14. letter = key1[location];
    15. }
    16. return 1;
    17. }
    18. //第二种加密
    19. int second_change(char& letter)
    20. {
    21. int location;
    22. string key2 = "lkjhgfdsaqwertyuiopmnbzxcvLKJHGFDSAQWERTYUIOPMNBZXCV";
    23. //string alphabet = "abcdefghijklmnopqrstuvwxyz";
    24. if (letter != ' ')
    25. {
    26. location = alphabet.find_first_of(letter);
    27. letter = key2[location];
    28. }
    29. return 1;
    30. }
    31. //加密时:判断该字母第几次出现在句子当中
    32. int judeg_encryption(const char letter)
    33. {
    34. static map<char, int> letter_nums
    35. {
    36. {'a',0},{'b',0},{'c',0},{'d',0},{'e',0},{'f',0},
    37. {'g',0},{'h',0},{'i',0},{'g',0},{'k',0},{'l',0},
    38. {'m',0},{'n',0},{'o',0},{'p',0},{'q',0},{'r',0},
    39. {'s',0},{'t',0},{'u',0},{'v',0},{'w',0},{'x',0},
    40. {'y',0},{'z',0},
    41. {'A',0},{'B',0},{'C',0},{'D',0},{'E',0},{'F',0},
    42. {'G',0},{'H',0},{'I',0},{'G',0},{'K',0},{'L',0},
    43. {'M',0},{'N',0},{'O',0},{'P',0},{'Q',0},{'R',0},
    44. {'S',0},{'T',0},{'U',0},{'V',0},{'W',0},{'X',0},
    45. {'Y',0},{'Z',0}
    46. };
    47. if (letter != ' ')
    48. {
    49. letter_nums[letter]++;
    50. }
    51. return letter_nums[letter] % 2;
    52. //若返回值为1,对应第一次加密方法
    53. //若返回值为0,对应第二次加密方法
    54. }
    55. //加密
    56. string encryption(string& sentence, vector<int>& record)
    57. {
    58. char letter;
    59. int option; //接受选择判断函数的返回值
    60. for (int i = 0; i < sentence.length(); i++)
    61. {
    62. if (sentence[i] != ' ')
    63. {
    64. letter = sentence[i];
    65. option = judeg_encryption(letter);
    66. if (option == 1)
    67. {
    68. first_change(letter);
    69. sentence[i] = letter;
    70. record.push_back(1);
    71. }
    72. if (option == 0)
    73. {
    74. second_change(letter);
    75. sentence[i] = letter;
    76. record.push_back(0);
    77. }
    78. }
    79. }
    80. return sentence;
    81. }
    82. //第一种解密
    83. int first_decode(char& letter)
    84. {
    85. int location;
    86. string key1 = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    87. //string alphabet = "abcdefghijklmnopqrstuvwxyz";
    88. if (letter != ' ')
    89. {
    90. location = key1.find_first_of(letter);
    91. letter = alphabet[location];
    92. }
    93. return 1;
    94. }
    95. //第二种解密
    96. int second_decode(char& letter)
    97. {
    98. int location;
    99. string key2 = "lkjhgfdsaqwertyuiopmnbzxcvLKJHGFDSAQWERTYUIOPMNBZXCV";
    100. //string alphabet = "abcdefghijklmnopqrstuvwxyz";
    101. if (letter != ' ')
    102. {
    103. location = key2.find_first_of(letter);
    104. letter = alphabet[location];
    105. }
    106. return 1;
    107. }
    108. string decode(string& sentence, const vector<int> record)
    109. {
    110. char letter;
    111. int j = 0;
    112. for (int i = 0; i < sentence.length(); i++)
    113. {
    114. if (sentence[i] != ' ')
    115. {
    116. if (record[j] == 1)
    117. {
    118. letter = sentence[i];
    119. first_decode(letter);
    120. sentence[i] = letter;
    121. }
    122. if (record[j] == 0)
    123. {
    124. letter = sentence[i];
    125. second_decode(letter);
    126. sentence[i] = letter;
    127. }
    128. j++;
    129. }
    130. }
    131. return sentence;
    132. }
    133. int main()
    134. {
    135. static vector<int> record;
    136. string sentence;
    137. cout << "please input what you want to encrypt:" << endl;
    138. getline(cin, sentence);
    139. cout << "加密后" << endl;
    140. cout << encryption(sentence, record) << endl;
    141. cout << "解密后" << endl;
    142. cout << decode(sentence, record);
    143. }