关于playfair密码的一点问题,请大家帮忙看一看(100分)

  • 主题发起人 主题发起人 xcj96
  • 开始时间 开始时间
X

xcj96

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,m,n,i1,i2,j1,j2,iLoop1, jLoop1,iLoop2, jLoop2: integer ;
s,s1,s2,s3,key,plaintext,ciphertext:string;
myarray1: array[0..4,0..22] of string;
myarray2: array[0..4,0..22] of string;


c1,c2:char;
begin

key:=edit1.text; //密钥输入
plaintext:=edit2.text; //明文输入

s1:='abcdefghiklmnopqrstuvwxyz'; //原字母表 及5*5矩阵
edit4.text :=s1;
for iLoop1 := 0 to 4 do
for jLoop1 := 0 to 4 do
myarray1[iLoop1, jLoop1] := S1;

for i:=1 to length(key) do //所有j都为i
begin
if key='j' then
key:='i';
end;
for i:=1 to length(plaintext) do
begin
if plaintext='j' then
plaintext:='i';
end;

s2:=key+s1; //后字母表 及5*5矩阵
s3:=s2;
for m:=1 to length(s3) do
begin
n:=length(s2);
while n>m do
begin
if s3[m]=s2[n] then
delete (s2,n,1) ;
n:=n-1;
end;
s3:=s2;
end;
edit5.Text:=s2;
for iLoop2 := 0 to 4 do
for jLoop2 := 0 to 4 do
myarray2[iLoop2, jLoop2] := S2;

for j:=1 to length(plaintext) do //取明文
c1:=plaintext[j];
c2:=plaintext[j+1];
j:=j+1;
if c1=c2 then
//两字母相同中间加q,明文长度为单,末尾加q
c2:='q';
if length(plaintext) mod 2=1 then
plaintext[length(plaintext)+1]:='q';

for iLoop2 := 0 to 4 do //加密
for jLoop2 := 0 to 4 do
begin
i1:= iLoop2 ;
i2:= iLoop2;
j1:= jLoop2;
j2:=jLoop2;
if (myarray2[i1, j1] = c1) and (myarray2[i2, j2] = c2) then


if i1=i2 then //同行 后移
s:=s+myarray2[i1, j1+1]+myarray2[i2, j2+1];
if j1=4 then //行末为行初
s:=s+ myarray2[i1, 0] +myarray2[i2, j2+1]
else if j2=4 then
s:=s+ myarray2[i1, j1+1] +myarray2[i2, 0]

else if j1=j2 then //同列下移
s:=s+myarray2[i1+1, j1]+myarray2[i2+1, j2];
if i1=4 then //列末为列初
s:=s+ myarray2[0, j1] +myarray2[i2+1, j2]
else if j2=4 then
s:=s+ myarray2[i1+1, j1] +myarray2[0, j2]

else s:=s+myarray2[i1, j2]+myarray2[i2, j1];
//不同行不同列 为 它所在的行是该字母所在行,列则是另一个字母的所在列。


ciphertext:=s;
edit3.text:=ciphertext;
end;

end;

end.

我输入的密钥为key,明文为ab,输出的密文却是keyabcdfghilmnopqrstuvwxz的n倍,即为后字母表的n倍
请大虾帮我看看那里出了问题
还有怎样使用listbox才能显示5*5矩阵阿 用listbox1.Items.Add()好像不行。谢谢阿
 

Similar threads

I
回复
0
查看
577
import
I
I
回复
0
查看
503
import
I
I
回复
0
查看
630
import
I
后退
顶部