With this library you can
- encrypt a file
- decrypt a file
- get the content of a file as a Base64 encoded string (to store it in a database)
- save the content of a Base64 string to a file
En-/Decryption is done with RandomAccessFile. The PW is generated automatically (50 chars using lower-/uppercase letters and numbers). It returns a simple map.
Used libs:
- RAF
- StringUtils
- ByteConverter
- #AdditionalJar: bcprov-jdk15on-150 (expansion for RAF)
It has 4 methods:
EncryptOneFile (FilePath As String, FileName As String, ExportPath As String) As Map
Encrypts the given file. Returns a map with the generated filepassword, the file's path, the filename and the file's content as bytes. ".ENC" is added to the encrypted file.
Return values:
DecryptOneFile (FilePath As String, FileName As String, ExportPath As String, FilePW As String) As Map
Decrypts the given file with the given pw, removes the ".ENC" suffix and writes it to the given path.
Return values:
RealFilename = The filename without the suffix
GetFileContentAsBase64String (FilePath As String, FileName As String) As Map
Gets the content of a file as a Base64 string
Return values:
WriteFileContentFromBase64String (FilePath As String, FileName As String, Base64String) As Map
Saves the content of a Base64 string to a file
Return values:
Error handling:
- check the result (map) for "Error" (file not found or decryption failure due to wrong pw)
Notes:
- It overwrites existing files
- safe the generated pw
- it's a beta version (I've tested it but... you never know)
Example:
Update: Added #AdditionalJar: bcprov-jdk15on-150 to the lib
- encrypt a file
- decrypt a file
- get the content of a file as a Base64 encoded string (to store it in a database)
- save the content of a Base64 string to a file
En-/Decryption is done with RandomAccessFile. The PW is generated automatically (50 chars using lower-/uppercase letters and numbers). It returns a simple map.
Used libs:
- RAF
- StringUtils
- ByteConverter
- #AdditionalJar: bcprov-jdk15on-150 (expansion for RAF)
It has 4 methods:
EncryptOneFile (FilePath As String, FileName As String, ExportPath As String) As Map
Encrypts the given file. Returns a map with the generated filepassword, the file's path, the filename and the file's content as bytes. ".ENC" is added to the encrypted file.
Return values:
B4X:
ReturnValues.Put("EncFilePath", ExportPath)
ReturnValues.Put("EncFileName", FileName&".ENC")
ReturnValues.Put("FilePW", GenPW)
ReturnValues.Put("EncFileContent", FileBuffer)
DecryptOneFile (FilePath As String, FileName As String, ExportPath As String, FilePW As String) As Map
Decrypts the given file with the given pw, removes the ".ENC" suffix and writes it to the given path.
Return values:
B4X:
ReturnValues.Put("FilePath", ExportPath)
ReturnValues.Put("FileName", RealFilename)
RealFilename = The filename without the suffix
GetFileContentAsBase64String (FilePath As String, FileName As String) As Map
Gets the content of a file as a Base64 string
Return values:
B4X:
ReturnValues.Put("FileContentB64", FileString)
WriteFileContentFromBase64String (FilePath As String, FileName As String, Base64String) As Map
Saves the content of a Base64 string to a file
Return values:
B4X:
ReturnValues.Put("OK", "File written")
Error handling:
- check the result (map) for "Error" (file not found or decryption failure due to wrong pw)
Notes:
- It overwrites existing files
- safe the generated pw
- it's a beta version (I've tested it but... you never know)
Example:
B4X:
Dim fe As FileEncrypter
Dim r As Map
Dim FileContent As String
'Encrypt file
r=fe.EncryptOneFile(File.DirApp & "/MyFolder","Test.txt", File.DirApp & "/Export")
'Decrypt file
r=fe.DecryptOneFile(File.DirApp & "/MyFolder","Test.txt.ENC",File.DirApp & "/Export",r.Get("FilePW"))
'Get content as Base64 string
r=fe.GetFileContentAsBase64String(File.DirApp & "/MyFolder","Test.txt")
Dim su As StringUtils
Dim fc64b() As Byte
Dim fc64s As String
fc64s=r.Get("FileContentB64")
'Convert to bytes
fc64b=su.DecodeBase64(fc64s)
'show content
Log (Bconv.StringFromBytes(fc64b,"UTF8"))
'write file from content of Base64 String
fe.WriteFileContentFromBase64String(File.DirApp & "/MyFolder","B64.txt",fc64s)
Update: Added #AdditionalJar: bcprov-jdk15on-150 to the lib
Attachments
Last edited: