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

برنامج تشفير رقم بطاقه الصراف

بدأه who me في 16 أبريل 2010 · 12 رد · 2,291 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

بسم الله الرحمن الرحيم

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

حبيت اسألكم عن برنامجي انو اليوزر يدخل الcreditcard no ويشفره بالمفتاح السري عن طريق DES ونشفر المفتاح السري والرساله بالpublic key

ونرسله لكلاس server بملف txt ونفك تشفير بprivate key وبعدين نفك تشفير الcredit card number بالمفتاح السري

طبعا حيكون عندي ملفين public.txt وprivate.txt نخزن فيهم المفتاحين العام والخاص طبعا كان عندي كذا سؤال

بدايه بكود server

1-كيف اخزن المفتاحين كل واحد بملف واناديهم مره ثانيه في الكلاسserver

/**
 * @(#)Text2.java
 *
 *
 * @author
 * @version 1.00 2010/4/13
 */
import java.security.*;
import java.util.Scanner;
import java.io.*;

public class Server {

    public static void main(String [] args)throws Exception {
 //Create the KeyPair then store public key in File1 and private key in File2.
     KeyPairGenerator generator =KeyPairGenerator.getInstance("RSA");
   	 generator.initialize(1024);
   	 KeyPair myPair=generator.generateKeyPair();
   	 myPair.getPublic();
   	 myPair.getPrivate();

    }
public String decrypt(encrypted key,encrypted cardNo){
//	Get private key from File2

// Create asymmetric cipher (RSA)
Cipher c=Cipher.getInstance("RSA");
 //Initialize cipher for decryption with private key
c.init(Cipher.ENCRYPT_MODE,myPair.getPrivative());
// Decrypt symmetric key

 //Create symmetric cipher

 //Initialize Cipher for decryption with symmetric key

//Decrypt credit card number with symmetric Cipher

//Print on the screen the decrypted credit card and the decrypted key


}

}

وكود الclient

non-static method encrypt(java.lang.String) cannot be referenced from a static context

cannot find symbol method init(int,java.io.BufferedReader)

هذي هي الاخطاء اللي طلعتلي فيه

/**
 * @(#)Text1.java
 *
 *
 * @author
 * @version 1.00 2010/4/13
 */
import javax.crypto.*;
import java.security.*;
import java.util.Scanner;
import java.io.*;
public class Client {

public static void main(String[] args){

    	String cardNo=new String();
    	Scanner scan=new Scanner(System.in);
        System.out.println("Enter your credit card number :");
        cardNo =scan.nextLine();

       encrypt(cardNo);


    }
    private void encrypt(String cardNo){//create cipher
    	Cipher c=Cipher.getInstance("DES");
    	// generate the key
    	KeyGenerator kg=KeyGenerator.getInstance("DES");
        Key sKey =kg.generateKey();
        // Initialize cipher for encryption with session key
        c.init(Cipher.ENCRYPT_MODE,sKey);
        //Encrypt credit card number with cipher
         byte[] encyptsbytes=c.doFinal(cardNo.getBytes());
         //Get public key from File1
        FileInputStream f =new FileInputStream("public.txt");
    	DataInputStream d =new DataInputStream (f);
    	InputStreamReader I= new InputStreamReader (d);
    	BufferedReader b=new BufferedReader(I);

    // Create cipher for asymmetric encryption (RSA)
     Cipher c2=Cipher.getInstance("RSA");
      //Initialize cipher for encryption with public key
       c2.init(Cipher.ENCRYPT_MODE,b);
       // Encrypt symmetric key
       //??
        byte[] encyptr=c.doFinal(encyptsbytes);
       // Save encrypted credit card number and session key to File3
       FileWriter f0=new FileWriter("file3.txt");
       for(int i=0;i<encyptr.length;i++){

       f0.write(encyptr);}
       f0.close();


}
}

ياريت احــــــــــــد يشرحلي ويعلمني ايش الاخطاء اللي وقعت فيها لأنو صراحه احس الفكره معقده

وجزيتم خيرا

#2

أضف static للدالة encrypt

أو قم بعمل كائن object من الفئة Client ثم استخدم الدالة encrypt بدلالة هذا الكائن

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3

طيب كيف اخزن public key في ملف public.txt

وprivate key في ملف private.txt

وبعدين اناديهم في كلاس ؟؟

ومشكور ع المــــــــــــــرور

#4

اطلع على المواضيع المثبتة

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#5
علاء الصالحي كتب:

