Jump to content

Modified Vigenere cipher


bluebearr
 Share

Recommended Posts

I like RC4, but I needed a way to encrypt user credentials so that they could also be encrypted and decrypted using Wise Installation System. If you've used Wise you know that its math skills are like those of AutoIt 2.x, so it had to be pretty simple. (I'm so thankful for AutoIt 3!)

I wrote the following as Good Enough Encryption for data stored in the registry over several reboots and then deleted. It is a modified Vigenere cipher - modified, because it uses a non-standard alphabet. I have analogous code in Wise so that I can use whatever tool is best suited for the task at hand.

There two straightforward ways to obfuscate this from what is posted if you use it:

1) Pick a different character of the key to use to mix the alphabet;

2) Change the value of $Base in the MixAlpha function.

Thanks for looking!

BlueBearr

#include <string.au3>

$StandardAlphabet = ' !"' & "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
  & "[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; & @CRLF & @TAB
$myText = "One definition of a star is someone you can't bring yourself to hate " _
  & "even while watching his or her dimwitted vanity projects. Miss Congeniality 2: " _
  & "Armed and Fabulous (Warner Bros.) is enjoyable in patches, but only because of " _
  & "the goodwill that most of us still have toward Sandra Bullock. " & @CRLF & @TAB _
  & "It doesn't matter if she overdoes the snorty laugh and tries too hard to look adorably klutzy."
  
$myKey = 'H2645352FSDER343DGDGET36swhgekskdhejsG845J84HDFHD734HJKJNCVJF84'
  
msgbox(0, "Test", "Regular Alphabet:" & @CRLF & "   " & $StandardAlphabet & @CRLF & @CRLF _
  & "Mixed Up Alphabet:" & @CRLF & "   " & MixAlpha($StandardAlphabet, StringInStr( $StandardAlphabet, "H")) )
msgbox(0, "Test", "Text to encrypt:" & @CR & $myText)
$eText = VCode('encrypt', $myText, $myKey, 'Yes')
msgbox(0, "Test", "Encrypted:" & @CRLF & $eText)
$dText = VCode('decrypt', $eText, $myKey, 'Yes')
msgbox(0, "Test", "Decrypted:" & @CRLF & $dText)


Func VCode($direction, $text, $key, $inHex)
  #comments-start
  ===============================================================================
   Encrypt/Decrypt text using a modified Vigenere Cipher
    See, for example, http://raphael.math.uic.edu/~jeremy/crypt/vignere.html
    The advantage of this algorithm is that there is a matching
    encryption/decryption Custom Action for Wise Installation System
  
   $direction : E[cnryption] or D[ecryption]
   $text :  Text to encryption or decrypt
   $key :   key used to encrypt text.  Must match key used to decrypt text.
            Function cannot validate if key is correct, it just uses it.
   $inhex : Y[es] or N[o]
            Encryption = Should the result be converted to hex values?
            Decryption = Is $text in hex?
            Since tabs, carriage returns and linefeeds are NOT encrypted 
            or decrypted, converting to hex is useful if your goal is to 
            store the encrypted text in a REG_SZ value.
   Author:  BlueBearr
  ===============================================================================
  #comments-end
 ; Mix up the alphabet, based on the first letter of the key
  $StandardAlphabet = ' !"' & "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
  & "[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; & @CRLF & @TAB
  $mixseed = StringInStr( $StandardAlphabet, StringLeft($key, 1), 1 )
  $alphabet = MixAlpha($StandardAlphabet, $mixseed)
  $alphabet = $alphabet & $alphabet
 ; Translate each letter of the key into a number
  $keyA = StringSplit($key, "")
  For $i = 1 to $keyA[0]
    $keyA[$i] = StringInStr( $alphabet, $keyA[$i], 1 )
  Next
 ; Check if we need to "dehex"
  If StringLeft($direction, 1) = 'd' And StringLeft($Inhex, 1) = "Y" Then 
    $text = _HexToString($Text)
  EndIf
  $keyPos = 0
  $VCode = ""
 ; Encrypt/decrypt the text
  For $j = 1 to StringLen($text)
   ; Next number from key
    $keyPos = $keyPos + 1
    If $keyPos > $keyA[0] Then $keyPos = 1
    $CurrChar = StringMid( $text, $j, 1)
    If StringInStr( $alphabet, $CurrChar, 1 ) = 0 Then
     ; Char not in alphabet, pass through to string
      $VCode = $VCode & $CurrChar
    Else
      If StringLeft($direction, 1) = 'e' Then
       ;Encrypt
        $Vcode = $VCode & StringMid($alphabet, StringInStr($alphabet, $CurrChar, 1) + $keyA[$keyPos], 1)
      Else
       ;Decrypt
        $Vcode = $VCode & StringMid($alphabet, StringInStr($alphabet, $CurrChar, 1, - 1) - $keyA[$keyPos], 1)
      EndIf
    EndIf
  Next
  If StringLeft($direction, 1) = 'e' And StringLeft($Inhex, 1) = "Y" Then 
    $Vcode = _StringToHex($Vcode)
  EndIf
  Return $VCode
EndFunc

Func MixAlpha($alphabet, $seed)
 ; Mix up the alphabet in a reproducible manner
  $Base = 729316
  $MixNum = $Base * $seed
  $MixNumA = StringSplit($MixNum, "")
  $ALen = stringlen($alphabet)
  $newA = ""
  $pos = 0
  For $i = 1 To $ALen
    $pos = $pos + 1
    If $pos > $MixNumA[0] Then $pos = 1 ;Loop thru the MixNum
    $ACurrLen = StringLen($alphabet)
   ; Use the MixNum to get the character x-tenths into the alphabet
    $index = Int(($MixNumA[$pos] * $ACurrLen) / 10)
    If $index = 0 then $index = int( $ACurrLen / 2)
    If $index = 0 then $index = 1
    $AlphaLeft = StringLeft($alphabet, $index)
    $AlphaRight = StringRight($alphabet, $ACurrLen - $index)
    $newA = $newA & StringRight($AlphaLeft, 1)
    $AlphaLeft = StringLeft($AlphaLeft, StringLen($AlphaLeft) -1)
    $alphabet = $AlphaLeft & $AlphaRight
  Next
  Return $newA
EndFunc
BlueBearrOddly enough, this is what I do for fun.
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...