Skip to content

Commit

Permalink
Improved Nando-Crypt panel! Updated screenshot in README.md
Browse files Browse the repository at this point in the history
Version bumped to 2.0
  • Loading branch information
Fernando-A-Rocha committed Apr 15, 2022
1 parent c2b5431 commit e3d81c7
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 44 deletions.
Binary file modified .github/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 76 additions & 9 deletions nando_crypt/c_encrypt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local thisResName = getResourceName(thisRes)
local SW, SH = guiGetScreenSize()
local window, window2

local savedFileName, secretKey
local savedFileName, secretKey, savedFileList

function openMenu(version)
closeMenu()
Expand All @@ -28,9 +28,17 @@ function openMenu(version)
outputChatBox("Loaded last used secret key from localPlayer element data", 180,180,180)
end
end
if savedFileList == nil then
local data = getElementData(localPlayer, thisResName..":savedFileList")
if data then
savedFileList = data
outputChatBox("Loaded file list from localPlayer element data", 180,180,180)
end
end

local WW, WH = 500, 205
local WW, WH = SW * 0.5, SH * 0.8
window = guiCreateWindow(SW/2 - WW/2, SH/2 - WH/2, WW, WH, "NandoCrypt ("..version..")", false)
guiSetAlpha(window, 0.9)

local close = guiCreateButton(10, WH-40, WW-20, 30, "Close", false, window)

Expand All @@ -43,23 +51,39 @@ function openMenu(version)
guiSetProperty(setkey, "NormalTextColour", "FF00FF00")
end

local decrypt = guiCreateButton(10, WH-110, WW-20, 30, "Decrypt File (Test)", false, window)
local decrypt = guiCreateButton(10, WH-110, WW-20, 30, "Decrypt Files (Test)", false, window)
if (secretKey ~= nil) then
guiSetProperty(decrypt, "NormalTextColour", "ff6373ff")
else
guiSetEnabled(decrypt, false)
guiSetProperty(decrypt, "NormalTextColour", "FFFF0000")
end

local encrypt = guiCreateButton(10, WH-145, WW-20, 30, "Encrypt File", false, window)
local encrypt = guiCreateButton(10, WH-145, WW-20, 30, "Encrypt Files", false, window)
if (secretKey ~= nil) then
guiSetProperty(encrypt, "NormalTextColour", "FFFFFFFF")
else
guiSetEnabled(encrypt, false)
guiSetProperty(encrypt, "NormalTextColour", "FFFF0000")
end

local file_edit = guiCreateEdit(10, WH-180, WW-20, 30, savedFileName, false, window)
local addFile = guiCreateButton(10, WH-180, (WW/2)-10, 30, "Add File", false, window)
if (secretKey ~= nil) then
guiSetProperty(addFile, "NormalTextColour", "FF00FF00")
else
guiSetEnabled(addFile, false)
guiSetProperty(addFile, "NormalTextColour", "FFFF0000")
end

local removeFile = guiCreateButton((WW/2), WH-180, (WW/2)-10, 30, "Remove File", false, window)
if (secretKey ~= nil) then
guiSetProperty(removeFile, "NormalTextColour", "ffff3098")
else
guiSetProperty(removeFile, "NormalTextColour", "FFFF0000")
end
guiSetEnabled(removeFile, false)

