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

طلب شرح كود تشفير وفك التشفير

مغلق
بدأه شاب مسلم في 21 أغسطس 2005 · 1 رد · 716 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

الاخوة الكرام

قام احد الاخوة جزاه الله كل خير بوضع كود للتشفير وفك التشفير

ولقد راسلته على بريده الخاص حتى يشرح لنا

ثم طلبت من الاخوة الكرام ان يشرحوه لنا

حيث اني مهتم جدا بهذا النوع من البرامج ( اعني التشفير )

ولهذا فتحت هذا الموضوع

واتمنى من الاخوة الافاضل ان يشرحوا لي هذا الكود سطر سطر

حتى تعم الفائده

ودمتم في امان الله

وهذا هو الكود

//***********************************************************************************
//****	Simple File Encryption With PassWord Program          ****
//****           Written By: Mohammed Maasher                       ****
//****	Hodiedh Universty - Engnerring Section 2005 -Yemen ****
//***********************************************************************************	

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;



//*******************************************************
//***** Encryption Main Class *********************
//*******************************************************
class Encryption {

	static final int MAXBLOCK=2048; 	 //2KB Bufferd

	//*********************************************************
	//*** FileEnc Method: Used To Encrypt a File ***
	//*********************************************************
	static void FileEnc(String strSrc,String strDes,String strPw) throws IOException 
	{
  int intI=0;
  long lngI=0;
  
  byte intPw[]=new byte[strPw.length()];
  for (intI=0;intI<=strPw.length()-1;intI++)
   intPw[intI]=(byte) strPw.charAt(intI);
  
    
  File fSrc=new File(strSrc);
  FileInputStream fisSrc=new FileInputStream(fSrc);
  FileOutputStream fosDes=new FileOutputStream(strDes);
   	 
 	 byte bytEncPw[]=new byte[intPw.length];
  for(intI=0;intI<=bytEncPw.length-1;intI++)
   bytEncPw[intI]=(byte) (intPw[intI] ^ 200);
  
  Integer intLen=new Integer(strPw.length());
  byte bytPL[]=new byte[2];

  if (intLen.toString().length()<2){
 	 bytPL[0]=(byte) 48;    //48 is Ascii of 0
 	 bytPL[1]=(byte) (intLen.toString().charAt(0));
  }
  else{
 	 bytPL[0]=(byte) (intLen.toString().charAt(0));
 	 bytPL[1]=(byte) (intLen.toString().charAt(1));  
  }
  
  fosDes.write(bytPL);
  fosDes.write(bytEncPw);
  
  byte bytBlock[]=new byte[MAXBLOCK];
  
  for (lngI=1;lngI<=(fSrc.length()/MAXBLOCK);lngI++){
 	 fisSrc.read(bytBlock);
 	 EncBlock(bytBlock,intPw);
 	 fosDes.write(bytBlock);
  }
  
  int intRemind=(int) (fSrc.length() % MAXBLOCK);
  if (intRemind !=0 ){
 	 bytBlock=new byte[intRemind];
 	 fisSrc.read(bytBlock);
 	 EncBlock(bytBlock,intPw);
 	 fosDes.write(bytBlock);
  }
  
  fisSrc.close();
  fosDes.close();
	}


	//**********************************************************
	//*** FileDec Method: Used To Dencrypt a File ***
	//**********************************************************
	static int FileDec(String strSrc,String strDes,String strPw1) throws IOException
	{
  
  int intI=0;
  long lngI=0;
  int intPL=0;
  byte bytPL[]=new byte[2];
  byte bytEncPw[];
  byte intPw[];
  
 	 
  File fSrc=new File(strSrc);	
  FileInputStream fisSrc=new FileInputStream(fSrc);
  
  
  fisSrc.read(bytPL);
  intPL=Integer.parseInt(new String(bytPL));
 	 
  bytEncPw=new byte[intPL];
  fisSrc.read(bytEncPw);
  
  intPw=new byte[intPL];
  for (intI=0;intI<=intPL-1;intI++)
   intPw[intI]=(byte) (bytEncPw[intI] ^ 200);  //200 is Encrypt
                  // Key of Password	
  String strPw2=new String(intPw);
  if (! strPw2.equals(strPw1))
   return -1;       	 //Password dosn't Match
   
   
  FileOutputStream fosDes=new FileOutputStream(strDes);
  
  long lngFileSize=fSrc.length()-(2+intPL); 
  byte bytBlock[]=new byte[MAXBLOCK];  
  for (lngI=1;lngI<=(lngFileSize/MAXBLOCK);lngI++){
 	 fisSrc.read(bytBlock);
 	 EncBlock(bytBlock,intPw);
 	 fosDes.write(bytBlock);
  }
  
  int intRemind=(int) (lngFileSize % MAXBLOCK);
  if (intRemind !=0 ){
 	 bytBlock=new byte[intRemind];
 	 fisSrc.read(bytBlock);
 	 EncBlock(bytBlock,intPw);
 	 fosDes.write(bytBlock);
  }
  
  fisSrc.close();
  fosDes.close(); 
  
  return 0;
	}
   

	//*******************************************************************
	//** EncBlock Method: Used To Encrypt a Byte Block **
	//*******************************************************************
	private static void EncBlock(byte bytA[],byte intB[])
	{
  int intL=0;
  for(int intI=0;intI<=bytA.length-1;intI++)
  {
 	 bytA[intI]=(byte) (bytA[intI] ^ intB[intL]);
 	 
 	 if (intL>=intB.length-1)
    intL=0;
 	 else
    intL++;
    
     } 
	}     
}  


