I have issue with encrypt function

Hello, I want encrypt a string using password.

There a problem when i decrypt my string and set the encrypted data into the DataStoreService.
When i get the encrypted data to decode it, it return a random string.

There is the code:

function encdec(mode,str,password)
	local str = tostring(str)
	local password = tostring(password)
	if mode == "crypt" then
		local cryptstr = ""
		for i=1,#str do
			cryptstr = cryptstr..string.char(string.byte(str,i)+string.byte(password,(i-1)%#password+1))
		end
		return cryptstr
	elseif mode == "decrypt" then
		local decryptstr = ""
		for i=1,#str do
			decryptstr = decryptstr..string.char(string.byte(str,i)-string.byte(password,(i-1)%#password+1))
		end
		return decryptstr
	else return false end
end
--....
local crypted_data = encdec("crypt","print('hello')","Z0AQE80R4DTGSC(E") --Return ʢ���`W�����zl
print(encdec("decrypt",crypted_data,"Z0AQE80R4DTGSC(E")) --Return print('hello')
--My other scripts
--....
--When the player quits
game:GetService("DataStoreService"):GetDataStore("STORAGE"):SetAsync("<my file path>",crypted_data)
--....
--When the player joined
print(encdec("decrypt",game:GetService("DataStoreService"):GetDataStore("STORAGE"):GetAsync("<my file path>"),"Z0AQE80R4DTGSC(E")) --Return pr�nx��k�{i��x��|�z��m��kv')

If I have error with utf-8 characters.How just have a encrypted data contains just utf-8 characters?

My question is, why are you encrypting the data? Who are you protecting it from with this overgrown caesar cipher?
What’s the advantage over storing the plaintext and key and only returning the data if a player supplies the correct key?
(edit: or a hashed password if the password might be anything senstive, read about password hashing in web services)

Back on topic: I think you need to escape any special characters after encrypting and before committing to datastore (e.g. with base64), then decode after loading and before decrypting.
Look for threads on the devforum about data compression libraries for datastores, I remember them finding the most efficient way to dump arbitrary binary data into a datastore with the least amount of overhead.

Basically I want to encrypt the strings because I am creating a LUA based OS editor that you can do whatever you want and release it to a local market.
But the problem is that when OS scripts are public, they can be read by everyone, that’s why I want to encrypt the strings (lines of code). I took caesar encryption type because I can’t find any other encryptor on the forum.