Jump to content

CaesarCipher


gamerman2360
 Share

Recommended Posts

MsgBox(0, "Test", _StringShiftCipher("Test", 5)); Returns Yjxy
MsgBox(0, "Test", _StringShiftCipher("Yjxy", 5, 1)); Returns Test
MsgBox(0, "Test", _StringShiftCipher(Chr(255), 5)); Returns [null](0)
MsgBox(0, "Test", _StringShiftCipher(Chr(4), 5, True)); Returns ÿ(255)
Dim $key[4] = [5, 3, 8, 5]
MsgBox(0, "Test", _StringShiftCipher("Test", $key)); Returns Yh{y
MsgBox(0, "Test", _StringShiftCipher("Yh{y", $key, "Yes")); Returns Test

; ===============================================================================
; Function Name:    _StringShiftCipher()
; Description:      Cipher your data
; Parameter(s):     $s_Data     - data to cipher
;                     $i_Key      - number to offset the data
;                               - can be an array to preform a modified caesar cipher
;                   $v_Decrypt  - varient to evaluate to true if this is a decryption
; Requirement(s):   none
; Return Value(s):  On Success - Returns the ciphered string
; Author(s):        gamerman2360
; Note(s):          Made so it can't fail, it just might not return what you want.
; ===============================================================================
Func _StringShiftCipher($s_Data, $i_Key, $v_Decrypt = False)
    If Not IsArray($i_Key) Then
        If $v_Decrypt Then $i_Key *= -1
        $v_Decrypt = $i_Key; reduce, reuse, recycle!
        Dim $i_Key[1] = [$v_Decrypt]; Dim $v[1] = [$v], dosn't work
    ElseIf $v_Decrypt Then; Modified Cipher
        For $i_Iteration = 0 To UBound($i_Key)-1
            $i_Key[$i_Iteration] *= -1
        Next
    EndIf
    $s_Buffer = ""
    For $i_Iteration = 1 To StringLen($s_Data)
        $s_Buffer &= Chr(Mod(Asc(StringMid($s_Data, $i_Iteration, 1))+$i_Key[Mod($i_Iteration-1, UBound($i_Key))]+256, 256))
    Next
    Return $s_Buffer
EndFunc
I kept seeing things like Chr(Asc($data)+1) but that can cause errors so I wanted to make my own function. One thing let to another and eventually I had enough to the point where I decided to just make my own topic. Please post your comments. :whistle:

PS: It didn't look like this at the start, but I wanted to practice changing it to the UDF standard.

BTW: Modified Caesar Cipher is when you use multiple keys on a piece of data.

Edit: Changed the modified part so it started with the 0 key instead of key 1. :P

Also added array examples.

Edit2: Took 'Caesar' out of there when I learned that when a real Caesar Cipher is is done, it only works on alphabetic characters. ;) Going with _StringShiftCipher().

Edited by gamerman2360
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...