> restart: # Umwandlungen > bits2number := proc( X ) > local x,i; > x:=0: > for i to nops(X) do > x:=2*x+X[nops(X)+1-i]; > od; > x; > end: > number2bits := proc( xx, l ) > local X,x; > x:=xx; > X:=NULL; > to l do > x:=iquo( x, 2, 'r' ); > X:=(X,r); > od; > if x<>0 then print("Warnung: Informationsverlust! ",x); fi; > [eval(X)]; > end: > bytes2number := proc( X ) > local x,i; > x:=0: > for i to nops(X) do > x:=256*x+X[nops(X)+1-i]; > od; > x; > end: > number2bytes := proc( xx, l ) > local X,x; > x:=xx; > X:=NULL; > to l do > x:=iquo( x, 256, 'r' ); > X:=(X,r); > od; > if x<>0 then print("Warnung: Informationsverlust! ",x); fi; > [eval(X)]; > end: > words2number := proc( X ) > local x,i; > x:=0: > for i to nops(X) do > x:=256^2*x+X[nops(X)+1-i]; > od; > x; > end: > number2words := proc( xx, l ) > local X,x; > x:=xx; > X:=NULL; > to l do > x:=iquo( x, 256^2, 'r' ); > X:=(X,r); > od; > if x<>0 then print("Warnung: Informationsverlust! ",x); fi; > [eval(X)]; > end: # Prozeduren > erzeuge_Rundenschluessel:=proc(k) > global R,K; > local p,i,j; > K:=k; > R:=array(1..9,1..6): > p:=0: > for i to 9 do > for j to 6 do > R[i,j]:=K mod 2^16; > K:=K * 2^112 mod (2^128-1); > p:=p+1; > if p=128/16 then > p:=0: > K:=K*2^25 mod (2^128-1): > fi; > od; > od; > eval(R); > end: > mult16:=proc(x,y) > local X,Y,Z; > X:=x; Y:=y; > if X=0 then X:=2^16; fi; > if Y=0 then Y:=2^16; fi; > Z:=(X*Y) mod (2^16+1); > if Z=2^16 then Z:=0; fi; > Z; > end: > inv16:=proc(x) > local X,Z; > X:=x; > if X=0 then X:=2^16; fi; > Z:=X&^(-1) mod (2^16+1); > if Z=2^16 then Z:=0; fi; > Z; > end: > xor16:=proc(x,y) > local i,X,Y,Z; > X:=number2bits(x,16); > Y:=number2bits(y,16); > Z:=NULL; > for i to 16 do > Z:=eval(Z),(X[i]+Y[i])mod 2; > od; > bits2number([eval(Z)]); > end: > sum16:=proc(x,y) > (x+y) mod 2^16; > end: > encrypt:=proc(x) > global R; > local i,y,Y,Z,a,b,c; > Y:=number2words(x,4); > Z:=[0,0,0,0]; > for i to 9 do > Z[1]:= mult16( Y[1] , R[i,1] ); > Z[2]:= sum16( Y[2] , R[i,2] ); > Z[3]:= sum16( Y[3] , R[i,3] ); > Z[4]:= mult16( Y[4] , R[i,4] ); > if (i=9) then break; fi; > a:= mult16( xor16( Z[1], Z[3] ), R[i,5] ); > b:= mult16( sum16( xor16( Z[2], Z[4] ), a ), R[i,6] ); > c:= sum16( a, b ); > Y[1]:= xor16( Z[1], b ); > Y[2]:= xor16( Z[3], b ); > Y[3]:= xor16( Z[2], c ); > Y[4]:= xor16( Z[4], c ); > od; > words2number(Z); > end: > encryptfile:=proc(infile,outfile) > local st,INPUT,OUTPUT,x,y,X,Y; > st:=time(): > INPUT:=fopen(infile,READ,BINARY); > OUTPUT:=fopen(outfile,WRITE,BINARY); > do > X:=readbytes( INPUT, 8 ); > if X=0 then break; fi; > x:=bytes2number( X ); > y:=encrypt( x ); > Y:=number2bytes( y, 8 ); > writebytes( OUTPUT, Y ); > od: > fclose(INPUT),fclose(OUTPUT); > time()-st; > end: > # Test > isprime(2^16+1); > erzeuge_Rundenschluessel(1); > rand(2^64)(); > decrypt(%); > encrypt(%); > KEY:="7654321GEHEIMNIS"; > K:=bytes2number(convert(KEY,bytes)): > iquo(%,2^56) - bytes2number(convert("GEHEIMNIS",bytes)); > erzeuge_Rundenschluessel(K); > FOLDER:="MYFOLDER/": > decryptfile("".FOLDER."ideaklar.txt","".FOLDER."ideaklar.crypt"); > encryptfile("".FOLDER."ideaklar.crypt","".FOLDER."ideaklar.decrypt"); >