# Cipher encryption with MATLAB

Matlab @ 07 October 2010

I recently came across a post on reddit requesting assistance with conversion of character data to encrypted numerical data based on a given encryption table – which takes the form of a 28×10 matrix where the columns designate the character and the rows designate the “cycle” or the numbered iteration of the particular character within a 10 number group.

This is apparently called a homophonic substitution cipher and the idea is not only to call the alphabetic substitutes from a table; but to also deter frequency analysis by giving high frequency letters a greater number of substitutes. This is implemented by adding some number of -1s to low frequency characters within the cipher table where the -1s are interpreted as a command to find some non-1 number within that character’s column.

Obviously, there are many ways to do this but since I came up with one that worked, I figured that I would post it and give a little demonstration. So first, here is the code for the encryption function.

function [cm,T] = hp_encr(string)
T1=randperm(280);
T=zeros(10,28);
for i=1:28:280
  T(ceil(i/28),:)=T1(i:i+27);
end
%I know very little about encryption tables so let us just
%generate a non-repeating random integer equivalent and forget
%the -1s for the table but certainly not neglect them in conversion.
 
na=strread(string,'%c')-96; %converts some alphabetical string into numerical 
%equivalents (based on alphabetical numbering) and places them in a column vector of
%an array. 
 
cm(1:length(na),1)=0;
for i=1:length(na(:,1))
  cm(i,1)=T(randi(10),na(i,1));
while T(randi(10),na(i,1))==-1
  cm(i,1)=T(randi(10),na(i,1));
end
%This gives you column vector cm with random keys for a certain letter
%picked from table T.
end

Now, if we have an encrypter we need to have a decrypter right? Well this is really simple if we have the key (i.e., make sure you called for the key in the output!).

function [dm,q] = hp_decr(cm,T)
for i=1:length(cm)
[p(i,1),q(i,1)]=find(T==cm(i,1));
end
%Gets the index of the number. q - the column index - is what we want here.
 
dm=setstr(q+96)';
%converts the sequence back into letters.
end

So now that we have the code, let’s see if it does what it’s supposed to.

>> [cm,T]=hp_encr('gooddaydearwalrusyourtuskislookinghealthyandyoureyesshinesobeautifully')
 
cm =
 
   261
   232
   233
   106
    92
    98
   255
   241
   278
    39
   165
   209
    39
   207
   227
   161
    21
   206
    90
     8
     7
   169
   257
   230
    86
    60
   140
    34
    28
    74
   149
    62
    87
    73
   240
   160
   195
   269
   260
    29
   223
   183
   138
   235
   255
    28
    23
   126
   224
   231
   143
    21
    93
   170
    60
    81
   143
   270
    61
   279
   224
   164
    23
    22
    30
   117
    65
    88
    34
   206
 
 
T =
 
  Columns 1 through 11
 
    75   180   114   106   203   218   108   262    30    47   163
    39   119    27     6   143   129   130    31   214    49   243
    98   279    11    63   137   105   261   202   158   103    86
   164    80   226    55   160   259    56   128   211   146   267
   216   266   135   100   242   144    73   240    24   141   149
    58   145   275    33   120   132   186   170    20   244   194
   183    96   181    92   224   159   273    29   156    50   185
   277   212   168   225    48    15   189   222    60    68   268
   195   153    51   235    84   150    36    10   116   265   131
    95    13   205   241   278   117   184   113    62    89   200
 
  Columns 12 through 22
 
   201   228   188    28   238   252   126   237    97    65    45
   207   264    81    61   190   276    18   270   272    23   199
   110   248   192   187   142    59   139   162    22    17    38
    88    16    87    78   258    85   177   230   136   251    53
   127   148   138   233     5   173   227   140   260   257    71
    34   176   193   213   104   155   165    79   169     8    52
   122   239    43     9   263   182   246   101   112    19    82
   269   115   152    90   175    69     7    93   107   161     3
   234    35   151   232    67    54   121    21   274    94   249
   220   245    42    74   133   250   124   123    70   125   166
 
  Columns 23 through 28
 
    64    44   157   221   179   271
   253    46   255   210   111   174
   191   236   231    91   256    14
   215   229     4   172   109   247
    25     1   154    37   208     2
   209    77   206   254   217   219
   204    83   147   167    32   178
    66   102    57    26    72   197
    12    99   196   280   171    40
   198    41   223   118    76   134
>> hp_decr(cm,T)
 
ans =
 
gooddaydearwalrusyourtuskislookinghealthyandyoureyesshinesobeautifully

So, we encrypted and then decrypted a string and that’s that. I’ll probably post exercises soon – maybe a picture of a walrus I guess.