Vous êtes sur la page 1sur 2

TP Cryptographie Université de Batna 2

Master 2 CS Département d’informatique


Enseignant : HAMOUID Khaled Année universitaire 2019/2020
TP 1 : Corrigé type
Exercice 1 :
Dans les algorithmes de chiffrement/déchiffrement par substitution, on assigne un nombre entier à chaque
lettre de l’alphabet tq : a = 0, b = 1, …, z = 25

a) Ecrire une fonction strtonum(S) qui retourne un vecteur numérique associé au vecteur de lettres (S)
passée en paramètre de cette fonction, en respectant la représentation ci-dessus. Tester cette fonction
b) Ecrire une fonction numtostr(n) qui convertit un vecteur de nombre entier (n) compris entre 0..25
passé en paramètre, à un vecteur de lettres qui lui correspond dans l’alphabet. Tester cette fonction.

1. La fonction strtonum(S) 2. La fonction numtostr(n)

function y = strtonum (x) function y = numtostr (x)


% This function takes the letter in the % This function takes the number in the
string x and converts variable x and converts
% It to an integer. % It to a character y.
% The convention for this function is % The convention for this function is
% a --> 0 % a <-- 0
% b --> 1 % b <-- 1
% and so on... % and so on...
L=length(x); L=length(x);
for (i=1:L) for (i=1:L)
if ( (x(i) < 'a') | (x(i) > 'z') ), if ( (x(i) < 0) | (x(i) > 25) )
error('Text character out of range a- error('Integer out of range 0 - 26');
z'); end;
end; end
end y=char(x + 'a'); %It helps to know
y=x - 'a'; %It helps to know Matlab Matlab tricks
tricks

Exercice 2 :
Proposer une extension des fonctions précédentes pour prendre en charge tous les caractères à l’exception des
caractères de contrôle, en utilisant la notation ASCII-128 (voir table ASCII ci-dessous).

3. La fonction strtonumall 4. La fonction numtostrall

%Convert strings to vectors %Convert vectors to strings

function v=strtonumall(s) function s=numtostrall(x)


L=length(x);
L=length(s); for (i=1:L)
for (i=1:L) if ( (x(i) < 0) || (x(i) > 94) ), %
if ( (s(i) < ' ') || (s(i) > '~') ), all characters except control ones
% all characters except control ones error('Integer out of range of
error('caractère à convertir non ASCII 32 - 126');
valide') end;
end end
end s=char(x+' '); % space is the first
v=double(s-' '); character in ASCII table starting from 32
end end
TP Cryptographie Université de Batna 2
Master 2 CS Département d’informatique
Enseignant : HAMOUID Khaled Année universitaire 2019/2020

Exercice 3 :
On considère un algorithme de chiffrement par décalage de K lettres.

a) En utilisant les fonctions de l’exercice 2, écrire les fonctions de Chiffrement/Déchiffrement, shift (M,
K) et deshift(C, K).

5. La focntion shift: 6. La focntion deshift:

function c = shift(p,k) function p = deshift(c,k)


% This function performs the shift % This function performs the shift
encryption function encryption function
% c = p + k mod 95 % p = c - k mod 95
% We assume that m is a text string and k % We assume that c is a cipher text
is a number string and k is a number
% The result is kept in text % The result is kept in text
representation representation
if(isempty(p)) if(isempty(c))
error('texte en clair est vide'); error('texte chiffré est vide');
end end
if(k<1|k>94) xnum=strtonumall(c);
error('texte en clair est vide'); pnum = mod(xnum - k, 95);
end p=numtostrall(pnum);
xnum=strtonumall(p); end
cnum = mod(xnum + k, 95);
c=numtostrall(cnum);
end

b) En utilisant ces deux fonctions, écrire un Script qui chiffre/déchiffre un texte saisi au clavier par une
clé (nombre entier entre 1..94) saisie au clavier.

7. MonscriptE pour chiffrer 8. MonscriptD pour déchiffrer

%f=input('entrer la clé numérique entre 0 %f=input('entrer la clé numérique entre 0


et 94 : ','s');%% introduire la clé par et 94 : ','s');%% introduire la clé par
clavier clavier
%k=str2num(f); %k=str2num(f);
% verefier si la clé est une valeur % verefier si la clé est une valeur
numérique numérique
k=100; k=100;
while (k<0 || k>94) while (k<0 || k>94)
f=input('entrer la clé numérique f=input('entrer la clé numérique
entre 0 et 94 : ','s'); entre 0 et 94 : ','s');
if(isstrprop(f,'digit')) if(isstrprop(f,'digit'))
k=str2num(f); k=str2num(f);
end end
end end

f=input('entrer le texte à chiffrer : f=input('entrer le texte à déchiffrer :


','s'); ','s');

disp(['Voici le texte chiffré : ' disp(['Voici le texte en clair : '


shift(f,k)]); deshift(f,k)]);

Vous aimerez peut-être aussi