jencrypt.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JENCRYPT_HPP
  14. #define JENCRYPT_HPP
  15. #include "platform.h"
  16. #include "jiface.hpp"
  17. #include "jbuff.hpp"
  18. #include "jexcept.hpp"
  19. //for AES, keylen must be 16, 24, or 32 Bytes
  20. extern jlib_decl MemoryBuffer &aesEncrypt(const void *key, size_t keylen, const void *input, size_t inlen, MemoryBuffer &output);
  21. extern jlib_decl MemoryBuffer &aesDecrypt(const void *key, size_t keylen, const void *input, size_t inlen, MemoryBuffer &output);
  22. #define encrypt _LogProcessError12
  23. #define decrypt _LogProcessError15
  24. extern jlib_decl void encrypt(StringBuffer &ret, const char *in);
  25. extern jlib_decl void decrypt(StringBuffer &ret, const char *in);
  26. // simple inline block scrambler (shouldn't need jlib_decl)
  27. class Csimplecrypt
  28. {
  29. unsigned *r;
  30. unsigned n;
  31. size32_t blksize;
  32. public:
  33. Csimplecrypt(const byte *key, size32_t keysize, size32_t _blksize)
  34. {
  35. n = (unsigned)(_blksize/sizeof(unsigned));
  36. blksize = (size32_t)(n*sizeof(unsigned));
  37. r = (unsigned *)malloc(blksize);
  38. byte * z = (byte *)r;
  39. size32_t ks = keysize;
  40. const byte *k = key;
  41. for (size32_t i=0;i<blksize;i++) {
  42. z[i] = (byte)(*k+i);
  43. if (--ks==0) {
  44. k = key;
  45. ks = keysize;
  46. }
  47. else
  48. k++;
  49. }
  50. encrypt(z);
  51. encrypt(z);
  52. }
  53. ~Csimplecrypt()
  54. {
  55. free(r);
  56. }
  57. void encrypt( void * buffer )
  58. {
  59. unsigned * w = (unsigned *)buffer;
  60. unsigned i = 0;
  61. while (i<n) {
  62. unsigned mask = r[i];
  63. unsigned j = ((unsigned)mask) % n;
  64. unsigned wi = w[i];
  65. unsigned wj = w[j];
  66. w[j] = ( ( wj & mask ) | ( wi & ~mask ) ) + mask;
  67. w[i] = ( ( wi & mask ) | ( wj & ~mask ) ) + mask;
  68. i++;
  69. }
  70. }
  71. void decrypt( void * buffer )
  72. {
  73. unsigned * w = (unsigned*)buffer;
  74. unsigned i = n;
  75. while (i--) {
  76. unsigned mask = r[i];
  77. unsigned j = ((unsigned)mask) % n;
  78. unsigned wi = w[i] - mask;
  79. unsigned wj = w[j] - mask;
  80. w[i] = ( wi & mask ) | ( wj & ~mask );
  81. w[j] = ( wj & mask ) | ( wi & ~mask );
  82. }
  83. }
  84. };
  85. #endif //JENCRYPT_HPP