Please help to encrypt and decrypt file using RSA.

J2EE, JSP, JSF, VisualWeb-Pack usw.

Moderator: wegus

genius010
Beiträge: 1
Registriert: 29.05.2017, 17:04

Please help to encrypt and decrypt file using RSA.

Beitragvon genius010 » 29.05.2017, 17:15

hello

am not sure if this the write section to post it here

please could anybody help me am using RSA algorithm to encrypt and decrypt file on C drive the encryption is working but the decryption is not

this code to encrypt and its working

Code: Alles auswählen

import java.io.*;
import java.util.*;
class readFile
{
 static public String getContents(File aFile) {
   
    StringBuilder contents = new StringBuilder();
    try {
      BufferedReader input =  new BufferedReader
(new FileReader(aFile));
      try {
        String line = null;       
while (( line = input.readLine()) != null){
          contents.append(line);
         contents.append(System.getProperty
("line.separator"));
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
    }
    return contents.toString();
  }
 static void enc(String text, File aFile,int k)
 {
        int key = k; /* this is for key */
        String plainText = text;
        char character;
try{
        System.out.print("Encrypted sentence is: ");
  FileWriter fstream = new FileWriter(aFile);
  BufferedWriter out = new BufferedWriter(fstream);
        for (int i = 0; i< plainText.length(); i++) {
            character = plainText.charAt(i);
            if (character != ' ') {
character = (char) ('a' + (character - 'a' + key) % 26); /* check for key it is number or character */
    out.append(character);
  System.out.print(character);
            }
        }
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());/* Catch any errors */
  }           
}

public void readFile()
{
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter File Name");/*this message came after run the program and choose what we want to do and write the name for encrypt */
    String name = sc.nextLine();
    System.out.println("Enter the key");/* write the key you want  */
    int key = sc.nextInt();
    String Fname = "C:\\ahmed\\"+name+".txt";
    String Ename = "C:\\ahmed\\"+name+"e.txt";/* this is th path for encrypt file */
    File testFile = new File(Fname);
    File eFile = new File(Ename);
    System.out.println("Plain Text: ");/* this is  decrypt message*/
   String ptext=getContents(testFile);
   System.out.println(ptext);
   enc(ptext,eFile,key);   
}
}




the decryption is not working :roll:

Code: Alles auswählen

import java.io.*;
import java.util.*;
class DecFile
{
 static public String getContents(File aFile) {
   
    StringBuilder contents = new StringBuilder();
    try {
      BufferedReader input =  new BufferedReader
(new FileReader(aFile));
      try {
        String line = null;       
while (( line = input.readLine()) != null){
          contents.append(line);
         contents.append(System.getProperty
("line.separator"));
        }
      }
      finally {
        input.close();
      }
    }
    catch (IOException ex){
    }
    return contents.toString();
  }
 static void dec(String text, File aFile,int k)  /* it is what void using in the decrypt */
 {
        int key = -k; 
        String plainText = text;
        char character;
try{
        System.out.print("Decrypted sentence is: "); /* this message came after doing decrypt with decrypt sentence */
  FileWriter fstream = new FileWriter(aFile);
  BufferedWriter out = new BufferedWriter(fstream);
        for (int i = 0; i< plainText.length(); i++) {
            character = plainText.charAt(i);
            if (character != ' ') {
character = (char) ('a' + (character - 'a' + key) % 26);
    out.append(character);
  System.out.print(character);
            }
        }
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());/* Catch any errors */
  }           
}

 public void DecFile()
 {
     Scanner sc = new Scanner(System.in);
     System.out.println("Enter File Name");/*this message came after run the program and choose what we want to do and write the name for decrypt */
     String name = sc.nextLine();
     System.out.println("Enter the key");/* write the  same key you using to encrypt  */
     int key = sc.nextInt();
     
     String Fname = "C:\\ahmed\\"+name+".txt";/* this is th path for encrypt file */
    String Ename = "C:\\ahmed\\"+name+"d.txt";/* this is th path for decrypt file */
 
     File tFile = new File(Fname);
     File eFile = new File(Ename);
     System.out.println("Decrypted text: ");/* this message came after doing decrypt with decrypt sentence */
    String DESX=getContents(tFile);
    System.out.println(DESX);
    dec(DESX,eFile,key);
   
 }
}





please i need help

Zurück zu „NetBeans - Enterprise“

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 2 Gäste