Crypt.cpp
Upload User: bjvcxy
Upload Date: 2021-05-06
Package Size: 2054k
Code Size: 2k
Development Platform:

Visual C++

  1. // Crypt.cpp: implementation of the CCrypt class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Attendance.h"
  6. #include "Crypt.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. // 常量
  13. #define C1 52845
  14. #define C2 22719
  15. //////////////////////////////////////////////////////////////////////
  16. // Construction/Destruction
  17. //////////////////////////////////////////////////////////////////////
  18. CCrypt::CCrypt()
  19. {
  20. }
  21. CCrypt::~CCrypt()
  22. {
  23. }
  24. CString CCrypt::Encrypt(CString S, WORD Key) // 加密函数
  25. {
  26. CString Result,str;
  27. int i,j;
  28. Result=S; // 初始化结果字符串
  29. for(i=0; i<S.GetLength(); i++) // 依次对字符串中各字符进行操作
  30. {
  31. Result.SetAt(i, S.GetAt(i)^(Key>>8)); // 将密钥移位后与字符异或
  32. Key = ((BYTE)Result.GetAt(i)+Key)*C1+C2; // 产生下一个密钥
  33. }
  34. S=Result; // 保存结果
  35. Result.Empty(); // 清除结果
  36. for(i=0; i<S.GetLength(); i++) // 对加密结果进行转换
  37. {
  38. j=(BYTE)S.GetAt(i); // 提取字符
  39. // 将字符转换为两个字母保存
  40. str="12"; // 设置str长度为2
  41. str.SetAt(0, 65+j/26);
  42. str.SetAt(1, 65+j%26);
  43. Result += str;
  44. }
  45. return Result;
  46. }
  47. CString CCrypt::Decrypt(CString S, WORD Key) // 解密函数
  48. {
  49. CString Result,str;
  50. int i,j;
  51. Result.Empty(); // 清楚结果
  52. for(i=0; i < S.GetLength()/2; i++) // 将字符串两个字母一组进行处理
  53. {
  54. j = ((BYTE)S.GetAt(2*i)-65)*26;
  55. j += (BYTE)S.GetAt(2*i+1)-65;
  56. str="1"; // 设置str长度为1
  57. str.SetAt(0, j);
  58. Result+=str; // 追加字符,还原字符串
  59. }
  60. S=Result; // 保存中间结果
  61. for(i=0; i<S.GetLength(); i++) // 依次对字符串中各字符进行操作
  62. {
  63. Result.SetAt(i, (BYTE)S.GetAt(i)^(Key>>8)); // 将密钥移位后与字符异或
  64. Key = ((BYTE)S.GetAt(i)+Key)*C1+C2; // 产生下一个密钥
  65. }
  66. return Result;
  67. }