Advertisement
ComputerMan123

AES

Aug 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. -- Simple API for encrypting strings.
  2. --
  3. AES128 = 16
  4. AES192 = 24
  5. AES256 = 32
  6.  
  7. ECBMODE = 1
  8. CBCMODE = 2
  9. OFBMODE = 3
  10. CFBMODE = 4
  11. CTRMODE = 4
  12.  
  13. local function pwToKey(password, keyLength, iv)
  14. local padLength = keyLength
  15. if (keyLength == AES192) then
  16. padLength = 32
  17. end
  18.  
  19. if (padLength > #password) then
  20. local postfix = ""
  21. for i = 1,padLength - #password do
  22. postfix = postfix .. string.char(0)
  23. end
  24. password = password .. postfix
  25. else
  26. password = string.sub(password, 1, padLength)
  27. end
  28.  
  29. local pwBytes = {string.byte(password,1,#password)}
  30. password = ciphermode.encryptString(pwBytes, password, ciphermode.encryptCBC, iv)
  31.  
  32. password = string.sub(password, 1, keyLength)
  33.  
  34. return {string.byte(password,1,#password)}
  35. end
  36.  
  37. --
  38. -- Encrypts string data with password password.
  39. -- password - the encryption key is generated from this string
  40. -- data - string to encrypt (must not be too large)
  41. -- keyLength - length of aes key: 128(default), 192 or 256 Bit
  42. -- mode - mode of encryption: ecb, cbc(default), ofb, cfb
  43. --
  44. -- mode and keyLength must be the same for encryption and decryption.
  45. --
  46. function encrypt(password, data, keyLength, mode, iv)
  47. assert(password ~= nil, "Empty password.")
  48. assert(password ~= nil, "Empty data.")
  49.  
  50. local mode = mode or CBCMODE
  51. local keyLength = keyLength or AES128
  52.  
  53. local key = pwToKey(password, keyLength, iv)
  54.  
  55. local paddedData = util.padByteString(data)
  56.  
  57. if mode == ECBMODE then
  58. return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB, iv)
  59. elseif mode == CBCMODE then
  60. return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC, iv)
  61. elseif mode == OFBMODE then
  62. return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB, iv)
  63. elseif mode == CFBMODE then
  64. return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB, iv)
  65. elseif mode == CTRMODE then
  66. return ciphermode.encryptString(key, paddedData, ciphermode.encryptCTR, iv)
  67. else
  68. error("Unknown mode", 2)
  69. end
  70. end
  71.  
  72.  
  73.  
  74.  
  75. --
  76. -- Decrypts string data with password password.
  77. -- password - the decryption key is generated from this string
  78. -- data - string to encrypt
  79. -- keyLength - length of aes key: 128(default), 192 or 256 Bit
  80. -- mode - mode of decryption: ecb, cbc(default), ofb, cfb
  81. --
  82. -- mode and keyLength must be the same for encryption and decryption.
  83. --
  84. function decrypt(password, data, keyLength, mode, iv)
  85. local mode = mode or CBCMODE
  86. local keyLength = keyLength or AES128
  87.  
  88. local key = pwToKey(password, keyLength, iv)
  89.  
  90. local plain
  91. if mode == ECBMODE then
  92. plain = ciphermode.decryptString(key, data, ciphermode.decryptECB, iv)
  93. elseif mode == CBCMODE then
  94. plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC, iv)
  95. elseif mode == OFBMODE then
  96. plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB, iv)
  97. elseif mode == CFBMODE then
  98. plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB, iv)
  99. elseif mode == CTRMODE then
  100. plain = ciphermode.decryptString(key, data, ciphermode.decryptCTR, iv)
  101. else
  102. error("Unknown mode", 2)
  103. end
  104.  
  105. result = util.unpadByteString(plain)
  106.  
  107. if (result == nil) then
  108. return nil
  109. end
  110.  
  111. return result
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement