ecb.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. local Array = require("lockbox.util.array");
  2. local Stream = require("lockbox.util.stream");
  3. local Queue = require("lockbox.util.queue");
  4. local ECB = {};
  5. ECB.Cipher = function()
  6. local public = {};
  7. local key;
  8. local blockCipher;
  9. local padding;
  10. local inputQueue;
  11. local outputQueue;
  12. public.setKey = function(keyBytes)
  13. key = keyBytes;
  14. return public;
  15. end
  16. public.setBlockCipher = function(cipher)
  17. blockCipher = cipher;
  18. return public;
  19. end
  20. public.setPadding = function(paddingMode)
  21. padding = paddingMode;
  22. return public;
  23. end
  24. public.init = function()
  25. inputQueue = Queue();
  26. outputQueue = Queue();
  27. return public;
  28. end
  29. public.update = function(messageStream)
  30. local byte = messageStream();
  31. while (byte ~= nil) do
  32. inputQueue.push(byte);
  33. if(inputQueue.size() >= blockCipher.blockSize) then
  34. local block = Array.readFromQueue(inputQueue, blockCipher.blockSize);
  35. block = blockCipher.encrypt(key, block);
  36. Array.writeToQueue(outputQueue, block);
  37. end
  38. byte = messageStream();
  39. end
  40. return public;
  41. end
  42. public.finish = function()
  43. local paddingStream = padding(blockCipher.blockSize, inputQueue.getHead());
  44. public.update(paddingStream);
  45. return public;
  46. end
  47. public.getOutputQueue = function()
  48. return outputQueue;
  49. end
  50. public.asHex = function()
  51. return Stream.toHex(outputQueue.pop);
  52. end
  53. public.asBytes = function()
  54. return Stream.toArray(outputQueue.pop);
  55. end
  56. return public;
  57. end
  58. ECB.Decipher = function()
  59. local public = {};
  60. local key;
  61. local blockCipher;
  62. local padding;
  63. local inputQueue;
  64. local outputQueue;
  65. public.setKey = function(keyBytes)
  66. key = keyBytes;
  67. return public;
  68. end
  69. public.setBlockCipher = function(cipher)
  70. blockCipher = cipher;
  71. return public;
  72. end
  73. public.setPadding = function(paddingMode)
  74. padding = paddingMode;
  75. return public;
  76. end
  77. public.init = function()
  78. inputQueue = Queue();
  79. outputQueue = Queue();
  80. return public;
  81. end
  82. public.update = function(messageStream)
  83. local byte = messageStream();
  84. while (byte ~= nil) do
  85. inputQueue.push(byte);
  86. if(inputQueue.size() >= blockCipher.blockSize) then
  87. local block = Array.readFromQueue(inputQueue, blockCipher.blockSize);
  88. block = blockCipher.decrypt(key, block);
  89. Array.writeToQueue(outputQueue, block);
  90. end
  91. byte = messageStream();
  92. end
  93. return public;
  94. end
  95. public.finish = function()
  96. -- local paddingStream = padding(blockCipher.blockSize, inputQueue.getHead());
  97. -- public.update(paddingStream);
  98. -----------------------------------------------------
  99. local data=Stream.toArray(outputQueue.pop)
  100. local paddingByte=data[#data]
  101. local realLength=#data-paddingByte--如果有padding,计算去除padding后的长度
  102. local padded=true
  103. for i=#data,realLength+1,-1 do
  104. if(data[i]~=paddingByte) then
  105. padded=false
  106. end
  107. end
  108. print("realLength is "..realLength)
  109. local paddedBytes=Array.slice(data,1,realLength)
  110. if padded then
  111. Array.writeToQueue(outputQueue,paddedBytes)
  112. end
  113. ---------------------------------------------------
  114. return public;
  115. end
  116. public.getOutputQueue = function()
  117. return outputQueue;
  118. end
  119. public.asHex = function()
  120. return Stream.toHex(outputQueue.pop);
  121. end
  122. public.asBytes = function()
  123. return Stream.toArray(outputQueue.pop);
  124. end
  125. return public;
  126. end
  127. return ECB;