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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
{C'est un simple example utilisant une routine encryptant/decryptant chaque
chaine de caracteres (ligne) Passée en Paramétre.Les Caractéres
CR(13)/LF(10) Reste Unencryptés.ET Cette routine suppose que la chaine
à encrypter ne comprend pas un caractere de haut bit (Inférieur a 127).
C'est pour cette raison on génere une séquence de nombre aléatoire comprise
entre 0 est 128-1 :)
}
Program Crypt_Text;
Var
N : Byte;
Function Crypt(Var S : String) : String;
Var
i : Byte;
Begin
Crypt[0] := S[0];
For i := 1 to Length(S) DO
Crypt[i] := Chr(Ord(S[i]) Xor (Random(128) Or 128));
End; {Crypt}
Function Make_Key(S : String) : Longint;
Var
i : Byte;
Key : Longint;
Begin
Key := 0;
For i := 1 to Length(S) Do
Key := Key + Ord(S[i]);
Make_Key := Key;
End; {Make_Key}
Function Crypt_Txt_File(I_File,O_File,Pwd : String) : Byte;
{Returned Value :
0 : Successful
1 : Source and target are the same
2 : cannot open source file
3 : Unnable to creat target file
}
Var
In_File,
Out_File : Text;
St : String;
Begin
If I_File = O_File Then
Begin
Crypt_Txt_File := 1;
Exit;
End;
RandSeed := Make_Key(Pwd);
Assign(In_File,I_File);
{$I-}
Reset(In_File);
{$I+}
If IOResult <> 0 Then
Begin
Crypt_Txt_File := 2;
Exit;
End;
Assign(Out_File,O_File);
{$I-}
Rewrite(Out_File);
{$I+}
If IOResult <> 0 Then
Begin
Crypt_Txt_File := 3;
Exit;
End;
While Not EOF(In_File) Do
Begin
Readln(In_File,St);
Writeln(Out_File,Crypt(St));
End;
Close(In_File);
Close(Out_File);
Crypt_Txt_File := 0;
End; {Crypt_Txt_File}
Begin
If ParamCount < 3 Then
Begin
Writeln(ParamStr(0),' : Too Few Argument');
Writeln('Use : ',ParamStr(0) ,
' [Source File] [Destination File] [Key]');
Halt;
End; {Cond1}
If ParamCount > 3 Then
Begin
Writeln(ParamStr(0),' : Too Many Argument');
Writeln('Use : ',ParamStr(0) ,
' [Source File] [Destination File] [Key]');
Halt;
End;{Cond2}
N := Crypt_Txt_File(ParamStr(1),ParamStr(2),ParamStr(3));
Case N Of
0 : Writeln('Successful');
1 : Writeln('Source and target are the same');
2 : Writeln('2 : cannot open source file');
3 : Writeln('3 : Unnable to creat target file');
End; {Case.. Of}
End. {Main}
{
RandSeed c'est la graine (Seed). Si on l'initialise à une valeur
Constante Alore random génere la meme séquence des nombres aléatoires.
Mais Comment Ca se Passe :(
Patience Je vais tout expliquer :)
Tout d'abord génerer des nombres aléatoires sur ordinateur revient à Créer
Une Suite d'entiers :
I(n+1) = f(I(n))
Ou f est une fonction qui doit etre judicieusement choisit.
La Suite peut fournir M nombres aléatoires dans l'intervalle [0,M-1].
M dépend du type des entiers :
Entier 16 bits : M = 2^16 = 65 536;
M = 2^32 = 4 294 967 296
La Plus Part des compilateurs utilisent les générateurs Congruentiels
Ils sont du type :
I(n+1) = (a*I(n) + c) mod b
ou a,c et b sont deja prédéfinies; n est l'index
Maintenant Passant a la pratique (Ex en turbo pascal nombres pseudo_aleaoires)
Var
Seed : Longint;
Const
a = 16807;
c = 1;
b = 2147483647;
Procedure My_Randomize;
Begin
Seed := MemL[$0040:$006c]; (*timer ticks*)
End;
Function My_Random(N : Word) : Word;
Begin
Seed := (Seed*a +c) mod b;
My_Random := Seed mod N;
End;
BEGIN
My_randomize;
writeln(My_random(8));
END.
Maintenant si vous donner a seed une valeur constante vous obtiendrez la
méme séquence.
A+
}
C'est pas la meme chose mais ca peut y aller :wink: |
Partager