Code to encrypt a string?

93 views (last 30 days)
Tyler Silva
Tyler Silva on 26 Oct 2015
Answered: TastyPastry on 26 Oct 2015
Im writing a code that needs to do the following:
  • 1) Prompt the user to enter the message to encrypt and the encryption key that will be used to encrypt the message
  • 2) Convert your message into the ASCII values by changing it from a string into a vector of double values using the double command:
  • 3) Go through each value in the message and do the following:
  • a. If the character is a capital letter (ASCII values 65-90), encrypt it by adding the encryption key value to the ASCII value of the letter, ensuring that the encrypted value remains a capital letter by wrapping around to the beginning of the alphabet (i.e. a Z (ASCII value of 90) would end up as a D (ASCII value of 68) using a key value of 4 b. If the character is a lowercase letter (ASCII values 97-122), encrypt it by adding the encryption key value to the ASCII value of the letter, ensuring that the encrypted value remains a lowercase letter by wrapping around to the beginning of the alphabet (i.e. a z (ASCII value of 122) would end up as a f (ASCII value of 102) using a key value of 6)
  • c. If the character is not a letter (number, punctuation, space, etc.), leave it as is
  • 4) Display the encrypted message using an fprintf statement and the %s placeholder
The first two are input statements which should be written as
original_message=input('What message would you like to encrypt')
key=input('What encryption key will you be using')
Next, I need to insert the doubles command
number_message = double(original_message);
I get confused after this. Should I make a vector with my message, and then write if statements to convert the letters to numbers. Im having trouble converting my message over to the encryption key.
These are the encryption keys for capital and lowercase tables below:
A B C D E F G H I J K L M
65 66 67 68 69 70 71 72 73 74 75 76 77
N O P Q R S T U V W X Y Z
78 79 80 81 82 83 84 85 86 87 88 89 90
a b c d e f g h i j k l m
97 98 99 100 101 102 103 104 105 106 107 108 109
n o p q r s t u v w x y z
110 111 112 113 114 115 116 117 118 119 120 121 122 123
Heres my code so far:
s ='MATLAB'
val=double(s)
nume=reshape(val, 3, 5)
m = [1 5 3; 2 11 8; 4 24 21]
%inv(m)
nume=nume-32
ncoded=mod(m*(nume),95)+32
scoded=reshape(char(ncoded),1,15)
%sdecoded = reshape(double(scoded),3,5)
%ndecoded = mod (inv(m)*(sdecoded-32), 95) + 32
%sdecoded = reshape (char(ndecoded), 1, 15)

Accepted Answer

TastyPastry
TastyPastry on 26 Oct 2015
There are tons of Caesar Cipher resources out there.
Also, delete one of these duplicate questions.

More Answers (0)

Categories

Find more on Encryption / Cryptography in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!