Jump to content

One Time Pad (Vernam) - Binary Problem


Recommended Posts

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...