أقصد محاولاتك في كتابة ملف بعد أن قرأتي هذا الموضوع

تحياتي

اسفه ع التأخير حاولت اكتب بعد لما قرات الموضوع وكمان لما قرات من مواقع تانيه

عشان كذا تأخرت في الرد

انا عندي اخطاء في method signature ياليت تنتبه لها :excl:

هذا كود الServer

/**
 * @(#)Text2.java
 *
 *
 * @author
 * @version 1.00 2010/4/13
 */
import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.security.KeyFactory;
 import java.security.KeyPair;
 import java.security.KeyPairGenerator;
 import java.security.PrivateKey;
 import java.security.PublicKey;
 import java.security.spec.PKCS8EncodedKeySpec;
 import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;

public class Server {

    public static void main(String [] args)throws Exception {
 //Create the KeyPair then store public key in File1 and private key in File2.
     KeyPairGenerator generator =KeyPairGenerator.getInstance("RSA");
   	 generator.initialize(1024);
   	 KeyPair myPair=generator.generateKeyPair();
   	 PublicKey pubKey = myPair.getPublic();
   	 PrivateKey priKey = myPair.getPrivate();
   	 // store each key in its file
   	 //Public Key
   	 FileOutputStream pubF = new FileOutputStream("public.txt");
   	 byte[] k1 = pubKey.getEncoded();
   	 pubF.write(k);
   	 pubF.close();
   	 //Private Key
   	  FileOutputStream priF = new FileOutputStream("private.txt");
   	 byte[] k2 = priKey.getEncoded();
   	 priF.write(k);
   	 priF.close();




    }
public void decrypt()throws Exception{
//	Get private key from File2
FileInputStream priKS = new FileInputStream("private.txt");
byte[] priKeyByte = new byte[128];
priKS.read(priKeyByte);
priKS.close();
//============================================
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(priKeyByte);
PrivateKey priKey = kf.generatePrivate(priKeySpec);

// Create asymmetric cipher (RSA)
Cipher c=Cipher.getInstance("RSA");
 //Initialize cipher for decryption with private key
c.init(Cipher.DECRYPT_MODE,priKey);
// Decrypt symmetric key
byte[] cipher = new byte[128];
byte[]  keys = new byte[128];
FileInputStream f= new FileInputStream("file3.txt");
f.read(cipher);
keys = c.doFinal(cipher);

 //Create symmetric cipher
 Cipher c2=Cipher.getInstance("DES");

 //Initialize Cipher for decryption with symmetric key
 c2.init(Cipher.DECRYPT_MODE,sKey);

//Decrypt credit card number with symmetric Cipher
byte[] cardNo=c2.doFinal(c2.getBytes());
//Print on the screen the decrypted credit card and the decrypted key


System.out.print(new String(keys));


//=============================================

	System.out.print(new String (cardNo));

}

وهذا كود الClient

/**
 * @(#)Text1.java
 *
 *
 * @author
 * @version 1.00 2010/4/13
 */
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.security.KeyFactory;
 import java.security.KeyPair;
 import java.security.KeyPairGenerator;
 import java.security.PrivateKey;
 import java.security.PublicKey;
 import java.security.spec.PKCS8EncodedKeySpec;
 import java.security.spec.X509EncodedKeySpec;
 import javax.crypto.*;
import java.security.*;
import javax.crypto.Cipher;
import java.util.Scanner;
public class Client {

public static void main(String[] args){

    	String cardNo=new String();
    	Scanner scan=new Scanner(System.in);
        System.out.println("Enter your credit card number :");
        cardNo =scan.nextLine();
       // Client cli= new Client();
       //cli.encrypt(cardNo);


    }
    private void encrypt(String cardNo)throws Exception{//create cipher
    	Cipher c=Cipher.getInstance("DES");
    	// generate the key
    	KeyGenerator kg=KeyGenerator.getInstance("DES");
        Key sKey =kg.generateKey();
        // Initialize cipher for encryption with session key
        c.init(Cipher.ENCRYPT_MODE,sKey);
        //Encrypt credit card number with cipher
         byte[] encyptsbytes=c.doFinal(cardNo.getBytes());
         //Get public key from File1
        FileInputStream pubKS= new FileInputStream("public.txt");
        byte[] pubKeyByte = new byte[128];
        pubKS.read(pubKeyByte);
        pubKS.close();
        KeyFactory kf= KeyFactory.getInstance("RSA");
        X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyByte);
        PublicKey pubKey = kf.generatePublic(pubKeySpec);


