Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

from string import ascii_lowercase as alphabet

class CaesarCypher:
    alpha_len = len(alphabet)
    min_guess_rate = 0.2

Encryption and decryption is a same stuff. when you want to decrypt for example with shift 10 that means that you can encrypt it with shift 26 - 10. In this case cycle will repeat at if you going to shift whole alphabet it will be the same. Also here i've proceed upper case and non chars

    def __call__(self, text, offset, encrypt=True):
        if not encrypt:
            offset = self.alpha_len - offset
        result = []
        for letter in text:
            if not letter.isalpha():
                result.append(letter)
                continue
            letter_to_process = letter.lower()
            processed_letter = self._encrypt_letter(letter_to_process, offset)
            if letter.isupper():
                processed_letter = processed_letter.upper()
            result.append(processed_letter)
        return ''.join(result)

all encryption goes here at most.

    def _encrypt_letter(self, letter, offset=0):
        position = (alphabet.find(letter) + offset) % self.alpha_len
        return alphabet[position]

this part is for broot force and guess throug dictionary frequency.

    @staticmethod
    def __how_many_do_i_know(text):
        clean_words = filter(lambda x: x.isalpha(), text.split())
        clean_words = ['\'{}\''.format(x) for x in clean_words]
        cursor = conn.cursor()
        query = 'SELECT COUNT(*) FROM mydictionary WHERE word IN ({})'.format(",".join(clean_words))
        cursor.execute(query)
        response = cursor.fetchone()[0]
        return response / len(clean_words)

    def guess_encode(self, text):
        options = [self(text, offset, encrypt=False) for offset in range(self.alpha_len)]
        best_option = [self.__how_many_do_i_know(option) for option in options]
        best_key, guess_rate = max(enumerate(best_option), key=lambda x: x[-1])
        guess_text = options[best_key]
        return best_key, guess_rate, guess_text

Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

Caesar's cypher is the simplest encryption algorithm. It adds a fixed value to the ASCII (unicode) value of each character of a text. In other words, it shifts the characters. Decrypting a text is simply shifting it back by the same amount, that is, it substract the same value from the characters. Write a function called caesar that accepts two arguments: the first is the character vector to be encrypted, while the second is the shift amount. The function returns the output argument coded, the encrypted text. The function needs to work with all the visible ASCII characters from space to ~. The ASCII codes of these are 32 through 126. If the shifted code goes outside of this range, it should wrap around. For example, if we shift ~ by 1, the result should be space. If we shift space by -1, the result should be ~.

for the first part of the code...this works

function coded=caesar(A,n)

char_set=char(32):char(126)

coded=char(A+n);

end

But if i want to maintain the range between 32 and 126 ....and also wrap around the same values as asked in later half of question , it doesnt work

function coded=caesar(A,n)

char_set=char(32):char(126)

while A <=char_set

coded=char(A+n);

end

end

please help me with the concerned code buiding ....( expect a simple approach , since iam a begineer)

test for these outputs

caesar('ABCD',1)

ans =

'BCDE'

caesar('xyz ~',1)

ans =

'yz{! '

caesar('xyz ~',-1)

ans =

'wxy~}'

Accepted Answer

Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(M, n)

num = double(M);

num2 = num;

N = n - 95 * fix(n/95);

for i = 1:length(num);

if num(i) + N < 32

num2(i) = 126 - (31- num(i) - N);

elseif num(i) + N > 126

num2(i) = 32 + (num(i) + N -127);

else

num2(i) = num(i) + N ;

end

coded = char(num2);

end

I spent halfhour on solving this problem, a little bit hard.

This is correct code. Hope it helps.

Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

More Answers (38)

Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded= caesar(string,shift)

value=string+shift;

for i=1:length(value)

while value(i)<32

value(i)=value(i)+95;

end

while value(i)>126

value(i)=value(i)-95;

end

end

coded=char(value);


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

Thanks to a person who told about circshift function. I've been tried several hours to solve this task without that function.

So finaly I've passed all tests. And final code is much shorter and elegant as I have at today's morning ))

It has only 4 lines including the "end".

The main idea is to shift character table, but not the symbols of input.

function coded = caesar (array,shift)

shifted_array=circshift(char(32:126),-shift);

coded = shifted_array(double(array)-31)

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(text,amound)

n=amound - 95 * fix(amound/95);

v=double(text)+n;

v(v<32)=126-(31-v(v<32));

v(v>126)=32+(-127+v(v>126));

coded=char(v);

end


Use mod or rem to constrain values between 0 and a maximum, with wrap-around.e.g:

>> mod(0:51, 26)

ans =

Columns 1 through 21

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Columns 22 through 42

21 22 23 24 25 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Columns 43 through 52

