Skip to content

Instantly share code, notes, and snippets.

@hyhilman
Last active September 13, 2017 12:20
Show Gist options
  • Save hyhilman/622ac6d674b51c484cdf897f94d714d4 to your computer and use it in GitHub Desktop.
Save hyhilman/622ac6d674b51c484cdf897f94d714d4 to your computer and use it in GitHub Desktop.
Caisar Cipher Text Matlab
alfabet = "abcdefghijklmnopqrstuvwxyz"
alfabet = [alfabet, upper(alfabet), ' ']
plain = 'Aku makan es krim'
plainafterdecode = ''
key = 33
cipher = ''
modulus = length(alfabet)
for plainsequence = 1:length(plain)
for alfabetsequence = 1:length(alfabet)
if plain(plainsequence) == alfabet(alfabetsequence)
i = mod(alfabetsequence + key - 1, modulus) + 1
cipher = [cipher, alfabet(i)]
end
end
end
for ciphersequence = 1:length(cipher)
for alfabetsequence = 1:length(alfabet)
if cipher(ciphersequence) == alfabet(alfabetsequence)
i = mod(alfabetsequence - key - 1, modulus) + 1
plainafterdecode = [plainafterdecode, alfabet(i)]
end
end
end
fprintf('%s -> %s -> %s \n', plain, cipher, plainafterdecode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment