+ Reply to Thread
Results 1 to 3 of 3

Vigenere cipher, can encrypt - decryption is troublesome!

Hybrid View

  1. #1
    Registered User
    Join Date
    10-16-2012
    Location
    earth
    MS-Off Ver
    Excel 2003
    Posts
    6

    Vigenere cipher, can encrypt - decryption is troublesome!

    hi there, i'm currently working on a spreadsheet that can encrypt and decrypt plaintext, using the vigenere method.

    I can encrypt the text successfully as you can see in the attached worksheet, however i'm having trouble with the decryption. I know the maths formula to work it out is the following, but I just can't figure this out in excel.

    Ma = Ca – Kb (mod 26)
    (Where C = Code, M = Message, K = Key, and where a = the ath character of the message bounded by the message, and b is the bth character of the Key bounded by the length of the key

    I don't think i'm too far off, any help would be appreciated
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Vigenere cipher, can encrypt - decryption is troublesome!

    How about a UDF?

          ----A---- -----B------ --------------C---------------
      1         Key LEMON        B1: Input                     
      2   Plaintext ATTACKATDAWN B2: Input                     
      3      Cypher LXFOPVEFRNHR B3: =Vigenere(B2, $B$1, TRUE) 
      4   Plaintext ATTACKATDAWN B4: =Vigenere(B3, $B$1, FALSE)

    Function Vigenere(ByVal sInp As String, _
                      ByVal sKey As String, _
                      Optional bEncrypt As Boolean = True) As String
    
        Dim i         As Long
        Dim j         As Long
    
        sInp = UCase(Replace(sInp, " ", ""))
        sKey = Replace(sKey, " ", "")
        sKey = Left(WorksheetFunction.Rept(sKey, Len(sInp) \ Len(sKey) + 1), Len(sInp))
    
        If bEncrypt Then
            For i = 1 To Len(sInp)
                j = Asc(Mid(sInp, i, 1)) + Asc(Mid(sKey, i, 1)) - 130
                If j > 25 Then j = j - 26
                Mid(sInp, i) = Chr(j + 65)
            Next i
        Else
            For i = 1 To Len(sInp)
                j = Asc(Mid(sInp, i, 1)) - Asc(Mid(sKey, i, 1))
                If j < 0 Then j = j + 26
                Mid(sInp, i) = Chr(j + 65)
            Next i
        End If
    
        Vigenere = sInp
    End Function
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-16-2012
    Location
    earth
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Vigenere cipher, can encrypt - decryption is troublesome!

    Thanks! this is great, i've never actually used this user defined function before, makes it a lot less cluttered

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1