16 17 18 19 20 21 22 23 24 25

You can see that values 26, 27, ... go back to 0, 1, ...

Add/subtract some offsets to do the same for values in the range 32:126


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

I came up with an approach which uses logical indexing instead of if statement

function coded=caesar(str,n)

str1=double(str);

m=n-95*floor(n/95);

codedstr1=str1+m;

codedstr1(codedstr1>=127)=codedstr1(codedstr1>=127)-127+32;

coded=char(codedstr1);

end

Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(message , n)

msg = double(message);

m = mod(n, 95);

coded_msg = msg + m;

coded_msg(coded_msg > 126) = coded_msg(coded_msg > 126) - 95;

coded_msg(coded_msg < 32) = coded_msg(coded_msg < 32) + 95;

coded =char(coded_msg);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(arr, num)

size = strlength(arr);

coded = arr+num

for i = 1:size

while coded(i)> 126

coded(i) = coded(i) - 95;

end

while coded(i) < 32

coded(i) = coded(i) + 95;

end

end

coded = char(coded);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

This problem is asking you to shift the character variable by a given element n

the word wrap means that if the ASCII code of your character exceeds 32 or 126 you need to circle back again .

For example

if ASCII code is 97 and n (shift variable) is 45 so your ASCII code is 142 which exceeds 126. So you need to subtract 126 from 142

142-126, and add the net result to 31.

you need'nt do all that..... use the function called circshift

so i defined a character array from 32 to 126 which is the required ascii range

these are the characters.

ch =

' !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'

and when i use the circshift command

ch_shift_pos=circshift(ch,2)

ch_shift_pos =

'}~ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|'

ch_shift_neg=circshift(ch,-2)

ch_shift_neg=

'"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !'

Notice how the characters have shifted by 2 positions without you having to manually keep it within its range.

now if i want to find a character and its corresponding encrypted/shifted value i simply transfer the index since both ch and ch_shift are of the samesize

so for example: if i want to find the encryption character of a , i first find the index of a in the 'ch' array and store it in a variable called x

and then i use that index to find the encryption in the shifted array

ch_shift_pos(x)

ans =

'_'

ch_shift_neg(x)

ans =

'c'

There! thats your answer. As simple as that. But i had to rack my brains for it. :P


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(str,n)

num1 = double(str);

for i = 1 : length(num1)

if num1(i) + n > 126

m = num1(i)-126+n;

p = 31+m;

num1(i) = p;

elseif num1(i)+n < 32

m = 32 - num1(i) + n;

p = 126 - m;

num1(i) = p;

else m = num1(i) + n;

num1(i) = m;

end

code(i) = num1(i);

end

coded = char(code);

I have written this code. Can anyone please expain as what is wrong in here? I know i have made a mistake. But i am not able to figure it out.

Thanks in advance.


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function [coded]= caesar(code , shift)

A = double(code);

x = 1;

z = length(A);

z = z+ 1;

shift = shift - 95*(fix(shift/95));

code(1:end) = code(1:end) + shift;

while x ~= z

if code(1,x)< 32

A(1,x) = A(1,x) - 32;

A(1,x) = A(1,x) + shift;

A(1,x) = A(1,x) + 127;

elseif (code(1,x)>32)&&(code(1,x)<127)

A(1,x) = A(1,x) + shift;

else

A(1,x) = A(1,x) - 127;

A(1,x) = A(1,x) + shift;

A(1,x) = A(1,x) + 32;

end

x= x + 1;

end

coded = char(A);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function txt = caesar(txt,key)

txt = double(txt) + key;

first = double(' ');

last = double('~');

txt = char(mod(txt - first,last - first + 1) + first);

end

function y = caesar2(ch, key)

v = ' ' : '~';

[~, loc] = ismember(ch, v);

v2 = circshift(v, -key);

y = v2(loc);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(ctbe,sa)

dd = double(ctbe)

if dd>=32 & dd<=126

ss =dd +sa

ss(ss<32) = rem((ss(ss<32)-32),95)+127

ss(ss>126)=rem((ss(ss>126)-126),95)+31

coded =char(ss);

end

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

Short code with use of Logical Indexing:

function coded= caesar(string,shift)

mod_str=string+shift;

for i=1:length(mod_str)

mod_str(mod_str<32)=mod_str(mod_str<32)+95;

mod_str(mod_str>126)=mod_str(mod_str>126)-95;

end

coded=char(mod_str);


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(V,N)

ascii = char(32:126);

coded1 = (double(V) + N - 31);

found = false;

ii = 1;

coded2 = [];

while ~(found)

if ii < length(coded1)+1

j = coded1(ii);

ii = ii +1;

