当前位置:   article > 正文

跨平台C++ 对称加密解密函数_c++跨平台字符串加密解密

c++跨平台字符串加密解密
  1. void Encrypt(unsigned char* pdata,int len,unsigned long key0,unsigned long key1)
  2. {
  3. union sDWORD
  4. {
  5. unsigned long ldata;
  6. unsigned char b[4];
  7. };
  8. union sWORD
  9. {
  10. short a;
  11. struct
  12. {
  13. unsigned char b0;
  14. unsigned char b1;
  15. }b;
  16. };
  17. union DISPLAY
  18. {
  19. struct{ unsigned char v; }B;
  20. struct {
  21. unsigned char b0 :1; unsigned char b1 :1; unsigned char b2 :1; unsigned char b3 :1; unsigned char b4 :1; unsigned char b5 :1; unsigned char b6 :1; unsigned char b7 :1;
  22. }b;
  23. };
  24. unsigned char *ps=pdata;
  25. int ByteLengthBak=len;
  26. union sDWORD p;
  27. union sDWORD k;
  28. DISPLAY c;
  29. p.ldata=key1;
  30. k.ldata=key0;
  31. char buffer1,buffer2;
  32. unsigned char RBKEY=p.b[0];
  33. unsigned char RBKEY1=p.b[3];
  34. unsigned char RBKEY2=p.b[1];
  35. unsigned char RBKEY3=p.b[2];
  36. RBKEY^=key0;
  37. buffer1=p.b[1];
  38. p.b[1]=k.b[3];
  39. buffer2=p.b[3];
  40. p.b[3]=k.b[1];
  41. k.b[1]=buffer1;
  42. k.b[3]=buffer2;
  43. int count=0;
  44. while(len)
  45. {
  46. if (count==8) count=0;
  47. if(count<4)
  48. {
  49. *(pdata++)^=k.b[count];
  50. }
  51. else
  52. {
  53. *(pdata++)^=p.b[count-4];
  54. }
  55. count++;
  56. len--;
  57. }
  58. for(int ii=0;ii<ByteLengthBak;ii++)
  59. {
  60. ps[ii]=ps[ii]^(RBKEY);
  61. c.B.v=ps[ii];
  62. c.b.b0^=(RBKEY1&0x1);
  63. c.b.b2^=((RBKEY1&0x2)>>1);
  64. c.b.b4^=((RBKEY1&0x4)>>2);
  65. c.b.b6^=((RBKEY1&0x8)>>3);
  66. c.b.b7^=((RBKEY1&0x80)>>7);
  67. c.b.b1^=c.b.b3;
  68. c.b.b3^=c.b.b5;
  69. c.b.b5^=c.b.b7;
  70. c.b.b5^=p.b[0];
  71. c.b.b3^=p.b[1];
  72. c.b.b1^=p.b[2];
  73. c.b.b7^=p.b[3];
  74. c.b.b0^=(RBKEY2&0x1);
  75. c.b.b2^=((RBKEY3&0x4)>>1);
  76. c.b.b4^=((RBKEY2&0x8)>>3);
  77. c.b.b6^=((RBKEY3&0x80)>>7);
  78. }
  79. }
  80. void Encrypt(unsigned char* pdata,int len,string key)
  81. {
  82. CMD5 md5;
  83. md5.GenerateMD5 ((unsigned char*)key.c_str (),key.length ());
  84. Encrypt(pdata,len,md5.m_data[0]^md5.m_data[3],md5.m_data[1]^md5.m_data[2]);
  85. }


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号