//****************************************************
//***** Main Class ********************************
//***************************************************


class Enc2 extends JFrame implements ActionListener {

	private JLabel jlSrc=new JLabel("Source File:       ");
	private JLabel jlDes=new JLabel("Destnation File: ");
	private JLabel jlPw=new JLabel("Password: ");

	private JButton jbBrowse1=new JButton("Browse");
	private JButton jbBrowse2=new JButton("Browse");
	private JButton jbEnc=new JButton("Encrypt");
	private JButton jbDec=new JButton("Dencrypt");
	private JButton jbAbout=new JButton("About..");

	private JTextField jtSrc=new JTextField();
	private JTextField jtDes=new JTextField();
	private JTextField jtPw=new JTextField();

	public Enc2(){
  //Init the main frame
  super("Simple File Encryption >>");
  setSize(450,180);
  
  JPanel jpContent=(JPanel) getContentPane();
  
  jpContent.setLayout(new FlowLayout());
  
  //Inti The Components
  jtSrc.setColumns(20);
  jtDes.setColumns(20);
  jtPw.setColumns(10);
  
  jbEnc.setBackground(new Color(255,255,223));
  jbDec.setBackground(new Color(255,130,130));
  jbAbout.setBackground(new Color(235,235,235));
  
  jbBrowse1.addActionListener(this);
  jbBrowse2.addActionListener(this);
  jbEnc.addActionListener(this);
  jbDec.addActionListener(this);
  jbAbout.addActionListener(this);
  
  //Add The Compnents to main Panel
  jpContent.add(jlSrc);
  jpContent.add(jtSrc);
  jpContent.add(jbBrowse1);
  
  jpContent.add(jlDes);
  jpContent.add(jtDes);
  jpContent.add(jbBrowse2);
  
  jpContent.add(jlPw);
  jpContent.add(jtPw);
  jpContent.add(jbEnc);
  jpContent.add(jbDec);
  
  JPanel jpAbout=new JPanel();
  jpContent.add(jpAbout);
  jpAbout.add(jbAbout);
  
  }	
  
	//***************************************************************
	//**** Action Events Handllers Method *****************
	//***************************************************************
	public void actionPerformed (ActionEvent e){
  
  // Event of Browse1
  if (e.getSource()==jbBrowse1)
  {
 	 JFileChooser jfSrc=new JFileChooser();
 	 int intRet=jfSrc.showOpenDialog(Enc2.this);
 	 if (intRet==jfSrc.APPROVE_OPTION){
    jtSrc.setText(jfSrc.getSelectedFile().getAbsolutePath());
 	 }
  }
  
  // Event of Browse2
  else if (e.getSource()==jbBrowse2)
  {
 	 JFileChooser jfSrc=new JFileChooser();
 	 int intRet=jfSrc.showSaveDialog(Enc2.this);
 	 if (intRet==jfSrc.APPROVE_OPTION){
    jtDes.setText(jfSrc.getSelectedFile().getAbsolutePath());
 	 }
  }
  
  // Event of About
  else if(e.getSource()==jbAbout){
 	 JOptionPane joMsg=new JOptionPane();
 	 joMsg.showMessageDialog(joMsg,
    "Simple File Encryption Program \n" +
    "Written by : Mohammed Maasher & Adnan Al-Khader \n" +
    "Hodiedh Universty - 2005"); 
  }
  
  // Event of Encrypt
  else if(e.getSource()==jbEnc){
 	 
 	 String strSrc=jtSrc.getText();
 	 String strDes=jtDes.getText();
 	 String strPw=jtPw.getText();
 	 
 	 if (strSrc.equals("") | strDes.equals("") | strPw.equals("")){
    JOptionPane joMsg=new JOptionPane();
    joMsg.showMessageDialog(joMsg,"Error,You Must Fill All Fields!"); 
    return;
 	 }
 	 
 	 try{
 	 Encryption.FileEnc(strSrc,strDes,strPw);
 	 JOptionPane joMsg=new JOptionPane();
 	 joMsg.showMessageDialog(joMsg,"OK , Encryption Done!"); 
 	 }
 	 catch(IOException ex){
    System.out.println(""+e);
 	 }
  }
  
  // Event of Decrypt
  else if(e.getSource()==jbDec){
 	 
 	 String strSrc=jtSrc.getText();
 	 String strDes=jtDes.getText();
 	 String strPw=jtPw.getText();
 	 
 	 if (strSrc.equals("") | strDes.equals("") | strPw.equals("")){
    JOptionPane joMsg=new JOptionPane();
    joMsg.showMessageDialog(joMsg,"Error,You Must Fill All Fields!"); 
    return;
 	 }
 	 
 	 try{
 	 int intRet=Encryption.FileDec(strSrc,strDes,strPw);
 	 JOptionPane joMsg=new JOptionPane();
 	 if (intRet != -1)
    joMsg.showMessageDialog(joMsg,"OK , Dencryption Done!"); 
 	 else
    joMsg.showMessageDialog(joMsg,"Error , Inviald Password"); 
 	 }
 	 catch(IOException ex){
    System.out.println(""+e);
 	 }
   }  
	}

  
	//*************************************************************
	//******* main Method ************************************
	//*************************************************************
	public static void main(String a[])  throws IOException {
    
  new Enc2().setVisible(true);
	}	
}
#2

هل سؤالي وطلبي صعب لهذه الدرجه

اكثر من 30 زيارة ولا يوجد رد واحد

هذا الموضوع مغلق.

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