while j < 32

j = j + 126 - 31;

end

while j > length(ascii)

j = j - length(ascii);

end

coded2 = abs([coded2,j]);

else

found = true;

break;

end

end

coded = ascii(coded2);


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

help me in this . i wrote this code but i am getting error please tell me what's wrong in this

function y=caesar(a,b)

q=double(a);

for i=1:length(a)

d(i)=q(i)+b;

if d(i)>=32;

e(i)=rem(d(i),126);

else

e(i)=95+d(i);

end

if e(i)>=32 ;

y(i)=char(e(i));

elseif e(i)==0;

y(i)=char(126);

else

e(i)=e(i)+31;

y(i)=char(e(i));

end

end

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded=caesar(c,s)

n=mod(s,95);

sc=c+n;

l=length(sc);

w=[];

for i=1:l

if sc(i)>126

p=31+(sc(i)-126);

elseif sc(i)<32

p=126-abs(sc(i)-31);

else

p=sc(i);

end

w=[w p];

end

coded=char(w);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

The code below is a long one but it is using a while loop and if you read it, it is an easy one and it is correct for any random shifts.

function coded = caesar(A,n)

a = double(A);

z = a + n;

for i =1: length(a)

if z(i)>126

b = z(i) - 126;

if b <=95

z(i) = 31 + b;

else

while b > 95

b = b-95;

end

z(i) = 31 + b;

end

end

if z(i) < 32

c = 32 - z(i);

if c <= 95

z(i) = 127 - c;

else

while c >95

c = c - 95;

end

z(i) = 127 -c;

end

end

end

encrypted_code = z;

coded = char(encrypted_code);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

This code is quite lenghty, but logic that i have used is quite simple understand

function coded = caesar(A, n)

a = double(A)

ele=size(a)

for i=1:ele(1,2)

if n>=0

for j=1:n

a(i) = a(i)+1;

if a(i)>126

a(i)=32;

end

end

end

if n<0

for j=1:abs(n)

a(i) = a(i)-1;

if a(i)<32

a(i)=126;

end

end

end

end

coded = char(a);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(M, n)

num = double(M);

num2 = num;

N = n - 95 * fix(n/95);

for i = 1:length(num);

if num(i) + N < 32

num2(i) = 126 - (31- num(i) - N);

elseif num(i) + N > 126

num2(i) = 32 + (num(i) + N -127);

else

num2(i) = num(i) + N ;

end

coded = char(num2);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

(using mod function)

function txt = caesar(txt,key)

txt = double(txt) + key;

first = double(' ');

last = double('~');

txt = char(mod(txt - first,last - first + 1) + first);

end

(using circ shify function)

function y = caesar2(ch, key)

v = ' ' : '~';

[~, loc] = ismember(ch, v);

v2 = circshift(v, -key);

y = v2(loc);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(message,code)

while(code>95)

code=code-95;

end

while(code<-95)

code=code+95;

end

message=message+code;

message(message>126)=char(double(message(message>126))-95)

message(message<32)=char(double(message(message<32))+95)

coded=char(message);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded=caesar(v,n)

x=double(v)+n;

q=x(x<32);

p=x(x>126);

while q<32;

x(x<32)=x(x<32)+95;

q=x(x<32);

end

while p>126;

x(x>126)=x(x>126)-95;

p=x(x>126);

end

coded=char(x);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

here you go

function [coded]=caesar(a,shift)

m=double(a)

codedd=m+shift;

for i=1:abs(shift)

codedd(codedd<32)=127-(32-codedd(codedd<32));

codedd(codedd>126)=31+(codedd(codedd>126)-126)

coded=char(codedd)

end

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded = caesar(char_vec,shift_amount)

char_value = char_vec+shift_amount;

for ii = 1:length(char_value)

if char_value>126

coded = char(char_value-95);

elseif char_value<32

coded = char(char_value+95);

else

coded = char(char_value);

end

end

end

I got correct output but in the assignment when i run this program it shows error...What fault i did i m unable to notice,if anyone can explain me then plz help me.


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

I'm beginner and I have written this code. Can anybody help me by expaining that what is wrong in here?

function coded = caesar(string,shift)

double_A = double(string);

position = double_A + shift;

for ii = 1:length(position)

if position > 126

new_position = position - 95;

elseif position < 32

new_position = position + 95;

else

new_position = position;

end

end

coded = char(new_position);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded=caesar(a,b)

x=double(a);

k=mod(b,95);

q=[];

for j=1:length(x)

p=x(j)+k;

if p<=126 && p>=32

q=[q p];

elseif p>126

r=p-95;

q=[q r] ;

elseif p<32

s=p+95;

q=[q s];

end

