yasemin

15 Reputation

2 Badges

11 years, 188 days

MaplePrimes Activity


These are replies submitted by yasemin

@gkokovidis thank you very much:)

@Axel Vogt yes and it worked. but i didnt understand what is the difference between the codes in the book and these ones? i just found that in the freqlist function the frequencies is written different. but why i stil didnt understand :/

@Carl Love i fixed the quote marks but now, the output is  "aaaaaaaaaaaa" which is meaningless :(

@Carl Love thank you :) but i tried it too. but didnt change anyting, i still have the same error :/ you can see the all codes above. 

@Axel Vogt the book is "Introduction to Cryptography with Maple", José Luis Gómez Pardo. i tried many things like write it myself from the begining but didnt work.  VigenereKeyFind function once give aaaa as an output which is not true :/  you can see all the codes below : (by the way than you very much)

>alph := proc(language)
if language = en then convert([$97 .. 122], bytes)
elif language = es then Insert(convert([$97 .. 122], bytes), 14, "ñ")
else error "Unrecognized language"
end if;
end proc:

>evig := proc(keylist, textlist, language)

local l, alphabet, modulus;
l := nops(keylist);
alphabet := alph(language);
modulus := StringTools:-Length(alphabet);
[seq((textlist[i] + keylist[(i-1) mod l + 1]) mod modulus, i=1..nops(textlist))];
end proc:
dvig := proc(keylist, textlist, language)
evig(-keylist, textlist, language)
end proc:

> Vigenere := proc(key::string, message::string, action::identical(enc, dec),

{language := en})
local alphabet, keycodes, messagecodes, act, resultcodes;
alphabet := alph(language);
keycodes := map(x -> SearchText(x,alphabet)-1, StringTools:-Explode(key));
messagecodes := map(x -> SearchText(x,alphabet)-1, StringTools:-Explode(message));
if action = enc then
act := evig
elif action = dec then
act := dvig
else "Incorrect action"
end if;
resultcodes := act(keycodes, messagecodes, language);
StringTools:-Implode(map(x -> alphabet[x+1], resultcodes))
end proc:

> frequencies := proc(text, language := en)
local alphabet;
alphabet := alph(language);
[seq(StringTools:-CountCharacterOccurrences(text, l), l in alphabet)]
end proc:
> Ic := proc(text, language :=en )
local n, f;
n := StringTools:-Length(text);
f := frequencies(text, language);
evalf((ListTools:-DotProduct(f,f)-n)/(n*(n-1)));
end:

> freqlist := proc(language)
if language = en then
[0.0817, 0.0149, 0.0278, 0.0425, 0.1270, 0.0223, 0.0202, 0.0609, 0.0697,
0.0015, 0.0077, 0.0403, 0.0241, 0.0675, 0.0751, 0.0193, 0.0010, 0.0599,0.0633, 0.0906, 0.0276, 0.0098, 0.0236, 0.0015, 0.0197, 0.0007]

elif language = es then
[0.1174, 0.0117, 0.0436, 0.0474, 0.1389, 0.0073, 0.0096, 0.0098, 0.0713,
0.0031, 0.00024, 0.0535, 0.0294, 0.0713, 0.001, 0.0922, 0.0265, 0.0115,
0.0624, 0.0781, 0.0453, 0.043, 0.0109, 0.00006, 0.0021, 0.0085, 0.0033]
end if
end proc:

> Ind := proc(language)
add(iˆ2, i in freqlist(language)))
end proc:

> Ind(en);
0.06552226
> Ind(es);
0.0749428912

> c := "oltmhvurseaebwzchltyqwncnhouyqquvwaibcnwgwvofmrplupaujpoxlogautayzkmxzevec\
wbbsvmifdfucgmmshtijtyngepyyqohasfontvnwtrivwzcbepyatbsfejbiwmnavbioikqwlmmsttlw\
nfkfpjlmvuwzakqnztxqqugsejjlhimsobmlifdmtnclyevwsfrnhimsirmwvfdmqiodvarvgirygwok\
nylstylupaujcnwoeinxtb":

> partit:= (lis, long) -> [seq([seq(lis[(i-1)*long+t], i =
1..floor(((nops(lis)-t)/long)+1))],t = 1..long)]:

> indiceslist := proc(text, min, max)
uses StringTools;
local i, ind, ic, t;
i := min;
while i <= max do
ind := map(IndexOfCoincidence, map(Implode, partit(Explode(text), i)));
ic := evalf((sum(ind[t], t = 1 .. nops(ind)))/nops(ind));
i := i+1;
printf("%2d %9f\n", i-1, ic);
end do;
end proc:

> keylength := proc(text, max:=floor(StringTools:-Length(text)/15), {language:=en})
uses StringTools;
local i, j, k, l, m, t, ind;
j := 1;
k := 0;
m := 0;
i := 1;
ind := [];
l := max;
while i <= l and m < Ind(language)-0.002 do
ind := map(IndexOfCoincidence, map(Implode, partit(Explode(text), i)));
m := evalf((sum(ind[t], t = 1 .. nops(ind)))/nops(ind));
if k < m then
k := m;
j := i
end if;
i := i+1
end do;
j
end proc:

> findmin := lis -> ListTools:-Search(min(op(lis)), lis):

> frequencyanalysis := proc(lis, {language := en})
uses ListTools;
local f, s, alphabet, modulus;
f := freqlist(language);
alphabet := alph(language);
modulus := StringTools:-Length(alphabet);
s := seq(DotProduct(map(i -> 1/i, f), map(i -> iˆ2, x-f)),
x = seq(Rotate(lis, i), i = 0 .. modulus-1));
alphabet[findmin([s])]
end proc:

> relativefrequencies := proc(text, language)
evalf(frequencies(text, language)/StringTools:-Length(text))
end proc:
VigenereKeyFind := proc(ciphertext, max:=floor(StringTools:-Length(ciphertext)/15),
{language:=en})
uses StringTools;
local freqs;
freqs := map(x -> relativefrequencies(x, language),
map(Implode, partit(Explode(ciphertext),
keylength(ciphertext, max, ’:-language’=language))));
Implode(map(li -> frequencyanalysis(li, ’:-language’=language), freqs))
end proc:

> VigenereCryptanalysis := proc(ciphertext,
max:=floor(StringTools:-Length(ciphertext)/15),{language:=en})
Vigenere(VigenereKeyFind(ciphertext, max, ’:-language’=language), ciphertext, dec,
’:-language’=language)
end proc:

Page 1 of 1