Matlab read Strings as arrays for transposition cipher -


i need use matlab implementing transposition algorithm. let's have variable called plaintext='qwerty........zxcv' (doesn't matter text is, alphabet should containd a-z , space characters) , variable called

lof = length(plaintext) %. (the size of message) 

also, have following transposition(stored in variable tp):

1 2 3 4 5 5 3 1 4 2  tp = '5 3 1 4 2'; 

i thought of using variable buffer , ltp = length(tp). in variable buffer read ltp characters time, till got no characters in plaintext. so, need suggestion how read ltp letters plaintext @ time. also, if 'buffer' variable has let's 5 chars, should use 'for' loop interchanging char on respective index? or there better solution letter permutation.

i appreciate help.

if understand want correctly, split text 5 character chunks. perform permutation of these chunks using transposition variable, tp , reassemble these chunks create ciphertext encrypt plaintext. therefore, characters @ positions 1 - 5 each chunk rearranged such that:

source --> target    1         5    2         3    3         1    4         4    5         2 

based on see, let's start simplest way. can use for loop suggested. use loop , create values start @ index 1 , skip on amount of characters - equal length of tp. use these indices slice character array , perform permutation of characters. make sure create output array same size input, permutation each chunk.

something this:

%// define plain text plaintext = 'hello name ray nice meet you';  %// define permutation vector tp = [5 3 1 4 2]; ltp = numel(tp); %// define length of transposition lp = numel(plaintext); %// length of plaintext  %// declare ciphertext array ciphertext = char(zeros(1,lp));  %// pad array spaces when grab chunks, don't %// go out of bounds plaintext = [plaintext 32*ones(1,ltp*ceil(lp/ltp) - lp)];  %// each chunk... idx = 1 : ltp : lp     %// plain text chunk     tx = plaintext(idx : idx+ltp-1);      %// permute chunk , place output array     ciphertext(idx : idx+ltp-1) = tx(tp); end 

so plain text is:

plaintext = 'hello name ray nice meet you'; 

the output is:

ciphertext =   olhleny  miea myrsa ei cnmo  tyte e  o u 

a more vectorized solution (in case you're curious) reshape array number of rows matches length of transposition vector , number of columns number of chunks decompose text, once pad array don't go out of bounds. way, each column represents unique chunk. permute rows of matrix , reshape back.

something this:

%// define plain text plaintext = 'hello name ray nice meet you';  %// define permutation vector tp = [5 3 1 4 2]; ltp = numel(tp); %// define length of transposition lp = numel(plaintext); %// length of plaintext  %// pad array spaces when grab chunks, don't %// go out of bounds plaintext = [plaintext 32*ones(1,ltp*ceil(lp/ltp) - lp)];  %// begin vectorized solution ciphertext = reshape(plaintext, ltp, []); ciphertext = reshape(ciphertext(tp, :), 1, []); 

we still see in our first attempt:

ciphertext =  olhleny  miea myrsa ei cnmo  tyte e  o u 

Comments