non c pas sur vista ...c sur windows xp
Version imprimable
non c pas sur vista ...c sur windows xp
j'ai recommencé un autre projet(ca marche tjrs pas:roll: ) avec une seule form où l utilisateur rentre son fichier (à crypter) et son mon de passe qui va servir pour crypter
une fois il clik sur ok le fichier sera crypté
voici mon code ...:(
il me met comme erreur dans le aes.cpp
API call 'ImportFile' failed :le systeme ne peut pas trouver "stdafx.h"
maintenant j'ai les methodes crypter et decrypter et generate key directement dans ma form1 pour que je puisse utiliser sans problèmes
voici le code de la form1:
il me mets exception : Could not find file 'h:\Data\Visual Studio 2005\Projects\crypt_file\crypt_file\Saisir un fichier'.Code:
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 #pragma once namespace crypt_file { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Text; using namespace System::Security::Cryptography; using namespace System::IO; /// <summary> /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; protected: private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::TextBox^ textBox2; private: System::Windows::Forms::Label^ label2; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->label1 = (gcnew System::Windows::Forms::Label()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->textBox2 = (gcnew System::Windows::Forms::TextBox()); this->label2 = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(103, 225); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(79, 25); this->button1->TabIndex = 0; this->button1->Text = L"OK"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(53, 55); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(20, 13); this->label1->TabIndex = 1; this->label1->Text = L"file"; // // textBox1 // this->textBox1->Location = System::Drawing::Point(56, 162); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(170, 20); this->textBox1->TabIndex = 2; // // textBox2 // this->textBox2->Location = System::Drawing::Point(56, 81); this->textBox2->Name = L"textBox2"; this->textBox2->Size = System::Drawing::Size(170, 20); this->textBox2->TabIndex = 3; // // label2 // this->label2->AutoSize = true; this->label2->Location = System::Drawing::Point(53, 122); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(29, 13); this->label2->TabIndex = 4; this->label2->Text = L"pass"; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 270); this->Controls->Add(this->label2); this->Controls->Add(this->textBox2); this->Controls->Add(this->textBox1); this->Controls->Add(this->label1); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private : String ^val1; private : String ^val2; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { val1 = "Saisir un pass ..."; val2 = "Saisir un fichier"; textBox1->Text = val1; textBox2->Text = val2; } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { StreamReader ^sr = gcnew StreamReader(this->GetValue2()); StreamWriter ^sw = gcnew StreamWriter("Desktop\text0.doc"); try { String ^a = Crypter(sr->ReadToEnd(), this->GetValue1()); sw->Write(a); } catch (Exception^) { } finally { sr->Close(); sw->Close(); } } public: String ^GetValue1() { return val1; } public: String ^GetValue2() { return val2; } public: void GenerateKey(String ^SecretPhrase, array<unsigned char> ^&Key, array<unsigned char> ^&IV) { array<unsigned char> ^bytePhrase = Encoding::ASCII->GetBytes(SecretPhrase); SHA384Managed ^sha384 = gcnew SHA384Managed(); sha384->ComputeHash(bytePhrase); array<unsigned char> ^result = sha384->Hash; for (int loop = 0; loop < 24; loop++) Key[loop] = result[loop]; for (int loop = 24; loop < 40; loop++) IV[loop - 24] = result[loop]; } public: String ^ Crypter(String ^original, String ^keyPhrase) { array<unsigned char> ^Key = gcnew array<unsigned char>(24); array<unsigned char> ^IV = gcnew array<unsigned char>(16); GenerateKey(keyPhrase, Key, IV); ASCIIEncoding ^textConverter = gcnew ASCIIEncoding(); RijndaelManaged ^myRijndael = gcnew RijndaelManaged(); array<unsigned char> ^encrypted; array<unsigned char> ^toEncrypt; myRijndael->Key = Key; myRijndael->IV = IV; ICryptoTransform ^encryptor = myRijndael->CreateEncryptor(Key, IV); MemoryStream ^msEncrypt = gcnew MemoryStream(); CryptoStream ^csEncrypt = gcnew CryptoStream(msEncrypt, encryptor, CryptoStreamMode::Write); toEncrypt = textConverter->GetBytes(original); csEncrypt->Write(toEncrypt, 0, toEncrypt->Length); csEncrypt->FlushFinalBlock(); encrypted = msEncrypt->ToArray(); return Convert::ToBase64String(encrypted); } }; }
en fait il prend pas en compte ce que l utilisateur a rentrer, il cherche le fichier 'h:\Data\Visual Studio 2005\Projects\crypt_file\crypt_file\Saisir un fichier'.
en fait il prend en compte que les fichiers existents dans le repertoire du projet
je lui ai rentré le fichier :
D:\Data\Visual Studio 2005\Projects\crypt_file\crypt_file\test.txt
il me met une autre erreur :
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Illegal characters in path.
est ce que quelqu'un a une idée comment on resoud cette erreur ??
merci:aie:
il faut doubler les \ dans une chaine
merci..en fait je veux utiliser un algorithme à clé publique au lieu du AES (que vous m'avez donné) est ce que vous avez un code pour crypter des fichiers utilisant un algorithme à clé publique (RSA ou DSA )?
merci beaucoups:aie:
en fait par rapport au code de cryptage et decryptage avec l'algorithme aes:
si je prend un fichier test.txt je le crypt puis decrypt pour avoir test0.txt
normalement test.txt et test0.txt doivent être identiques ??
pourquoi test0.txt et test.txt n'ont pas la même taille ?
j'ai testé aussi de crypter un fichier zip(mais en changeant son extension en .txt) je l ai crypté et decrypté je le re-extrais ca marche pas , il m'affiche fichier corrompu !
est ce que t'as une idée comment je pourrais utiliser l'algorithme aes pour crypter et decrypter des fichiers de n'importe quelle autre type?
l'exemple que je t'ai donné avec rindjael fonctionne très bien, j'ai testé
ca te suffit pas ?
je l'ai utilisé mais il encrypte et decrypte que des fichiers text.
j'ai testé avec un fichier zip : prend par exemple un fichier test.zip crypte le et decrypte le ca te donnera par exemple un fichier test1.zip si tu veux extraire ce fichier test1.zip ca te donnera fichier corrompu !(j'ai testé aussi pour un fichier .doc ca marche pas non plus!)
est ce que tu as idée pour pouvoir utiliser l algo rijndael pour tous les types de fichiers (.zip .doc ..)?:aie:
Bonjour,
En fait ca marche que pour les fichiers .txt, si tu essaie pour les autres types de fichiers (.doc, .rar, .zip ) ca marche pas !:(
donc je crois que si on change les méthodes crypter et decrypter de telle sorte qu'elle donne des chaines de caractères au lieu de string peut etre ca marchera ...
j'ai essayé mais ca me donne un fichier decrypté vide
est ce que t a une idée comment utiliser le code que tu m'a donné pour crypter et decrypter tous les types de fichiers ?
merci :aie:
c'est parce qu'il faut utiliser un stream binaire
je te l'ai fait ...
à tout hasard :aie:
Code:
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 using namespace System; using namespace System::Text; using namespace System::Security::Cryptography; using namespace System::IO; void GenerateKey(String ^SecretPhrase, array<unsigned char> ^&Key, array<unsigned char> ^&IV) { array<unsigned char> ^bytePhrase = Encoding::ASCII->GetBytes(SecretPhrase); SHA384Managed ^sha384 = gcnew SHA384Managed(); sha384->ComputeHash(bytePhrase); array<unsigned char> ^result = sha384->Hash; for (int loop = 0; loop < 24; loop++) Key[loop] = result[loop]; for (int loop = 24; loop < 40; loop++) IV[loop - 24] = result[loop]; } array<unsigned char> ^ Crypter(array<unsigned char> ^encrypted, String ^keyPhrase) { array<unsigned char> ^Key = gcnew array<unsigned char>(24); array<unsigned char> ^IV = gcnew array<unsigned char>(16); GenerateKey(keyPhrase, Key, IV); ASCIIEncoding ^textConverter = gcnew ASCIIEncoding(); RijndaelManaged ^myRijndael = gcnew RijndaelManaged(); myRijndael->Key = Key; myRijndael->IV = IV; ICryptoTransform ^encryptor = myRijndael->CreateEncryptor(Key, IV); MemoryStream ^msEncrypt = gcnew MemoryStream(); CryptoStream ^csEncrypt = gcnew CryptoStream(msEncrypt, encryptor, CryptoStreamMode::Write); csEncrypt->Write(encrypted, 0, encrypted->Length); csEncrypt->FlushFinalBlock(); encrypted = msEncrypt->ToArray(); return encrypted; } array<unsigned char> ^ Decrypter(array<unsigned char> ^encrypted, String ^keyPhrase) { array<unsigned char> ^Key = gcnew array<unsigned char>(24); array<unsigned char> ^IV = gcnew array<unsigned char>(16); GenerateKey(keyPhrase, Key, IV); array<unsigned char> ^fromEncrypt; RijndaelManaged ^myRijndael = gcnew RijndaelManaged(); ASCIIEncoding ^textConverter = gcnew ASCIIEncoding(); myRijndael->Key = Key; myRijndael->IV = IV; ICryptoTransform ^decryptor = myRijndael->CreateDecryptor(Key, IV); MemoryStream ^msDecrypt = gcnew MemoryStream(encrypted); CryptoStream ^csDecrypt = gcnew CryptoStream(msDecrypt, decryptor, CryptoStreamMode::Read); fromEncrypt = gcnew array<unsigned char>(encrypted->Length); csDecrypt->Read(fromEncrypt, 0, fromEncrypt->Length); return fromEncrypt; } int main(array<System::String ^> ^args) { FileStream ^fs = gcnew FileStream("c:\\test.zip", FileMode::Open); BinaryReader ^br = gcnew BinaryReader(fs); FileStream ^fsw = gcnew FileStream("c:\\test.cry", FileMode::CreateNew); BinaryWriter ^bw = gcnew BinaryWriter(fsw); try { bw->Write(Crypter(br->ReadBytes((int)fs->Length), "code secret")); } catch (Exception^) { } finally { br->Close(); fs->Close(); bw->Close(); fsw->Close(); } fs = gcnew FileStream("c:\\test.cry", FileMode::Open); br = gcnew BinaryReader(fs); fsw = gcnew FileStream("c:\\test2.zip", FileMode::CreateNew); bw = gcnew BinaryWriter(fsw); try { bw->Write(Decrypter(br->ReadBytes((int)fs->Length), "code secret")); } catch (Exception^) { } finally { br->Close(); fs->Close(); bw->Close(); fsw->Close(); } return 0; }
merci beaucoups:king:
En fait j'aimerais utiliser un algorithme à clé publique (RSA ou DSA) pour pouvoir l'utiliser pour crypter le mot de passe (le code secret) qui sert à crypter et decrypter les fichiers avec Rijndael
est ce que t'as un code pour RSA ?
merci d'avance:aie:
hésite pas à regarder dans msdn http://msdn2.microsoft.com/en-us/lib...raphy.rsa.aspx
j y suis déja allé ..mais j'ai pas reussi :aie:
une autre question je veux aussi compresser des fichiers donc j'ai utilisé le code de cet faq :
http://dotnet.developpez.com/faq/cpp...ge=compression
mais ca n'a pas marché il m'affiche beaucoups d'erreurs, donc je me suis inspiré du code de rijndael que tu m'a donné, voici mon code :
ca me genere un fichier C:\\testcomp.gz mais quand je veuy l ouvrir il m'affiche une erreurs :(Code:
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 public: array<unsigned char> ^compressFile(array<unsigned char> ^compressed) { MemoryStream ^mscompress = gcnew MemoryStream(); GZipStream ^gscompress = gcnew GZipStream(mscompress, CompressionMode::Compress,false); gscompress->Write(compressed, 0, compressed->Length); // gscompress->FlushFinalBlock(); compressed = mscompress->ToArray(); return compressed; } int main(array<System::String ^> ^args) { FileStream ^fs = gcnew FileStream("C:\\test.txt", FileMode::Open); BinaryReader ^br = gcnew BinaryReader(fs); FileStream ^fsw = gcnew FileStream("C:\\testcomp.gz",FileMode::CreateNew); BinaryWriter ^bw = gcnew BinaryWriter(fsw); try { bw->Write(compressFile(br->ReadBytes((int)fs->Length))); } catch (Exception^) { } finally { br->Close(); fs->Close(); bw->Close(); fsw->Close(); } return 0; } __________________
est ce que tu pourrais jeter un coup d'oeil sur ce code et me dire ce qui va pas stp ?
merci d avance :king:
le code de la faq fonctionne
je l ai testé ca marche pas
ca me genere des erreurs
est ce que tu peux voir le code que j'ai fait et voir est ce qui cloche stp?
je me suis inspiré du code de la faq et du code de rijndal que tu m a donné
parce que j ai remarqué que dans le code de rijendal cést dans le main que tu fais le try alors que dans le code de la faq non et parce que le code rijndael ca marche bien chez moi!:aie:
merci
en fait le code de la faq ne marche po !je l 'ai essayé ca veut pas marcher
par contre le code que tu m a donné (Rijndael)marche !
donc je me suis dit que je vais m'inspirer du code de la faq et faire une fonction comress avec un main (comme le code de rijndael)
voici le code :
ca marche mais ca me genere un fichier .zp mais ke j arrive po a ouvrir (de taille 0 ko)Code:
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 array<unsigned char> ^compressFile(array<unsigned char> ^compressed) { MemoryStream ^mscompress = gcnew MemoryStream(); GZipStream ^gscompress = gcnew GZipStream(mscompress, CompressionMode::Compress,false); gscompress->Write(compressed, 0, compressed->Length); // gscompress->FlushFinalBlock(); compressed = mscompress->ToArray(); return compressed; } int main(array<System::String ^> ^args) { FileStream ^fs = gcnew FileStream("C:\\test.txt", FileMode::Open); BinaryReader ^br = gcnew BinaryReader(fs); FileStream ^fsw = gcnew FileStream("C:\\testcomp.gz",FileMode::CreateNew); BinaryWriter ^bw = gcnew BinaryWriter(fsw); try { bw->Write(compressFile(br->ReadBytes((int)fs->Length))); } catch (Exception^) { } finally { br->Close(); fs->Close(); bw->Close(); fsw->Close(); } return 0; }
est ce que tu peux regarder stp ?
merci :king:
je ne suis pas sur de comprendre
tu cherches à compresser un fichier encodé ?