Jump to content

Simple Cipher in Excel?

Recommended Posts

I am trying to do a simple cipher - offset a string of letter by 2 characters automatically

 

so a becomes c, b becomes d etc

 

any suggestions for the cell formula to do that as I am getting in a right pickle

 

tia

Share this post


Link to post
Share on other sites

So with the letter a in cell a1, use formula =CHAR(CODE(A1)+2)

 

Job done (returns c).

 

Encrypt / Decrypt any 6 character string see: http://www.dt4u.com/filearea/cryptography.xls

 

Bit clunky but works. You could easily change the offset for any number.

Edited by Afilsdesigne

Share this post


Link to post
Share on other sites

Open your spreadsheet and press alt-F11 to get the Visual Basic editor. Choose Insert/Module from the menu and paste in the code below. Press alt-Q to close the editor.

 

You can use =Rot("My string",2) or =Rot(F3,2) to encode your string with offset 2.

 

Option Explicit
Function Rot(codeString, Optional offset)
'Simple encrypting. Rotate original string by 'offset' characters
'By maggi 13/2/13

   Dim charIndex As Long, charCode As Integer, alpha As Integer

   If IsMissing(offset) Then
       offset = 0                                      ' ignore missing values
   End If
   If offset > 26 Or offset < 0 Then
       offset = 0                                      ' ignore invalid values
   End If

   For charIndex = 1 To Len(codeString)                ' for each character in the string
       charCode = Asc(Mid$(codeString, charIndex, 1))  ' get the character code
                                                       ' only use an offset if the character is A-Z or a-z
       If (charCode >= Asc("A") And charCode <= Asc("Z")) Or (charCode >= Asc("a") And charCode <= Asc("z")) Then

           If charCode >= Asc("a") Then                ' are we in upper or lower case?
               alpha = Asc("a")
           Else
               alpha = Asc("A")
           End If

           charCode = charCode + offset                ' add on 'offset' characters

           If charCode >= alpha + 26 Then              ' have we gone past the end of our case range?
              charCode = charCode - 26
           End If

       End If
       Mid$(codeString, charIndex, 1) = Chr(charCode)  ' substitute the new character
   Next charIndex
   Rot = codeString

End Function

Edited by maggi

Share this post


Link to post
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
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.