    // Create cipher for asymmetric encryption (RSA)
     Cipher c2=Cipher.getInstance("RSA");
      //Initialize cipher for encryption with public key
       c2.init(Cipher.ENCRYPT_MODE,pubKey);
       // Encrypt symmetric key

        byte[] encyptr=c2.doFinal(encyptsbytes);
       // Save encrypted credit card number and session key to File3
      FileOutputStream f0=new FileOutputStream("file3.txt");
       f0.write(encyptr);
       f0.close();


}
}

وجزيتم خــــــــــــــــــــــيرا ان شاء اللـــــــــــــــهـ

تم تعديل هذه المشاركة بواسطة علاء الصالحي في 21 أبريل 2010 في 22:24

#6

ألقي نظرة على هذا الموضوع

/index.php?showtopic=125547

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#7

يا أختي ما رأيك أن تقومي أولاً بكتابة شيفرة بسيطة في البداية تقوم بعملية الكتابة على ملف

ثم بعد هذا نفكر في عملية إضافتها إلى الشيفرة الخاصة بك

لكي أكون صادقاً معك

فأنا لن أقوم بكتابة أي شيفرة لك

أنا سأساعدك على كتابة شيفرتك

لذا اطرحي مشاكلك بالتفصيل فأنا لا أقوم بترجمة الملفات compile إلى في حالة الضرورة

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#8

دحين مشكلتي مو في تشفير في نفس method signature

يعني مثلا

private void encrypt(String cardNo)

كيف انادي الميثود في main تبع class client

عشانه private فاكيد له طريق انا حاليا ما اعرفها ؟؟

وياليت تساعدني في هذا الشيئ ..بس

#9

لم أستطع أن أصل إلى هدفك

أعيدي الصياغة من فضلك

وياليت لو تتأكدي من الكتابة بالفصحى وتتأكدي من خلو الكلام من الأخطاء الإملائية

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#10

طيب حأكتب بالفصحى مع اني لا اتقنها جيدا

في class client

اريد ان استدعي (يعني انادي ) الميثود encrypt في main

كيف استدعيها ؟؟

اذا كمان ما فهمت ..هل استطيع كتابة ما اريد بالانجليزي ؟؟

#11

حتى تنادي دالة تباعة للكائن non static method في دالة تابعة للفئة static method

فإنه يجب أن تقومي بعمل كائن من الفئة ثم مناداة الدالة

في حالتنا نقوم عمل كائن من الفئة عميل client

Client cli= new Client();

ثم تتم مناداة الدالة encrypt

cli.encrypt(cardNo);

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#12

جمعت الكلاسات كلها في كلاس واحد

/**
 * @(#)Text1.java
 *
 *
 * @author
 * @version 1.00 2010/4/26
 */
import java.io.*;
import java.util.Scanner;
 import java.security.KeyFactory;
 import java.security.KeyPair;
 import java.security.KeyPairGenerator;
 import java.security.PrivateKey;
 import java.security.PublicKey;
 import java.security.spec.PKCS8EncodedKeySpec;
 import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;


public class CCN {
	public CCN()throws Exception{

//Create the KeyPair then store public key in public file and private key in private.
     KeyPairGenerator generator =KeyPairGenerator.getInstance("RSA");
   	 generator.initialize(1024);
   	 KeyPair myPair=generator.generateKeyPair();
   	 PublicKey pubKey = myPair.getPublic();
   	 PrivateKey priKey = myPair.getPrivate();
   	 // store each key in its file
   	 //Public Key

   	 byte[] k1 = pubKey.getEncoded();
   	        DataOutputStream pubF = new DataOutputStream(new FileOutputStream("public.txt"));

   	 pubF.write(k1);
   	 pubF.close();
   	 //Private Key

   	 byte[] k2 = priKey.getEncoded();
   	           DataOutputStream priF = new DataOutputStream(new FileOutputStream("private.txt"));

   	 priF.write(k2);
   	 priF.close();}


 private void encrypt(String cardNo)throws Exception{
    	//create cipher
    	Cipher c=Cipher.getInstance("DES");
    	// generate the key
    	KeyGenerator kg=KeyGenerator.getInstance("DES");
        Key sKey =kg.generateKey();
        // Initialize cipher for encryption with session key
        c.init(Cipher.ENCRYPT_MODE,sKey);
        //Encrypt credit card number with cipher
         byte[] encyptsbytes=c.doFinal(cardNo.getBytes());
         System.out.println(new String(encyptsbytes));

        DataOutputStream f = new DataOutputStream(new FileOutputStream("file3.txt"));
       f.write(encyptsbytes);
       f.close();
         //Get public key from File

      DataInputStream pubKS = new DataInputStream(new FileInputStream("public.txt"));

        byte[] pubKeyByte = new byte[128]; // 1024 bits

        pubKS.read(pubKeyByte);

        pubKS.close();
System.out.println(new String(pubKeyByte));


        KeyFactory kf =KeyFactory.getInstance("RSA");
       X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyByte);

