Sign in to follow this  
Followers 0
LordBlackout

One Time Pad (Vernam) - Binary Problem

7 posts in this topic

Hi,

I'm working on a File Crypter and I have a problem: I read the file in binary mode but I can't XOR the bytes with the key because they're displayed in HEX. How can I display the file's data in true binary, made of 1 and 0?

Thank you very much,

Lord_Blackout

Share this post


Link to post
Share on other sites



Yes but I need to read the file obtaining a string made of 0 and 1... in your example the variable type is "Binary", but it isn't displayed as 0 and 1..

Share this post


Link to post
Share on other sites

Thank you very much ^^

Share this post


Link to post
Share on other sites

Try this:

#include <Constants.au3>
Global Const $sInFile = @ScriptDir & "test.txt"
Global Const $sOutFile = @ScriptDir & "Test.crypt"

CryptFile($sInFile, $sOutFile)

MsgBox(0, "Encrypt", FileRead($sOutFile))
MsgBox(0, "Decrypt", CryptFile($sOutFile,  ""))

Func CryptFile($sInFile, $sOutFile, $XOR = 0xFF) ;coded by UEZ 2011
    Local $hFile = FileOpen($sInFile, 16)
    If $hFile = -1 Then Return SetError(1, @error, 0)
    Local $i = 0
    If Not FileSetPos($hFile, $i, $FILE_BEGIN) Then Return SetError(2, @error, 0)
    Local $crypt
    While 1
        $char = FileRead($hFile, 1)
        If @error = -1 Then ExitLoop
        $crypt &= Hex(BitXOR($char, $XOR), 2)
        $i += 1
        FileSetPos($hFile, $i, $FILE_BEGIN)
    WEnd
    FileClose($hFile)
    If $sOutFile = "" Then Return BinaryToString("0x" & $crypt)
    $hFile = FileOpen($sOutFile, 18)
    If $hFile = -1 Then Return SetError(3, @error, 0)
    FileWrite($hFile, Binary("0x" & $crypt))
    FileClose($hFile)
EndFunc

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

It is possible to use BitXOR with binary data :D

$XOR = 0xFFFFFFFF
$sFile = @ScriptDir & "test.txt"
$sOutFile = $sFile & ".xored"
$hFile = FileOpen($sFile, 16)
$bOutput = Binary("")
While 1

    $bRead = FileRead($hFile, 4)
    If @error Then
        ExitLoop
    ElseIf BinaryLen($bRead) < 4 Then
        $bOutput &= BinaryMid(Binary(BitXOR($bRead, $XOR)), 1, BinaryLen($bRead))
    Else
        $bOutput &= Binary(BitXOR($bRead, $XOR))
    EndIf
WEnd
FileClose($hFile)

$hFile = FileOpen($sOutFile, 18)
FileWrite($hFile, $bOutput)
FileClose($hFile)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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  
Followers 0