Encryption Function Enhancements?

Felix_Dragonhammer

Board Regular
Joined
Apr 7, 2015
Messages
117
I wrote the following code (my very first all by myself code! I'm a big boy now lol) and was wondering if anyone knows of a better way, like a loop or something, that I could use instead of going through all the cases.

Code:
Function ENCRYPT2(Message As String, Code_Word As String)
Dim LenMes As String
Dim LenCod As Integer
Dim ML As String
Dim CL As String
Dim Word As String
Dim ModLenCod As String
Dim x As Integer
Dim Col As Integer
Dim Row As Integer
Dim EL As String
Dim LCord As String
Dim Encryption As String
LenMes = Len(Message)
LenCod = Len(Code_Word)
Message = UCase(Message)
Code_Word = UCase(Code_Word)
    For n = 1 To LenMes
        ML = Mid(Message, n, 1)
        
        If ML = " " Then
            EL = " "
            GoTo NextML
        Else
            
        End If
        
        ModLenCod = n Mod LenCod
        
        If ModLenCod = 0 Then
             x = LenCod
        Else
             x = ModLenCod
        End If
        
        CL = Mid(Code_Word, x, 1)
        
        LCord = ML & CL
        
        EL = LCordF(LCord)
NextML:
        Encryption = Encryption & EL
    Next n
ENCRYPT2 = Encryption
End Function
Sub Exp()
Dim Value
Dim Message As String
Dim Code_Word As String
Message = "This is awesome"
Code_Word = "Happy"
MsgBox ENCRYPT2(Message, Code_Word)
------------------------------------------------------------------------------------------------------------
End Sub
Function LCordF(L As String)
Select Case L
    Case "AA", "BZ", "CY", "DX", "EW", "FV", "GU", "HT", "IS", "JR", "KQ", "LP", "MO", "NN", "OM", "PL", "QK", "RJ", "SI", "TH", "UG", "VF", "WE", "XD", "YC", "ZB": LCordF = "A"
    Case "AB", "BA", "CZ", "DY", "EX", "FW", "GV", "HU", "IT", "JS", "KR", "LQ", "MP", "NO", "ON", "PM", "QL", "RK", "SJ", "TI", "UH", "VG", "WF", "XE", "YD", "ZC": LCordF = "B"
    Case "AC", "BB", "CA", "DZ", "EY", "FX", "GW", "HV", "IU", "JT", "KS", "LR", "MQ", "NP", "OO", "PN", "QM", "RL", "SK", "TJ", "UI", "VH", "WG", "XF", "YE", "ZD": LCordF = "C"
    Case "AD", "BC", "CB", "DA", "EZ", "FY", "GX", "HW", "IV", "JU", "KT", "LS", "MR", "NQ", "OP", "PO", "QN", "RM", "SL", "TK", "UJ", "VI", "WH", "XG", "YF", "ZE": LCordF = "D"
    Case "AE", "BD", "CC", "DB", "EA", "FZ", "GY", "HX", "IW", "JV", "KU", "LT", "MS", "NR", "OQ", "PP", "QO", "RN", "SM", "TL", "UK", "VJ", "WI", "XH", "YG", "ZF": LCordF = "E"
    Case "AF", "BE", "CD", "DC", "EB", "FA", "GZ", "HY", "IX", "JW", "KV", "LU", "MT", "NS", "OR", "PQ", "QP", "RO", "SN", "TM", "UL", "VK", "WJ", "XI", "YH", "ZG": LCordF = "F"
    Case "AG", "BF", "CE", "DD", "EC", "FB", "GA", "HZ", "IY", "JX", "KW", "LV", "MU", "NT", "OS", "PR", "QQ", "RP", "SO", "TN", "UM", "VL", "WK", "XJ", "YI", "ZH": LCordF = "G"
    Case "AH", "BG", "CF", "DE", "ED", "FC", "GB", "HA", "IZ", "JY", "KX", "LW", "MV", "NU", "OT", "PS", "QR", "RQ", "SP", "TO", "UN", "VM", "WL", "XK", "YJ", "ZI": LCordF = "H"
    Case "AI", "BH", "CG", "DF", "EE", "FD", "GC", "HB", "IA", "JZ", "KY", "LX", "MW", "NV", "OU", "PT", "QS", "RR", "SQ", "TP", "UO", "VN", "WM", "XL", "YK", "ZJ": LCordF = "I"
    Case "AJ", "BI", "CH", "DG", "EF", "FE", "GD", "HC", "IB", "JA", "KZ", "LY", "MX", "NW", "OV", "PU", "QT", "RS", "SR", "TQ", "UP", "VO", "WN", "XM", "YL", "ZK": LCordF = "J"
    Case "AK", "BJ", "CI", "DH", "EG", "FF", "GE", "HD", "IC", "JB", "KA", "LZ", "MY", "NX", "OW", "PV", "QU", "RT", "SS", "TR", "UQ", "VP", "WO", "XN", "YM", "ZL": LCordF = "K"
    Case "AL", "BK", "CJ", "DI", "EH", "FG", "GF", "HE", "ID", "JC", "KB", "LA", "MZ", "NY", "OX", "PW", "QV", "RU", "ST", "TS", "UR", "VQ", "WP", "XO", "YN", "ZM": LCordF = "L"
    Case "AM", "BL", "CK", "DJ", "EI", "FH", "GG", "HF", "IE", "JD", "KC", "LB", "MA", "NZ", "OY", "PX", "QW", "RV", "SU", "TT", "US", "VR", "WQ", "XP", "YO", "ZN": LCordF = "M"
    Case "AN", "BM", "CL", "DK", "EJ", "FI", "GH", "HG", "IF", "JE", "KD", "LC", "MB", "NA", "OZ", "PY", "QX", "RW", "SV", "TU", "UT", "VS", "WR", "XQ", "YP", "ZO": LCordF = "N"
    Case "AO", "BN", "CM", "DL", "EK", "FJ", "GI", "HH", "IG", "JF", "KE", "LD", "MC", "NB", "OA", "PZ", "QY", "RX", "SW", "TV", "UU", "VT", "WS", "XR", "YQ", "ZP": LCordF = "O"
    Case "AP", "BO", "CN", "DM", "EL", "FK", "GJ", "HI", "IH", "JG", "KF", "LE", "MD", "NC", "OB", "PA", "QZ", "RY", "SX", "TW", "UV", "VU", "WT", "XS", "YR", "ZQ": LCordF = "P"
    Case "AQ", "BP", "CO", "DN", "EM", "FL", "GK", "HJ", "II", "JH", "KG", "LF", "ME", "ND", "OC", "PB", "QA", "RZ", "SY", "TX", "UW", "VV", "WU", "XT", "YS", "ZR": LCordF = "Q"
    Case "AR", "BQ", "CP", "DO", "EN", "FM", "GL", "HK", "IJ", "JI", "KH", "LG", "MF", "NE", "OD", "PC", "QB", "RA", "SZ", "TY", "UX", "VW", "WV", "XU", "YT", "ZS": LCordF = "R"
    Case "AS", "BR", "CQ", "DP", "EO", "FN", "GM", "HL", "IK", "JJ", "KI", "LH", "MG", "NF", "OE", "PD", "QC", "RB", "SA", "TZ", "UY", "VX", "WW", "XV", "YU", "ZT": LCordF = "S"
    Case "AT", "BS", "CR", "DQ", "EP", "FO", "GN", "HM", "IL", "JK", "KJ", "LI", "MH", "NG", "OF", "PE", "QD", "RC", "SB", "TA", "UZ", "VY", "WX", "XW", "YV", "ZU": LCordF = "T"
    Case "AU", "BT", "CS", "DR", "EQ", "FP", "GO", "HN", "IM", "JL", "KK", "LJ", "MI", "NH", "OG", "PF", "QE", "RD", "SC", "TB", "UA", "VZ", "WY", "XX", "YW", "ZV": LCordF = "U"
    Case "AV", "BU", "CT", "DS", "ER", "FQ", "GP", "HO", "IN", "JM", "KL", "LK", "MJ", "NI", "OH", "PG", "QF", "RE", "SD", "TC", "UB", "VA", "WZ", "XY", "YX", "ZV": LCordF = "V"
    Case "AW", "BV", "CU", "DT", "ES", "FR", "GQ", "HP", "IO", "JN", "KM", "LL", "MK", "NJ", "OI", "PH", "QG", "RF", "SE", "TD", "UC", "VB", "WA", "XZ", "YY", "ZX": LCordF = "W"
    Case "AX", "BW", "CV", "DU", "ET", "FS", "GR", "HQ", "IP", "JO", "KN", "LM", "ML", "NK", "OJ", "PI", "QH", "RG", "SF", "TE", "UD", "VC", "WB", "XA", "YZ", "ZY": LCordF = "X"
    Case "AY", "BX", "CW", "DV", "EU", "FT", "GS", "HR", "IQ", "JP", "KO", "LN", "MM", "NL", "OK", "PJ", "QI", "RH", "SG", "TF", "UE", "VD", "WC", "XB", "YA", "ZZ": LCordF = "Y"
    Case "AZ", "BY", "CX", "DW", "EV", "FU", "GT", "HS", "IR", "JQ", "KP", "LO", "MN", "NM", "OL", "PK", "QJ", "RI", "SH", "TG", "UF", "VE", "WD", "XC", "YB", "ZA": LCordF = "Z"
End Select
End Function
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Can you explain the methodology of what you are trying to do? I get the message part, what is the code word for, etc.?
 

Felix_Dragonhammer

Board Regular
Joined
Apr 7, 2015
Messages
117
ABCDEFGHIJKLMNOPQRSTUVWXYZ
AABCDEFGHIJKLMNOPQRSTUVWXYZ
BBCDEFGHIJKLMNOPQRSTUVWXYZA
CCDEFGHIJKLMNOPQRSTUVWXYZAB
DDEFGHIJKLMNOPQRSTUVWXYZABC
EEFGHIJKLMNOPQRSTUVWXYZABCD
FFGHIJKLMNOPQRSTUVWXYZABCDE
GGHIJKLMNOPQRSTUVWXYZABCDEF
HHIJKLMNOPQRSTUVWXYZABCDEFG
IIJKLMNOPQRSTUVWXYZABCDEFGH
JJKLMNOPQRSTUVWXYZABCDEFGHI
KKLMNOPQRSTUVWXYZABCDEFGHIJ
LLMNOPQRSTUVWXYZABCDEFGHIJK
MMNOPQRSTUVWXYZABCDEFGHIJKL
NNOPQRSTUVWXYZABCDEFGHIJKLM
OOPQRSTUVWXYZABCDEFGHIJKLMN
PPQRSTUVWXYZABCDEFGHIJKLMNO
QQRSTUVWXYZABCDEFGHIJKLMNOP
RRSTUVWXYZABCDEFGHIJKLMNOPQ
SSTUVWXYZABCDEFGHIJKLMNOPQR
TTUVWXYZABCDEFGHIJKLMNOPQRS
UUVWXYZABCDEFGHIJKLMNOPQRST
VVWXYZABCDEFGHIJKLMNOPQRSTU
WWXYZABCDEFGHIJKLMNOPQRSTUV
XXYZABCDEFGHIJKLMNOPQRSTUVW
YYZABCDEFGHIJKLMNOPQRSTUVWX
ZZABCDEFGHIJKLMNOPQRSTUVWXY

<tbody>
</tbody><colgroup><col span="2"><col span="25"></colgroup>
 

Felix_Dragonhammer

Board Regular
Joined
Apr 7, 2015
Messages
117
Basically, this was how encryption used to be done at one point or another, generally speaking:

You would have a message (let's say I am happy), and to encrypt it, you would need a chart like the one above and a code word (let's say Hi).
To encrypt it, you would follow the following procedure:
Code:
I AM HAPPY
HIHIHIHIHI

You would repeat the code word and line it up with the message, and you would use the letters as coordinates.
In this example, you would get (I, H), nothing (because it's blank), (A, H), (M, I), nothing again, (H, I), (A, H), (P, I), (P, H), (Y, I).

You would then find the coordinates on the grid and come up with a message.
In this example, you'd go to I column in the top row, then go to corresponding H row, and you get "P".

If you did it for all of them, you'd get the following encryption: "P HU PHXWG"
(Granted, it is probably not a very good idea to have spaces in your encryption as that makes it easier to crack, but I haven't gotten around to fixing that yet, plus I'm not sure how I would get the code to skip spaces with out allocating letters to the spaces. If that makes sense. However, it's not a major concern, as this is just for fun).


Anyways, the way I originally did this was I had an excel worksheet that did all the computations for me and it would reference that chart.

<tbody>
</tbody>
 

Watch MrExcel Video

Forum statistics

Threads
1,129,861
Messages
5,638,734
Members
417,049
Latest member
baka416

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top