Menu
 

Pour ouvrir le programme Maple permettant de crypter et de décrypter un message codé avec le système RSA cliquez sur le lien suivant :

CRYPTOGRAPHIE.mws


> restart;
Message, clés secrètes : p et q et clés publiques : n et e

> message:=bonjour;
P:=nextprime(997):
Q:=nextprime(2371):
N:=P*Q;

message := bonjour
N := 2398393

Les nombres p et q doivent absolument être premier

> if isprime(P)=true then print("P est premier") else print("P n'est pas premier")fi;
if isprime(Q)=true then print("Q est premier") else print("Q n'est pas premier") fi;

P est premier
Q est premier

Détermination de E

> LE:=[]:
for EE from 1 to P while nops(LE)<5 do
if igcd(EE,(P-1)*(Q-1))= 1 then LE:=[op(LE),EE] else "E n'est pas premier avec p-1 et q-1"
fi:
od;
LE;
E:=LE[2];

[1, 5, 13, 17, 19]
E := 5

Cryptage du message

Vérification des conditions

> if isprime(Q)=true and
isprime(P)=true and
igcd(E,(P-1)(Q-1))= 1
then "cryptage possible"
else "une condition n'est pas remplie"
fi;

cryptage possible

Assignation des lettres

> (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,_):=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27):

Cryptage

> lg:=length(message):

L0:=[]:
L1:=[]:
L10:=[]:
L11:=[a]:

nombre[0]:=0:
S:=1:
mess[0]:=message:

# les lettres sont associées 2 à 2 avant d'être cryptées

if type(message,integer)=true
then
L11:=[nombre]:
for kl from 1 to lg
do nombre[kl]:=trunc(mess[kl-1]/10^(lg-S));
mess[kl]:=(mess[kl-1]-nombre[kl]*10^(lg-S));
S:=S+1;
od;

for R from 1 to lg
do L10:=[op(L10),nombre[R]]:
L11:=[op(L11),modp((nombre[R])^E,N)]:
od:

print("nombre non crypté" =L10);
print("nombre crypté" =L11);
else

for R from 1 to lg
do Lettre[R]:=substring(message,R)
od;

if N > 2727
then
if type(lg/2,integer)=true
then
else Lettre[lg+1]:=Lettre[lg]:
Lettre[lg]:=0:
lg:=lg+1
fi:

for R from 1 to lg/2
do L0:=[op(L0),Lettre[2*R]+100*Lettre[2*R-1]]:
L1:=[op(L1),modp((Lettre[2*R]+100*Lettre[2*R-1])^E,N)]:
od:
else
for R from 1 to lg
do L0:=[op(L0),Lettre[R]]:
L1:=[op(L1),modp((Lettre[R])^E,N)]:
od:
fi;
print("message non crypté" =L0);
print("message crypté" =L1);
fi:

message non crypté = [215, 1410, 1521, 18 ]
message crypté = [1197190, 761023, 762350, 1889568 ]

Décryptage du message

Désassignation des lettres

> (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,_):=('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','_'):

Détermination de la clé d

> D1:=op(2,op(msolve(E*D=1,(P-1)*(Q-1))));
facD:=ifactor(D1);
for JK from 1 to nops(facD) do
if nops(op(JK,facD))=2
then CléD[JK]:=op(op(1,op(1,facD)))^op(2,op(1,facD));
else
CléD[JK]:=op(op(JK,facD))
fi;
od;

D1 := 147005

Facd := (5) (31) (73) (127)

Décryptage

> L2:=[]:
L3:=[]:
L12:=[]: #pour les chiffres
L13:=[nomb]:

if op(1,L11)=nombre
then
opL:=nops(L11):
for R from 2 to opL
do ME[R]:=modp(op(R,L11)^D1,N);
od:

L13:=[nombre,seq(ME[Rt],Rt=2..opL)];
print(L13);

else
opL:=nops(L1):
opL1:=nops(ifactor(D1));
opF:=ifactor(D1);
for R from 1 to opL
do ME[R]:=modp(op(R,L1)^(op(op(1,opF))),N);
for RL from 2 to opL1 do
ME[R]:=modp(ME[R]^(op(op(RL,opF))),N);
od:
od:

if N>2727

then

L2:=[seq([Z[trunc(ME[RT]/100)],Z[ME[RT]-trunc(ME[RT]/100)*100]],RT=1..opL)];
L3:=[seq(op(L2[ER]),ER=1..opL)]:

if L3[2*opL-1]=Z[0]
then L3[2*opL-1]:=op(2*opL,L3);
opL:=opL-1/2
else
fi;
else L3:=[seq(Z[ME[Rt]],Rt=1..opL)]
fi;
print(L2);
print(L3);
print("message décrypté");
fi:

[[Z 2, Z 15], [Z 14, Z 10], [Z 15, Z 21], [Z 0, Z 18]]

[Z 2, Z 15, Z 14, Z 10, Z 15, Z 21, Z 18, Z 18 ]

message décrypté

Réassignation

> (Z[1],Z[2],Z[3],Z[4],Z[5],Z[6],Z[7],Z[8],Z[9],Z[10],Z[11],Z[12],Z[13],Z[14],Z[15],Z[16],Z[17],Z[18],Z[19],Z[20],Z[21],Z[22],Z[23],Z[24],Z[25],Z[26],Z[27]):=(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,_):

Lecture du message décrypté

> mess1:=NULL:
mess2:=NULL:
if op(1,L13)=nombre
then

for S from 2 to opL
do
mess1:=cat(mess1,L13[S])
od:

print("message"=mess1);

else

if N>2727
then opL:=2*opL
else
fi:
for S from 1 to opL
do
mess1:=cat(mess1,L3[S])
od:

print("message"=mess1);
fi:

message = bonjour

Outils Maple

Trouver un nombre premier entre n et p (n et p 2 réels)

> Premier:=[]:
for N from 340 to 350
do
if isprime(N)=true
then Premier:=[op(Premier),N]
else
fi:
od:
Premier;

[347, 349 ]

Pour trouver facilement un nombre premier
> npremier:=nextprime(34567);
isprime(npremier);

npremier := 34583

true

Trouver les facteurs premiers d'un réel R
ifactor(234);

(2) (3)² (13)

           Accueil