الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Edit This Code Output!

بدأه c++Beginner في 5 أبريل 2009 · 5 رد · 560 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

مرحبا

من خلال هذا الكود استطيع تشغيل البرنامج بهذه الطريقة ,

من خلال نافذة الاوامر CMD يتوجب علي كتابة الامر التالي ( e.exe -d c:\test.txt c:\test1.txt c:\key.txt ) (for decryption)


{

const string Useing = "Use it by this way [-e | -d] Dfile Sfile kfile ";

if(argc != 5)
{
cout << Using << endl;
return 1;
}
ifstream is(argv[3], ios::in | ios::binary);
if(!is)
{
cerr << "Input File Not Found Check it please" << endl;
return 2;
}

ifstream keyS(argv[4], ios::in | ios::binary);
if(!ks)
{
cerr << "Key File Not Found Check it please" << endl;
return 2;
}

AES aes;
const unsigned char *key = new unsigned char[16];

keyS.read((char*)key, 16);

ofstream os(argv[2], ios::out | ios::binary);

if(strcmp(argv[1], "-E") == 0 || strcmp(argv[1], "-e") == 0)
{
const unsigned char* cipherText;
const unsigned char* plainText = new unsigned char[16];
while(is.read((char*)plainText, 16))
{
cipherText = aes.Cipher(plainText, key, 16);
os.write((const char*)cipherText, 16);
}
}

if(strcmp(argv[1], "-D") == 0 || strcmp(argv[1], "-d") == 0)
{
const unsigned char* plainText;
const unsigned char* cipherText = new unsigned char[16];
while(is.read((char *)cipherText, 16))
{
plainText = aes.InvCipher(cipherText, key, 16);
os.write((const char*)plainText, 16);
}
delete[] cipherText;
}

delete[] key;

is.close();
keyS.close();
os.close();

return 0;
}
int main(int argc, char* argv[])

ولكنني أرغب بتشغيل البرنامج من خلال قائمة على الشكل التالي

من خلال نافذة الاوامر CMD كتابة اسم الملف E.exe

ومن ثم الحصول على قائمة بالشكل التالي

1- encrypt

2- decrypt

3- exit

بتمنى انكون تساعدوني بهالشي , انا حاولت بس ما زبطت معي.

تم تعديل هذه المشاركة بواسطة c++Beginner في 5 أبريل 2009 في 16:26

#2

وعليكــم السـلام ورحمة الله وبركاتـه..

عليك بحذف الargs وادخال اسامي الملفات يدويا ،، جرب هذا وقم بالتتغير بما يلزم :

int menu () {
	cout << "Encryption ..... [1]\n" 
		 << "Decryption ..... [2]\n"
		 << "Exit ........... [3]\n";
	int choice;
	cin >> choice;
	return choice;

}

int main(int argc, char* argv[])
{
bool state = true;
while ( state ) {
	switch ( menu() ) {
		case 1:
			// enter name of ifile & OFile & key
			// using cout & cin

			ifstream is(iFile, ios::in | ios::binary);
			if(!is)
			{
				cerr << "Input File Not Found Check it please" << endl;
				return 2;
			}

			ifstream keyS(kFile , ios::in | ios::binary);
			if(!ks)
			{
				cerr << "Key File Not Found Check it please" << endl;
				return 2;
			}

			AES aes;
			const unsigned char *key = new unsigned char[16];
			keyS.read((char*)key, 16);
			ofstream os(oFile, ios::out | ios::binary);
			const unsigned char* cipherText;
			const unsigned char* plainText = new unsigned char[16];

			while(is.read((char*)plainText, 16))
			{
				cipherText = aes.Cipher(plainText, key, 16);
				os.write((const char*)cipherText, 16);
			} 

			delete[] cipherText;
			delete[] key;

			is.close();
			keyS.close();
			os.close();
			break;

		case 2:
			// enter name of ifile & OFile & key
			// using cout & cin
			ifstream is(iFile, ios::in | ios::binary);
			if(!is)
			{
				cerr << "Input File Not Found Check it please" << endl;
				return 2;
			}

			ifstream keyS(kFile, ios::in | ios::binary);
			if(!ks)
			{
				cerr << "Key File Not Found Check it please" << endl;
				return 2;
			}

			AES aes;
			const unsigned char *key = new unsigned char[16];
			keyS.read((char*)key, 16);
			ofstream os(oFile, ios::out | ios::binary);
			const unsigned char* plainText;
			const unsigned char* cipherText = new unsigned char[16];

			while(is.read((char *)cipherText, 16))
			{
				plainText = aes.InvCipher(cipherText, key, 16);
				os.write((const char*)plainText, 16);
			} 

			delete[] cipherText;
			delete[] key;

			is.close();
			keyS.close();
			os.close();
			break;
		case 3:
			state = false; 
			break;
		default:
			cout << "Error";
	}

}
return 0;
}

يمكنك تحسين البرنامج وكتابه wrapper class ، بحيث تبعد مستخدم الكلاس عن التفاصيل ، بهذا الشكل :

