Hey all, I am trying to read in an encrypted text file to a string, decrypt and then create 3 arrays out of the csv data in 3 rows. I am using the MBS Encryption module (functions straight from the example to decrypt/encrypt strings). I am able to decrypt/encrypt the files and strings separately but not a string read from file … The decrypt fails on the first step of the Decrypt function below (DecodeBase64) because it returns an empty string. . The string/file is UTF8. I don’t really get how all these different encodings interact … How should I be getting base64encoded text from a utf (or any) encoding?
put together a small reproduction. In it, I am trying to get the value of tStringD to be the decrypted string contained in the textE file that is created. I use that method to decrypt the file and the string decryption part is used in the app with string manipulation after.
Decrypt Function beginning
Function Decrypt (key, text)
dim Input as String = DecodeBase64(text)
if lenb(input) < 8 then
// no salt?
Return ""
end if
..
..
End Function
Decrypt String from File
Dim tStringE As String 'encrypted string
Dim tStringD As String 'decrypted string
Dim tis As TextInputStream
Dim f As FolderItem
f = GetFolderItem("").Parent.Child("testE.txt")
If f <> Nil Then
tis = TextInputStream.Open(f)
tStringE = tis.ReadAll
tStringD = Decrypt(App.Num, tStringE)
break
End
Encrypting/Decrypting File
[code]
Dim fSource As FolderItem
Dim fEncrypted As FolderItem
Dim fDecrypted As FolderItem
fsource = GetFolderItem("").Parent.Child(“test.txt”)
fEncrypted = GetFolderItem("").Parent.Child(“testE.txt”)
fDecrypted = GetFolderItem("").Parent.Child(“TestD.txt”)
If fSource.Exists = False Then
MsgBox “oops”
Else
if EncryptFile(App.Num, fSource, fEncrypted) then
if Decryptfile(App.Num, fEncrypted, fDecrypted) then
MsgBox “OK”
else
MsgBox “Failed to decrypt.”
end if
else
MsgBox “Failed to encrypt.”
end if
End[/code]
Full Decrypt Function
Function Decrypt (key, text)
// AES 256 plus good key generation
// returns Base64, so you can store in text field in database
dim Input as String = DecodeBase64(text)
if lenb(input) < 8 then
// no salt?
Return ""
break
end if
key = ConvertEncoding(key, encodings.UTF8)
dim iKey as MemoryBlock = key
dim salt as MemoryBlock = leftb(input, 8)
const RoundNumbers = 1000
dim CKey as MemoryBlock
dim CIV as MemoryBlock
if CipherMBS.BytesToKey(CipherMBS.aes_256_cfb128, DigestMBS.SHA512, salt, key, RoundNumbers, CKey, CIV) then
// last 64 bytes are hash
dim CheckHash as string = input.RightB(64)
Input = Input.midb(9, lenb(input) - 64 - 8)
dim c as CipherMBS = CipherMBS.aes_256_cfb128
call c.DecryptInit Ckey, CIV
dim output as string = c.ProcessString(Input) + c.FinalizeAsString
dim outputHash as string = SHA512MBS.Hash(output)
break
'if StrCompBytesMBS(outputHash, CheckHash) = 0 then
// ok, bytes are same in hash
if encodings.UTF8.IsValidData(output) then
// ok
dim content as string = DefineEncoding(output, encodings.UTF8)
break
Return content
else
// text encoding error? Maybe wrong key?
break
end if
break
'else
'// hash doesn't work, so wrong key!
'break
'end if
else
break
// failed to make key
end if
End function