       PublicKey pubKey = kf.generatePublic(pubKeySpec);

    // Create cipher for asymmetric encryption (RSA)
     Cipher c2=Cipher.getInstance("RSA");
      //Initialize cipher for encryption with public key
       c2.init(Cipher.ENCRYPT_MODE,pubKey);
       // Encrypt symmetric key
         byte [] encrypSk=sKey.getEncoded();
        byte[] encyptr=c2.doFinal(encrypSk);

       System.out.println(new String(encyptr));
       // Save  session key to File4
      DataOutputStream f0 = new DataOutputStream(new FileOutputStream("file4.txt"));
       f0.write(encyptr);
       f0.close();




}
public void decrypt()throws Exception{
//	Get private key from File2
 DataInputStream priKS = new DataInputStream(new FileInputStream("private.txt"));
byte[] priKeyByte = new byte[128];
priKS.read(priKeyByte);
priKS.close();
//============================================
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(priKeyByte);
PrivateKey priKey = kf.generatePrivate(priKeySpec);

// Create asymmetric cipher (RSA)
Cipher c=Cipher.getInstance("RSA");
 //Initialize cipher for decryption with private key
c.init(Cipher.DECRYPT_MODE,priKey);
// Decrypt symmetric key
        DataInputStream ff = new DataInputStream(new FileInputStream("file3.txt"));
byte[] encrypSk = new byte[128];
ff.read(encrypSk);
ff.close();
byte[] decrypSK = c.doFinal(encrypSk);
SecretKeySpec skeySpec = new SecretKeySpec(decrypSK, "DES");
 //Create symmetric cipher
 Cipher c2=Cipher.getInstance("DES");

 //Initialize Cipher for decryption with symmetric key
 c2.init(Cipher.DECRYPT_MODE,skeySpec);
//read encryption card number from file 4
 DataInputStream f4 = new DataInputStream(new FileInputStream("file4.txt"));
byte[] encrypCardNo = new byte[128];
f4.read(encrypCardNo);
f4.close();
//Decrypt credit card number with symmetric Cipher
byte[] decrypCardNo=c2.doFinal(encrypCardNo);
//Print on the screen the decrypted credit card and the decrypted key


System.out.print(new String(decrypSK));


//=============================================

	System.out.print(new String (decrypCardNo));

}
    public static void main(String [] args)throws Exception {
    	String cardNo=new String();
    	Scanner scan=new Scanner(System.in);
        System.out.println("Enter your credit card number :");
        cardNo =scan.nextLine();
       CCN cli= new CCN();
       //CCN server= new CCN();
       cli.encrypt(cardNo);
      cli.decrypt( );

    }


}

وطلعتلي هاذ الاخطاء مو عارفه ايش معناها ضروري احد يرد عليا بسررررررررررررررررررررررررعه

--------------------Configuration: <Default>--------------------

Enter your credit card number :

12341234

قZ‏.لy\Pڑ ےpأqµ

0پں0

*†H†÷

پچ 0پ‰پپ ·ط¯¹ث5ن\r·ƒN±ـ¶_<[Aw–’م(‏1مN´ىm†<¢؛\{ےهْYڑç?ضQٍت•—,َ“t±kؤnoٍh¶خش‏Pw{ٌد´§%ڑ«دھI¥?}u›

Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF

at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:188)

at java.security.KeyFactory.generatePublic(KeyFactory.java:304)

at CCN.encrypt(CCN.java:79)

at CCN.main(CCN.java:152)

Caused by: java.security.InvalidKeyException: IOException: Detect premature EOF

at sun.security.x509.X509Key.decode(X509Key.java:380)

at sun.security.x509.X509Key.decode(X509Key.java:386)

at sun.security.rsa.RSAPublicKeyImpl.<init>(RSAPublicKeyImpl.java:66)

at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:281)

at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:184)

... 3 more

Process completed.

#13

لا علم لي أختي بالتشفير

يمكنك أن تسألي الأخ وجدي

على الأغلب لديه معلومات أكثر

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

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