int main(int argc, char* argv[])
{
	bool state = true;
	MyCipher mc;

	while ( state ) {
		switch ( menu() ) {
			case 1:
				// enter name of  iFile,dFile,kFile
				mc.encrpty(iFile,dFile,kFile);
				break;
			case 2:
				// enter name of  iFile,dFile,kFile
				mc.decrpt(iFile,dFile,kFile);
				break;
			default :
				// error
		}

	}
	return 0;
}

بالتوفيق :) .

http://informatic-ar.com منصة تعليمية عربية في علوم الحاسب والبرمجة

https://moalfat.com  للكتب الالكترونية والكورسات التعليمية

Everything we see now is just an engineering solution based on old science

#3

السلام عليكم

شكرا كتير على المساعدة اخي , ولكن خلال تعديل الكود واجهت الخطأ التالي:

error: jump to case label

int menu () {
	cout << "Encryption ..... [1]\n" 
		 << "Decryption ..... [2]\n"
		 << "Exit ........... [3]\n";
	int choice;
	cin >> choice;
	return choice;

}

int main(char *FileName1, char *FileName2, char *FileName3)
{

bool state = true;
while ( state ) {
	switch ( menu() ) {
		case 1:
			// enter name of ifile & OFile & key
			// using cout & cin
			cout << "Enter the input file name:  \n";
			cin >> FileName1;
			cout << "Enter the key file name:  \n";
			cin >> FileName2;
			cout << "Enter the output file name:  \n";
			cin >> FileName3;


			ifstream is(FileName1, ios::in | ios::binary);
			if(!is)
			{
				cerr << "Input File Not Found Check it please" << endl;
				return 0;
			}

			ifstream keyS(FileName2 , ios::in | ios::binary);
			if(!keyS)
			{
				cerr << "Key File Not Found Check it please" << endl;
				return 0;
			}

			AES aes;
			const unsigned char *key = new unsigned char[16];
			keyS.read((char*)key, 16);
			ofstream os(FileName3, ios::out | ios::binary);
			const unsigned char* cipherText;
			const unsigned char* plainText = new unsigned char[16];

			while(is.read((char*)plainText, 16))
			{
				cipherText = aes.Cipher(plainText, key, 16);
				os.write((const char*)cipherText, 16);
			} 

			delete[] cipherText;
			delete[] key;

			is.close();
			keyS.close();
			os.close();
			break;
case 2:
.
.
.
.
.
.

اتمنى توضيح هذا الخطا و توضيح فيما ان كان التعديل الذي قمت به صحيحا ما لا

وشكرا :)

#4

فكره التعديل صحيحه ، ورساله الخطأ غير واضحه ،، جرب أن تضع { } داخل كل case ، فقد تكون المشكله عند تعريف متغير في case على ما أذكر بعض المترجمات كانت تعطي رساله خطأ عند تعريف متغير في الcase ..

بالتوفيق :) .

http://informatic-ar.com منصة تعليمية عربية في علوم الحاسب والبرمجة

https://moalfat.com  للكتب الالكترونية والكورسات التعليمية

Everything we see now is just an engineering solution based on old science

#5
اقتباس
بعض المترجمات كانت تعطي رساله خطأ عند تعريف متغير في الcase

سوف تحصل على رسالة خطأ إذا قمت بعملية إسناد للمتغير عند تعريفه في الـ case بدون عمل بلوك جديد

هذا والله أعلم

والسلام عليكم

تم تعديل هذه المشاركة بواسطة mandareka في 8 أبريل 2009 في 01:12

دولة الخلافة الاسلامية في المخاض الاخير وميلادها قريب

حزب التحرير : التزم الطريق الصحيح الشرعي الوحيد لاعادة دولة الخلافة الاسلامية .

الامة الاسلامية متعطشة لقيادة مخلصة لتذوق حلاوة النصر ولن يكون ذلك الا بعودة الخلافة الاسلامية

الامة الاسلامية تحتاج لاحداث تسحقها سحقا وتمعسها معسا لتفكر تفكيرا جماعيا بالتغير لان وحدة المشاعر لوحدها لاتكفي وهذة الاحداث كلها خير

#6

السلام عليكم,

شكرا كتير أخ وجدي لمساعدتك , لقد قمت بوضع أقواس ونجح الامر , ولكن عند محاولة القيام إما بتشفير او بفك تشفير ملف , بعد تحديد مسار الملف المراد تشفيره او فك تشفيره تظهر رسالة خطا

An unhandled win32 exeption occured in Main.exe

do you want to debug using the selected debuger

والمشكلة ليست بمسار الملف , لانني حاولت ادخال احرف عشوائية وارقام وظهرت لي نفس رسالة الخطا ايضا.

:(

بصراحة بلشت اتعقد من السي ++ لانو كنت متوقع الموضوع يكون اسهل من هيك , بس صارلي فترة منيحة عم حاول وما كانت تزبط معي هالشغلة :(

شكرا كتير اخ mandareka

تم تعديل هذه المشاركة بواسطة c++Beginner في 8 أبريل 2009 في 08:03

مواضيع مشابهة