+ Reply to Thread
Results 1 to 8 of 8

Text Encoding

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-12-2007
    Posts
    144

    Text Encoding

    I want to make a very basic text encoder to change a text string to a long number. (Yes I know its not very secure!)

    This is what I have so far but I cant get the VLookup to work properly...

    Sub encode1()
    
    Dim Counter As Integer
    Dim MyString As String
    Dim NewString As String
    Dim NextChar As String
    Dim Key As Integer
    
    MyString = Cells(1, 1)
    Key = inputbox("Enter Key")
    
    For Counter = 1 To Len(MyString)
    
    range1 = Mid(MyString, Counter, 1)
    
    NextChar = Application.VLookup(Range(range1), Range("y1:z95"), 2, 0)
    
    NewString = NewString & NextChar
    
    Next
    
    
    'convert newstring to number?
    'NewString = NewString * Key
    Cells(3, 1) = NewString
    
    End Sub
    The Text to encode is in A1 and the table to lookup is Y1:Z95

    If you have any suggestions of other ways to do it please bear in mind I do need the output to be all numbers, not other odd characters.
    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
    Here's part of it working. Make sure to format A3 as text.

    Sub Encode()
    
        Dim iChr As Integer
        Dim sPlain As String
        Dim sCypher As String
        Dim sNext As String
        Dim Key     As Integer
    
        sPlain = Cells(1, 1)
        Key = inputbox("Enter Key")
    
        For iChr = 1 To Len(sPlain)
            sNext = Application.VLookup(Mid(sPlain, iChr, 1), Range("x1:y95"), 2, 0)
            sCypher = sCypher & sNext
        Next
    
        'convert newstring to number?
        'sCypher = sCypher * Key
        Cells(3, 1) = sCypher
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-12-2007
    Posts
    144
    Cool thanks!

    OK here is a copy of my current work book.

    It kinda works, just has problems with punctuation marks for some reason.

    And I havent been able to implement any kind of key system.

    Any ideas or different approaches?
    Attached Files Attached Files

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Encryption dates back at least to Julius Caesar, who used number substitution for letters. There have been somewhat more sophisticated methods developed since then, to today's DES, Blowfish, RSA, and RijnDael.

    There's lots of info in the web, including various algorithms implemented in Excel.

    A simple scheme (but not as simple as Caesar's) will keep all but your most dedicated enemies in the dark.

  5. #5
    Forum Contributor
    Join Date
    06-12-2007
    Posts
    144
    Theres a cool little excel thing here:

    http://blog.livedoor.jp/andrewe/arch...at_107032.html

    and I have attached my modified versions too.

    Its still REALLY insecure tho...


    I read about hiding your encoded message in an image file, that way people dont even know that there IS a message!
    Attached Files Attached Files
    Last edited by treva26; 10-31-2007 at 08:58 PM.

  6. #6
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267

    Some more...

    For what amounts to a surfeit, the following idyllic Function code, coutesy of Bob Bovey, does the trick:


    Function  EncriptDecript(sdata as String, N as Long)
    Dim bData() as Byte
    
    bData=sData
    For i = LBound(bData) to UBound(bdata)
    bdata(i)=bdata(i) XOR N
    Next
    sData=bData
    
    EncriptDecript=sData
    
    End Function
    
    'To test
    
    Sub Test()
    Dim c as Range
    For each c in ActiveSheet.UsedRange
     y=EncriptDecript(c.value,178)  '-any number within 1-255 will do
    c.value=y
    End Sub
    The first-run ecodes, the second automatically decodes to original. Bob's Code uses Octal representations of byte and transmogrifies them with XOR function.

    Like all encoding/decoding utilities, it is not fool-proof as you require 255 passes in a loop to unlock the key.

    For a very long time now, I have, by continual usage, found my own custom Encript/Decript code fairly reliable, if ponderous. It is again not 100% fail-safe. The codings can use both positive and negative keys, and thereby sidestep tradition. Again, a different key can be used for any one run -to take care of a single encode/decode session.

    Public FirstCel As Range
    Sub AutoENCRIPT()
    Dim num, i%, k$, m$, p$, r$
    Dim c As Range, ct As String
    Dim LastRw As Long
    Dim EffectiveLastcel As Range
    Dim FirstRw As Long
    Dim FirstCol As Integer
    Dim acsheet As Worksheet
    Application.ScreenUpdating = False
    
    'make a spare copy of text on a fresh worksheet for backup
     Set acsheet = ActiveSheet
     Application.DisplayAlerts = False
     On Error Resume Next 'if there a sheet named AutoSpareText
     Worksheets("AutoSpareText").Delete
     Worksheets.Add.Name = "AutoSpareText"
     acsheet.Select
     ActiveSheet.UsedRange.Copy Sheets("AutoSpareText").Range("A1")
     
    'detect if encripting has ever been run
    FirstRw = ActiveSheet.UsedRange.Row
    FirstCol = ActiveSheet.UsedRange.Column
    Set FirstCel = Cells(FirstRw, FirstCol)
    
    If Mid(FirstCel.Value, 1, 2) = Chr(32) & Chr(95) Then MsgBox "Text has already been encripted" & vbCrLf & "Run the Decript code", vbInformation: Exit Sub
    Randomize
    Rnum = Choose(Int(1 + Rnd * 2), Int(1 + Rnd * 29) * -1, Int(1 + Rnd * 134))
    num = InputBox("Enter encripting code: -29 to 134", Default:=Rnum)
    If num = "" Then Exit Sub
    If num > 134 Or num < -29 Then Exit Sub
    'reverse text
    On Error Resume Next
    For Each c In ActiveSheet.UsedRange
     ct = Application.Trim(c)
     For i = Len(ct) To 1 Step -1
     k = k & Mid(ct, i, 1)
     Next
     c.Value = k
     k = ""
    Next c
    'change characters into asci numbers
    For Each c In ActiveSheet.UsedRange
      For i = 1 To Len(c)
      m = m & num + Asc(Mid(c, i, 1)) & Chr(32)
       Next
     c.Value = m
     m = ""
    Next c
    'EncriptTextNumbers()
    For Each c In ActiveSheet.UsedRange
     For i = 1 To Len(c)
      If Mid(c, i, 1) <> Chr(32) Then
       p = p & Mid(c, i, 1)
      Else
       r = r & Chr(p)
       p = ""
      End If
      Next
     c.Value = r
     r = ""
    Next c
    'append encrypting code at very end of text
    LastRw = Cells.SpecialCells(xlCellTypeLastCell).Row
    Set Lastcel = Cells.SpecialCells(xlCellTypeLastCell)
    Set EffectiveLastcel = Cells(LastRw, Range("iv" & LastRw).End(xlToLeft).Column)
    'MsgBox EffectiveLastCel.Address
    
    If num > 0 And Len(num) = 1 Then 'eg 5
    EffectiveLastcel.Value = EffectiveLastcel.Value & "000" & num
    ElseIf num > 0 And Len(num) = 2 Then 'eg 25
    EffectiveLastcel.Value = EffectiveLastcel.Value & "00" & num
    ElseIf num > 0 And Len(num) = 3 Then 'eg 125
    EffectiveLastcel.Value = EffectiveLastcel.Value & "0" & num
    ElseIf num < 0 And Len(num) = 2 Then 'eg -5
    EffectiveLastcel.Value = EffectiveLastcel.Value & "-" & "00" & Abs(Val(num))
    ElseIf num < 0 And Len(num) = 3 Then 'eg -25
    EffectiveLastcel.Value = EffectiveLastcel.Value & "-" & "0" & Abs(Val(num))
    End If
    'MsgBox Val(Right(EffectiveLastCel, 4))
    'Camouflage encripted 4-digit number by coloring font white
    EffectiveLastcel.Characters(Len(EffectiveLastcel.Value) - 4 + 1, 4).Font.Color = vbWhite
    
    'provide coding seal of Chr(32)&chr(95) as first 2 characters on Line1 to prevent re-encrypting and confusing code numbering
    FirstCel.Value = Chr(32) & Chr(95) & FirstCel.Value
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    
    'Code automatically detects the ecrpting code and uses it
    Sub AutoDECRIPT()
    Dim cd%, q$, y$, i%, k$
    Dim c As Range
    Dim LastRw As Long, FirstCol%
    Dim EffectiveLastcel As Range
    Dim Lastcel As Range
    Application.ScreenUpdating = False
    
    If Mid(FirstCel.Value, 1, 2) <> Chr(32) & Chr(95) Then MsgBox "You cannot attempt to Decrept a normal text." & vbCrLf & "You may have to encrpit before decripting", vbInformation: Exit Sub
    'remove encript seal of Chr(32)& Chr(95) at beginning
    FirstCel.Value = Mid(FirstCel.Value, 3, Len(FirstCel.Value) - 2)
    'Detect 3-digit encrypting code
    LastRw = ActiveSheet.UsedRange.Rows.Count
    Set Lastcel = Cells.SpecialCells(xlCellTypeLastCell)
    Set EffectiveLastcel = Cells(LastRw, Range("iv" & LastRw).End(xlToLeft).Column)
    cd = Val(Right(EffectiveLastcel, 4))
    'MsgBox cd
    'delete 2-digit encripting code appended as 00XX
    q = Left(EffectiveLastcel, Len(EffectiveLastcel) - 4)
    EffectiveLastcel.Value = q
    For Each c In ActiveSheet.UsedRange
      For i = 1 To Len(c)
      y = y & Chr(Asc(Mid(c, i, 1)) - cd)
      Next
      c.Value = y
      y = ""
    Next c
    'restoring reverse text to normal
     For Each c In ActiveSheet.UsedRange
     For i = Len(c) To 1 Step -1
     k = k & Mid(c, i, 1)
     Next
     c.Value = k
     k = ""
     Next c
     Application.ScreenUpdating = True
    End Sub
    Last edited by Myles; 11-02-2007 at 01:26 AM.
    HTH
    Myles

    ...constantly looking for the smoother pebble while the whole ocean of truth lies before me.

+ 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