Skip to content

Instantly share code, notes, and snippets.

@hyhilman
Last active October 2, 2017 15:41
Show Gist options
  • Save hyhilman/6c25a685970657f3ce0ff3d7c3f7a7fa to your computer and use it in GitHub Desktop.
Save hyhilman/6c25a685970657f3ce0ff3d7c3f7a7fa to your computer and use it in GitHub Desktop.
Classical Cryptography

This is classical cruyptography series with Octave, i hope this working on matlab too :)

% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function plain = columnarTranspositionDecode(key, cipher)
if(size(key)(2) != size(unique(key))(2))
printf('Kunci yang digunakan tidak unique\n');
result = '';
return;
else
removedKeyFromOrderedColumnarMatrices = num2cell(reshape(cipher, [], size(key)(2)));
removedKeyFromOrderedColumnarMatricesString = strjoin(removedKeyFromOrderedColumnarMatrices', '');
size = size(removedKeyFromOrderedColumnarMatricesString)(2) / size(key)(2);
orderedColumnarMatrices = stringToMatricesWithHeader(sort(key), removedKeyFromOrderedColumnarMatricesString);
columnarMatrices = cell(size+1,[]);
for i = key
firstrow = cell2mat(orderedColumnarMatrices(1,:));
c = orderedColumnarMatrices(:,firstrow==i);
columnarMatrices = [columnarMatrices,c];
end
columnarMatrices([1],:) = [];
plain = strjoin(columnarMatrices','');
return;
end
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function cipher = columnarTranspositionEncode(key, plain)
key = key(~isspace(key));
plain = plain(~isspace(plain));
if(sizeof(key) != sizeof(unique(key)))
printf('Kunci yang digunakan tidak unique\n');
result = '';
return;
else
columnarMatrix = stringToMatricesWithHeader(key, plain);
orderedColumnarMatrix = sortrows(columnarMatrix',1)';
removedKeyFromOrderedColumnarMatrix = orderedColumnarMatrix;
removedKeyFromOrderedColumnarMatrix([1],:) = [];
cipher = strjoin(removedKeyFromOrderedColumnarMatrix,'');
return;
end
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function nChar = createArrayOfChar(row, col)
char = cell(row, col);
char(:) = {'x'};
nChar = strjoin(char,'');
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function plain = doubleColumnarTranpositionDecode(key1, key2, cipher)
if(size(key1)(2) != size(unique(key1))(2) && size(key2)(2) != size(unique(key2))(2))
printf('Kunci yang digunakan tidak unique\n');
result = '';
return;
else
cipherTextFirst = columnarTranspositionDecode(key2, cipher);
plain = columnarTranspositionDecode(key1, cipherTextFirst);
return;
end
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function cipher = doubleColumnarTranpositionEncode(key1, key2, plain)
if(size(key1)(2) != size(unique(key1))(2) && size(key2)(2) != size(unique(key2))(2))
printf('Kunci yang digunakan tidak unique\n');
result = '';
return;
else
cipherTextFirst = columnarTranspositionEncode(key1, plain);
cipher = columnarTranspositionEncode(key2, cipherTextFirst);
return;
end
end
% This code running on octave :)
% author : hyhilman(hilman nihri)(hy.hilmann@gmail.com)
function matrices = generatePolybiusMatrices(key = '')
stringOfMatrices = strcat(upper(key), upper('abcdefghiklmnopqrstuvwxyz'));
[~,sortedUniqueStringOfMatrices] = unique(stringOfMatrices, 'first');
polybiusText = stringOfMatrices(sort(sortedUniqueStringOfMatrices));
matrices = reshape(polybiusText, 5, [])';
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function vigenereMatrix = generateVigenereMatrices()
aToZ = [1:1:26];
vigenereMatrixOfAscii = mod((aToZ + aToZ') - 2, 26) + 65;
vigenereMatrix = char(vigenereMatrixOfAscii);
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function plain = nihilistDecode(key, cipher)
cipherOfKey = polybiusEncode(key, key);
charOfKey = 0;
cipher1 = [];
for i = cipher
charOfKey = mod(charOfKey, size(key))(2) + 1;
cipher1 = [cipher1, i - cipherOfKey(charOfKey)];
end
plain = polybiusDecode(cipher1, key);
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function cipher = nihilistEncode(key, plain)
cipherTextFirst = polybiusEncode(plain, key);
cipherOfKey = polybiusEncode(key, key);
charOfKey = 0;
cipher = [];
for i = cipherTextFirst
charOfKey = mod(charOfKey, size(key))(2) + 1;
cipher = [cipher, i + cipherOfKey(charOfKey)];
end
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function plain = polybiusDecode(cipher, key='')
polybiusMatrices = generatePolybiusMatrices(key);
plain = [];
for i = cipher
strOfIndex = num2str(i);
row = strOfIndex(1);
col = strOfIndex(2);
c = polybiusMatrices(str2num(row),str2num(col));
plain = [plain, c];
end
return;
end
% This code running on octave :)
% author : hyhilman(hilman nihri)(hy.hilmann@gmail.com)
function cipher = polybiusEncode(plain, key = '')
plainWithoutSpace = upper(plain(~isspace(plain)));
polybiusMatrices = generatePolybiusMatrices(key);
cipher = [];
for i = plainWithoutSpace
if(i=='j')
i = 'i';
end
[row, col] = find(polybiusMatrices==i);
c = [num2str(row), num2str(col)];
cipher = [cipher, str2num(c)];
end
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function matrixWithHeader = stringToMatricesWithHeader(firstrow, plain)
col = size(firstrow)(2);
extraColToPlain = col - mod(size(plain)(2), col);
if(extraColToPlain != col)
plain = strcat(plain,createArrayOfChar(1,extraColToPlain));
end
charArray = num2cell(strcat(firstrow, plain));
matrixWithHeader = reshape(charArray, size(firstrow)(2), [])';
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function plain = vigenereDecode(key, cipher)
length = size(key);
key = upper(key);
vigenereMatrix = generateVigenereMatrices();
indexOfVigenereMatrix = [1:1:26];
indexOfKey = 0;
plain = [];
for c = cipher
indexOfKey = mod(indexOfKey, length(2)) + 1;
row = double(indexOfVigenereMatrix+ 64) == key(indexOfKey);
cipherRow = vigenereMatrix(row,:);
col = indexOfVigenereMatrix(cipherRow == c);
plain = [plain, char(col+64)];
end
return;
end
% This code running on octave :)
% author : hyhilman (hilman nihri)(hy.hilmann@gmail.com)
function cipher = vigenereEncode(key, plain)
key = upper(key);
plain = upper(plain);
length = size(key);
vigenereMatrix = generateVigenereMatrices();
indexOfVigenereMatrix = [1:1:26];
indexOfKey = 0;
cipher = [];
for c = plain
indexOfKey = mod(indexOfKey, length(2)) + 1;
row = double(indexOfVigenereMatrix+ 64) == key(indexOfKey);
col = double(indexOfVigenereMatrix+ 64) == c;
cipher = [cipher, vigenereMatrix(row, col)];
end
return;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment