Jump to content
Sign in to follow this  
leomoon

Decrypt an Encrypted File Without Generating a New File

Recommended Posts

leomoon

Hello,
 
I'm trying to write a program that will decrypt an encrypted file (_Crypt_EncryptFile was used to encrypt) without generating a new file.
 
I tried to use FileRead(FileOpen($sSourceRead,0),-1) which in theory should read the whole file into a variable then I can use _Crypt_DecryptData to decrypt the variable. But FileRead won't read the whole file. It will stop somewhere in the middle of the first line for some reason.
 
I've tried FileRead, FileReadLine, and FileReadToArray. None of them can read the encrypted file properly.

I've attached the encrypted text. Nothing special. Algorithm: AES256, Password: password

If you try to read this file using the command below, it will stop somewhere in the middle of the first line:

ConsoleWrite(FileRead(FileOpen(@ScriptDir&'\testRead.txt',0),-1)&@CRLF)

Anybody knows what I'm doing wrong? It's a simple FileRead!

testRead.txt

Share this post


Link to post
Share on other sites
Bowmore

Try opening the file in binary mode


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
Nessie

Try this:

#include <Crypt.au3>

$sFilePath = @ScriptDir & "\testRead.txt"
$sKey = "password"

$hOpen = FileOpen($sFilePath, 0)
$sRead = FileRead($hOpen)
FileClose($hOpen)

$sDecript = _Crypt_DecryptData($sRead, $sKey , $CALG_AES_256)

If @error Then
    MsgBox(16, "Error", "Unable to decript the file")
    Exit
EndIf

$hOpen = FileOpen($sFilePath, 2)
FileWrite($hOpen, BinaryToString($sDecript))
FileClose($hOpen)

By the way the next time please use an english test, as not anyone have persian font installed ;)

Hi!

Edited by Nessie
  • Like 1

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites
leomoon

Thanks @Universalist for the sample code. All modern Operating Systems will not need special font to display Unicode correctly. We just have to use with Unicode mode which is 4:

BinaryToString($sDecript, 4)

Same with writing out to a file, we can use mode 128:

FileOpen($sFilePath, 2+128)

But yes I forgot that was Persian. Sorry. :D

Again thanks for the sample code.

#include <Crypt.au3>

$sFilePath = @ScriptDir & "\testRead.txt"
$sKey = "password"

$hOpen = FileOpen($sFilePath, 0)
$sRead = FileRead($hOpen)
FileClose($hOpen)

$sDecript = _Crypt_DecryptData($sRead, $sKey , $CALG_AES_256)

If @error Then
    MsgBox(16, "Error", "Unable to decript the file")
    Exit
EndIf

$hOpen = FileOpen($sFilePath, 2+128)
FileWrite($hOpen, BinaryToString($sDecript, 4))
FileClose($hOpen)
  • Like 1

Share this post


Link to post
Share on other sites
Nessie

Glad to help you ;)

By the way i am Nessie the Loch Ness Monster ;)

Hi!

  • Like 1

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites
anhyeuem

Try this:

#include <Crypt.au3>

$sFilePath = @ScriptDir & "\testRead.txt"
$sKey = "password"

$hOpen = FileOpen($sFilePath, 0)
$sRead = FileRead($hOpen)
FileClose($hOpen)

$sDecript = _Crypt_DecryptData($sRead, $sKey , $CALG_AES_256)

If @error Then
    MsgBox(16, "Error", "Unable to decript the file")
    Exit
EndIf

$hOpen = FileOpen($sFilePath, 2)
FileWrite($hOpen, BinaryToString($sDecript))
FileClose($hOpen)

By the way the next time please use an english test, as not anyone have persian font installed ;)

Hi!

Hello, now i want encrypt a text file like testRead.txt

can you show me and teach me, my friend

Share this post


Link to post
Share on other sites
JLogan3o13

anhyeuem, first off why are you hijacking another thread instead of beginning your own? Secondly, all you're stating is what you want. How about showing what you have tried on your own, and what problems you're having?


√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
anhyeuem

anhyeuem, first off why are you hijacking another thread instead of beginning your own? Secondly, all you're stating is what you want. How about showing what you have tried on your own, and what problems you're having?

Ah, i use this code for encrypt and decrypt:

 

#include <Crypt.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Encrypt text using a generic password.
    Local $sEncrypted = StringEncrypt(True, 'بی شک اگر روزی ۵۰۰ تومان به قیمت اجناس اضافه می‌کردی؛', 'securepassword')

    ; Display the encrypted text.
    MsgBox($MB_SYSTEMMODAL, '', $sEncrypted)

    ; Decrypt the encrypted text using the generic password.
    Local $sDecrypted = StringEncrypt(False, $sEncrypted, 'securepassword')

    ; Display the decrypted text.
    MsgBox($MB_SYSTEMMODAL, '', $sDecrypted)
EndFunc   ;==>Example

Func StringEncrypt($fEncrypt, $sData, $sPassword)
    _Crypt_Startup() ; Start the Crypt library.
    Local $sReturn = ''
    If $fEncrypt Then ; If the flag is set to True then encrypt, otherwise decrypt.
        $sReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_RC4)
    Else
        $sReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_RC4))
    EndIf
    _Crypt_Shutdown() ; Shutdown the Crypt library.
    Return $sReturn
EndFunc   ;==>StringEncrypt

But when decrypt, i can't received: "بی شک اگر روزی ۵۰۰ تومان به قیمت اجناس اضافه می‌کردی؛"

it is: "?? ?? ??? ???? ??? ????? ?? ???? ????? ????? ????????"

Can you teach me or show me?

I want encrypt/decrypt unicode

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×