end

coded=char(q);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

A much shorter solution is possible using logical arrays.

function coded =caesar(str, n)

coded = str + n;

while ( sum(coded >= 127) > 0 || sum(coded <= 31) >0 )

coded(coded >= 127) = 31 + (coded(coded>=127)-126);

coded(coded <= 31) = 127 - (32-coded(coded<=31));

end

coded = char(coded);

The while loop condition simply ensures that there is no overflow after each round of correction

The important thing is your output should be a string


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded= caesar(v,s)

v=v+s;

v(v>126)=rem(v(v>126),95);

v(v<32)=127-rem(32-v(v<32),95);

coded=char(v);


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function cloud=caesar(m,n)

sum=double(m+n);

for i=1:length(sum)

if n>0

if (sum(i)<127)

cloud(i)=char(sum(i));

elseif sum(i)>126

store=sum(i)-126;

sto=mod(store,95);

if sto==0

s=126;

cloud(i)=char(s+sto);

else

s=32;

cloud(i)=char(s+sto-1);

end

end

else

if n<0

if (sum(i)>=32)

cloud(i)=char(sum(i));

else

if sum(i)<32

store=32-sum(i);

sto=mod(store,95);

cloud(i)=char(126-sto+1);

end

end

end

end

end

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function [coded] = caesar(v ,sa)

secret = double(v);

code = ones(1, length(v));

for ii=1:length(secret)

if secret(ii)+sa > 126

remainder=rem(sa,95);

if remainder + secret(ii)>126

code(ii)=31+(remainder-(126-secret(ii)));

else

code(ii)=remainder+secret(ii);

end

elseif secret(ii)+sa < 32

remainder=abs(rem(sa,95));

if secret(ii)-remainder < 32

code(ii)=127-(remainder-(secret(ii)-32));

else

code(ii)=secret(ii)-remainder;

end

else

code(ii) = sa + secret(ii);

end

end

coded=char(code);

end


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

simple and fast

function coded = caesar(txt,nr)

char_set=char(32):char(126);

char_set3=[char_set char_set char_set];

coded = char_set3(txt+64+nr-floor(nr/95)*95);

end


function txt = caesar(txt,key)

txt = double(txt) + key;

first = double(' ');

last = double('~');

txt = char(mod(txt - first,last - first + 1) + first);

end

This also helps to solve the problem. Try it


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

Can someone help to explain how the ismember function work in this provided solution? Why it returns double instead of strings? Thanks in advance.


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function coded=caesar(c,n)

while x(a)<32 || x(a)>126


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

I am agree with @Wilver Sánchez's solution. I highly recommend avoiding to use "for loop" (it is not the wrong way, but it sometimes increases the processing time; maybe not for this code).

for ii = 1:length(unicodeMessage)

secret(secret < 32) = ...

Then, my solution is that:

function coded = caesar(M,s)

codes = highest - lowest + 1;

unicodeMessage = double(M);

secret = unicodeMessage + res;

secret(secret < 32) = 126 - (31 - secret(secret < 32));

secret(secret > 126) = 32 + (secret(secret > 126) - 127);


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

function out = caesar(m,n) m = double(m)+n; m = mod(m,95); m(m>126) = m(m>126)-126 +31; m(m<32) = m(m<32) -32+127; out = char(m);


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

%Help required

function coded = caesar(X,Y)

coded=fprintf('%s',code);

coded=fprintf('%s',code);

coded=fprintf('%s',code);


Which set of letters spells a valid word when decrypted using the caesar cipher with a key of 2?

Hi folks,

I write this code, this is working fine with the problem.

Need further improvement if any from seniors as I'm very novice in coding and MATLAB. It took almost 90 minutes to solve.

function coded = caesar(a,b)

See Also

Categories

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Can you use two keys in Caesar cipher?

Pick a key from 1 and 25; then for each character in your message, shift each letter forward by the key, wrapping around the end of the alphabet. For example, if your original message is "helloyou", and your key is 2, your encrypted message is "jgnnqaqw".

How do you decrypt a Caesar cipher?

To encrypt a message, enter the message in the Plaintext textbox, specify the shift, and click Encrypt. To decrypt a message, enter the message in the Ciphertext textbox, specify the shift, and click Decrypt.

Which cipher uses two keys?

Although symmetric key cryptography makes use of only one key, asymmetric key cryptography, also known as public key cryptography, utilizes two keys: a public key and a private key. The public key is used to encrypt data sent from the sender to the receiver and is shared with everyone.

What is correct code for Caesar cipher?

The encrypted message is "MDYDWSRLQW". Note that the Caesar cipher is monoalphabetic, so the same plaintext letters are encrypted as the same letters.