local file_edit = guiCreateEdit(10, WH-215, WW-20, 30, savedFileName, false, window)
if (secretKey ~= nil) then
addEventHandler( "onClientGUIChanged", file_edit,
function (theElement)
Expand All @@ -71,28 +95,65 @@ function openMenu(version)
guiSetEnabled(file_edit, false)
end

local fileList_grid = guiCreateGridList(10, 25, WW-20, WH-250, false, window)
guiGridListAddColumn(fileList_grid, "Files ready to be encrypted", 0.9)

for path, _ in pairs(savedFileList or {}) do
guiGridListAddRow(fileList_grid, path)
end


addEventHandler( "onClientGUIClick", window,
function (button, state, absoluteX, absoluteY)
if button ~= "left" then return end

local row,col = guiGridListGetSelectedItem(fileList_grid)
if row ~= -1 then
guiSetEnabled(addFile, false)
guiSetEnabled(removeFile, true)
else
guiSetEnabled(addFile, true)
guiSetEnabled(removeFile, false)
end

if source == close then
closeMenu()

elseif source == setkey then
openChangeKeyMenu()

elseif source == encrypt or source == decrypt then

elseif source == removeFile then

local filePath = guiGridListGetItemText(fileList_grid, row, 1)
savedFileList[filePath] = nil
setElementData(localPlayer, thisResName..":savedFileList", savedFileList, false)
openMenu(version)

elseif source == addFile then

local filePath = guiGetText(file_edit)
if filePath == "" then
return outputChatBox("You need to enter a valid serverside file path.", 255,25,25)
end

if not savedFileList then savedFileList = {} end
savedFileList[filePath] = true
setElementData(localPlayer, thisResName..":savedFileList", savedFileList, false)
openMenu(version)

elseif source == encrypt or source == decrypt then

if not savedFileList or (table.size(savedFileList) == 0) then
return outputChatBox("You need to add serverside file paths to the list.", 255,25,25)
end

closeMenu()

if source == encrypt then
triggerServerEvent(thisResName..":requestEncryptFile", resourceRoot, filePath, secretKey)
triggerServerEvent(thisResName..":requestEncryptFile", resourceRoot, savedFileList, secretKey)
setElementData(localPlayer, thisResName..":lastUsedSecretKey", secretKey, false)
elseif source == decrypt then
triggerServerEvent(thisResName..":requestDecryptFile", resourceRoot, filePath)
triggerServerEvent(thisResName..":requestDecryptFile", resourceRoot, savedFileList)
end
end
end)
Expand Down Expand Up @@ -179,4 +240,10 @@ function genRandomKey()
rstring = rstring .. chars:sub(rand, rand)
end
return rstring
end

function table.size(tab)
local length = 0
for _ in pairs(tab) do length = length + 1 end
return length
end
2 changes: 1 addition & 1 deletion nando_crypt/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- DOCUMENTATION:
https://github.com/Fernando-A-Rocha/mta-nandocrypt
-->
<info author="Fernando" name="mta-nandocrypt" description="file encrypter resource" version="1.0.1" type="script" />
<info author="Fernando" name="mta-nandocrypt" description="file encrypter resource" version="2.0" type="script" />

<script src="nando_decrypter" type="server" />
<script src="c_encrypt.lua" type="client" cache="false" />
Expand Down
109 changes: 75 additions & 34 deletions nando_crypt/s_encrypt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ local ENCRYPTED_EXT = ".nandocrypt"

-- Extra security to hide the secret key from people who can access server files
local COMPILE_DERCRYPTER = true
local COMPILE_URL = "https://luac.mtasa.com/?compile=1&debug=0&obfuscate=3"

local waitingEncrypt = {}

function compileCallback(responseData, responseError, fn, player)

Expand Down Expand Up @@ -52,22 +55,26 @@ end
function encryptFile(fpath, secretKey, player)

if not fileExists(fpath) then
return outputChatBox("File doesn't exist: "..fpath, player, 255,25,25)
outputChatBox("File doesn't exist: "..fpath, player, 255,25,25)
return false
end
local ef = fileOpen(fpath)
if not ef then
return outputChatBox("Failed to open: "..fpath, player, 255,25,25)
outputChatBox("Failed to open: "..fpath, player, 255,25,25)
return false
end
local fileContent = fileRead(ef, fileGetSize(ef))
fileClose(ef)
if (not fileContent) or (fileContent == "") then
return outputChatBox("Failed to read or file is empty: "..fpath, player, 255,25,25)
outputChatBox("Failed to read or file is empty: "..fpath, player, 255,25,25)
return false
end

local encoded, iv = encodeString("aes128", fileContent, { key = secretKey })
-- iv depends on data encrypted
if not encoded then
return outputChatBox("Encoding algorithm failed", player, 255,25,25)
outputChatBox("Encoding algorithm failed", player, 255,25,25)
return false
end
local encodedContentHash = md5(encoded)

Expand All @@ -79,41 +86,47 @@ function encryptFile(fpath, secretKey, player)
opened = true
kf = fileOpen(kfn)
if not kf then
return outputChatBox("Failed to open: "..kfn, player, 255,25,25)
outputChatBox("Failed to open: "..kfn, player, 255,25,25)
return false
end
end
if opened then
local kfJson = fileRead(kf, fileGetSize(kf))
fileClose(kf)
if not kfJson then
return outputChatBox("Failed to read: "..kfn, player, 255,25,25)
outputChatBox("Failed to read: "..kfn, player, 255,25,25)
return false
end
if kfJson ~= "" then
ivList = fromJSON(kfJson)
end

if not ivList then
iprint(kfJson)
return outputChatBox("Failed to read IV keys from: "..kfn, player, 255,25,25)
outputChatBox("Failed to read IV keys from: "..kfn, player, 255,25,25)
return false
end
fileDelete(kfn)
end
kf = fileCreate(kfn)
if not kf then
return outputChatBox("Failed to create: "..kfn, player, 255,25,25)
outputChatBox("Failed to create: "..kfn, player, 255,25,25)
return false
end

local efnn = fpath..ENCRYPTED_EXT
if fileExists(efnn) then
local efn = fileOpen(efnn)
if not efn then
return outputChatBox("Failed to open: "..efnn, player, 255,25,25)
outputChatBox("Failed to open: "..efnn, player, 255,25,25)
return false
end
-- delete old useless hash
local efnContent = fileRead(efn, fileGetSize(efn))
fileClose(efn)
if not efnContent then
return outputChatBox("Failed to read: "..efnn, player, 255,25,25)
outputChatBox("Failed to read: "..efnn, player, 255,25,25)
return false
end
local encodedContentHash_old = md5(efnContent)
if ivList[encodedContentHash_old] then
Expand All @@ -124,7 +137,8 @@ function encryptFile(fpath, secretKey, player)
end
local efn = fileCreate(efnn)
if not efn then
return outputChatBox("Failed to open: "..efnn, player, 255,25,25)
outputChatBox("Failed to open: "..efnn, player, 255,25,25)
return false
end
fileWrite(efn, encoded)
fileClose(efn)
Expand All @@ -145,7 +159,8 @@ function encryptFile(fpath, secretKey, player)
end
f = fileCreate(fn)
if not f then
return outputChatBox("Failed to open: "..fn, player, 255,25,25)
outputChatBox("Failed to open: "..fn, player, 255,25,25)
return false
end

local content = string.format(
Expand Down Expand Up @@ -179,21 +194,12 @@ end
fileWrite(f, content)
fileClose(f)


addEncryptLog(
"Encrypted file '"..fpath.."' and obtained hash '"..encodedContentHash.."'. Corresponding IV stored in '"..kfn.."'."
)

-- Skip compilation:
if (not COMPILE_DERCRYPTER) then
outputChatBox("Created '"..fn.."'", player, 25,255,25)
outputChatBox("Restarting "..thisResName.." resource..", player, 187,187,187)
restartResource(thisRes)

else
outputChatBox("Created '"..fn.."', compiling..", player, 25,255,25)
fetchRemote("https://luac.mtasa.com/?compile=1&debug=0&obfuscate=3", compileCallback, content, true, fn, player)
end
outputChatBox("Created '"..fn.."'", player, 25,255,25)
return true
end

function addEncryptLog(msg)
Expand Down Expand Up @@ -230,25 +236,54 @@ function requestMenu(thePlayer, cmd)
end
addCommandHandler("nandocrypt", requestMenu, false, false)

function requestDecryptFile(filePath)
function requestDecryptFile(filePaths)
if type(ncDecrypt) ~= "function" then
return outputChatBox("Decryption function not loaded (check if "..FN_DECRYPTER_SCRIPT.." is valid)", thePlayer, 255,0,0)
end
filePath = filePath..ENCRYPTED_EXT
local worked, reason = ncDecrypt(filePath,
function(data)
outputChatBox("Decryption of '"..filePath.."' worked", thePlayer, 225,255,0)
for filePath,_ in pairs(filePaths) do
filePath = filePath..ENCRYPTED_EXT
local worked, reason = ncDecrypt(filePath,
function(data)
outputChatBox("Decryption of '"..filePath.."' worked", thePlayer, 225,255,0)
end
)
if not worked then
return outputChatBox("Aborting, decryption of '"..filePath.."' failed: "..tostring(reason), thePlayer, 255,0,0)
end
)
if not worked then
return outputChatBox("Decryption of '"..filePath.."' failed: "..tostring(reason), thePlayer, 255,0,0)
end
end
addEvent(thisResName..":requestDecryptFile", true)
addEventHandler(thisResName..":requestDecryptFile", resourceRoot, requestDecryptFile)

function requestEncryptFile(filePath, secretKey)
encryptFile(filePath, secretKey, client)
function requestEncryptFile(filePaths, secretKey)
if table.size(waitingEncrypt) > 0 then
return outputChatBox("One or more files are currently being encrypted, try again later.", client, 255,0,0)
end

for filePath,_ in pairs(filePaths) do
if not encryptFile(filePath, secretKey, client) then
outputChatBox("Aborted encryptions.", client, 255,255,0)
break
end
end

if not COMPILE_DERCRYPTER then
outputChatBox("Restarting "..thisResName.." resource..", client, 187,187,187)
restartResource(thisRes)
else
local fn = FN_DECRYPTER_SCRIPT
local f = fileOpen(fn)
if not f then
return outputChatBox("Failed to open: "..fn, client, 255,25,25)
end
local content = fileRead(f, fileGetSize(f))
fileClose(f)
if not content or content == "" then
return outputChatBox("File is empty: "..fn, client, 255,25,25)
end
outputChatBox("Compiling '"..fn.."'..", client, 75,255,75)
fetchRemote(COMPILE_URL, compileCallback, content, true, fn, client)
end
end
addEvent(thisResName..":requestEncryptFile", true)
addEventHandler(thisResName..":requestEncryptFile", resourceRoot, requestEncryptFile)
Expand All @@ -260,4 +295,10 @@ function (startedResource)

local ver = getResourceInfo(startedResource, "version")
scriptVersion = ((ver and "v"..ver) or "Unknown Version")
end)
end)

function table.size(tab)
local length = 0
for _ in pairs(tab) do length = length + 1 end
return length
end

0 comments on commit e3d81c7

Please sign in to comment.