文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 自然科学 > 数学 > MATH2068_Number Theory and Cryptography_2013 Semester 2_MagmaProcedures
侵权投诉

MATH2068_Number Theory and Cryptography_2013 Semester 2_MagmaProcedures

University of Sydney_MATH2068_Number Theory and Cryptography_2013 Semester 2

Attach("m2068crypto.m");

"(Magma functions defined for use in MATH2068/2988)";
NewLogFile:=procedure(newname)
SetLogFile(newname);
Write("newlyspecifiedlogfilename.txt",newname: Overwrite:=true);
end procedure;

SetHistorySize(10000);
alphabet:=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];

FreqDist:=function(ct)
return [<FrequencyDistribution(ct)[i],alphabet[i]>: i in [1..26]];
end function;

SortedFreqDist:=function(ct)
x,pi:=Sort(FrequencyDistribution(ct),func<x,y|y-x>);
return [FreqDist(ct)[i^pi]: i in [1..26]];
end function;

SortedFreqDist:=function(ct)
fd:=FreqDist(ct);
sfd:=[];
for i := 1 to 26 do
_,M:=Max([fd[j,1]: j in [1..27-i]]);
sfd[i]:=fd[M];
Remove(~fd,M);
end for;
return sfd;
end function;

AlphToInt:=function(x)
return Index(alphabet,x);
end function;

IntToAlph:=function(x)
xx:= x mod 26;
if xx eq 0 then xx:=26;
end if;
return alphabet[xx];
end function;

typical:=[CartesianProduct(RealField(8),Parent("A"))| <0.12583125999,"E">, <0.09216541997,"T">, <0.08250207996,"A">, <0.07751454997,"O">, <0.07148794997,"S">, <0.06972152998,"I">, <0.06764338996,"N">, <0.05787613999,"H">, <0.05569409998,"R">, <0.04426433998,"L">, <0.03574396998,"D">, <0.02753532999,"M">, <0.02722360999,"U">, <0.022859519993,"C">, <0.022547799994,"G">, <0.021924359991,"Y">, <0.021716539992,"F">, <0.021300909994,"W">, <0.019638399989,"P">, <0.014339149993,"B">, <0.010806319996,"V">, <0.005714879997,"K">, <0.0011429799997,"J">, <0.0011429799997,"X">, <0.0009351599996,"Q">, <0.0007273499995,"Z">];

order:=[5, 20, 1, 15, 9, 14, 19, 8, 18, 4, 12, 21, 3, 13, 23, 6, 7, 25, 16, 2, 22, 11, 10, 24, 17, 26];

freqkey:=function(ct)
sfd:=SortedFreqDist(ct);
nums:=[AlphToInt(sfd[i,2]): i in [1..26]];
return &cat[IntToAlph(order[Index(nums,i)]): i in [1..26]];
end function;

swap:=procedure(x,y,~key);
sk:=[AlphToInt(String(key)[i]): i in [1..26]];
G:=Sym(26);
pi:=G!(Index(sk,AlphToInt(x)),Index(sk,AlphToInt(y)));
key:=Parent(key)!&cat[IntToAlph(sk[i^pi]):i in [1..26]];
end procedure;

rotateKey:=procedure(x,y,~key,ct);
sk:=[String(key)[i]: i in [1..26]];
fd:=SortedFreqDist(ct);
inc:=1;
for i:=1 to 26 do
if fd[i,2] eq x then
inc:=-1;
elif fd[i,2] eq y then
l:=i;
break;
end if;
end for;
nsk:=sk;
nsk[Index(sk,y)]:=x;
repeat
l+:=inc;
t:=fd[l,2];
nsk[Index(sk,t)]:=y;
y:=t;
until y eq x;
key:=Parent(key)!&cat nsk;
end procedure;

CheckPeriod:=procedure(text,seq,a,b);
if Type(text) eq CryptTxt then
t:=String(text);
else
t:=text;
end if;
for i in seq do
"Checking for transposition cipher period",i;
for j:=1 to i-1 do
for k:=j+1 to i do
if CoincidenceIndex(Decimation(t,[j,k],i)) gt a then
if CoincidenceDiscriminant(Decimation(t,[j,k],i)) gt b then
i,"is possible";
break j;
end if;
end if;
end for;
end for;
end for;

第1页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email