简体   繁体   中英

Encryption and Decryption (java)

have adapted this codes from - http://www.roseindia.net/answers/viewqa/Java-Beginners/7551-encryption-and-decryption.html

but i'm having some error. on the (str) , it keeps saying to initiate variable. and when i corrected it to be

String st,str = null;

and run, it gives me "Error: Could not find or load main class tryoutEncryption.encryptingfile"

package tryoutEncryption;

import java.io.*;
import java.security.*;
import javax.crypto.*;

class EncryptAndDecrypt {

public static void main (String[] args) throws Exception{
KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keygenerator.initialize(1024, random);

KeyPair keypair = keygenerator.generateKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);
BufferedReader br=new BufferedReader(new FileReader(new File("C:\\Users\\Desktop\\testing.txt")));
String st,str;
while((st=br.readLine()) != null)   {
    str+=st+" ";
}


byte[] cleartext = null;
cleartext = str.getBytes();
byte[] ciphertext = null;
ciphertext = cipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + ciphertext.toString());

cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cleartext1 = cipher.doFinal(ciphertext);
System.out.println("the decrypted cleartext is: " + new String(cleartext1));
}
}

it gives me "Error: Could not find or load main class tryoutEncryption.encryptingfile"

This gives me the impression that your problem is in the way you are trying to start the program. Your class name is EncryptAndDecrypt , but the error suggests that you are specifying encryptingfile .

Also, a critique:
Having your main method declare thrown Exceptions is generally a bad practice. You should always be in practice of setting try/catch blocks and understanding what sorts of exceptions are thrown by the code you are writing.

Certainly you want:

String st;
String str = "";

For the error "Error: Could not find or load main class tryoutEncryption.encryptingfile", it means you are either misspelling the java class that you need to run in your java command (if this is from the command line), or you are using some corrupted runtime configurations in your IDE (if you are using one).

Also, it is very bad style to declare two variables in one line. Some people may encourage it in order to save the amount of code you need to type and reduce line count, but doing so may cause hard-to-detect bugs, such as uninitialized variables. Also, I would argue that declaring multiple variables in one line is hard-to-read code (too much clutter). For your situation, I would recommend:

String st = "";
String str = "";

Or, leave st uninitialized because it will be set by the br.readLine() call. Up to you. I prefer to set all my declared strings to "" in order to avoid NullPointerExceptions...but I only usually do this, depending on my situation.`

@Hello I tried your code and it works fine for me. Kindly give the exception trace you are getting, So that we can help you out.

make class public and assign empty value to String

import java.io.*;
import java.security.*;
import javax.crypto.*;

public class EncryptAndDecrypt
{


    public static void main(String[] args) 
    {
        try{
        KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        keygenerator.initialize(1024, random);

        KeyPair keypair = keygenerator.generateKeyPair();
        PrivateKey privateKey = keypair.getPrivate();
        PublicKey publicKey = keypair.getPublic();
        Cipher cipher = Cipher.getInstance("RSA");

        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Users\\Desktop\\testing.txt")));
        String st="", str="";
        while ((st = br.readLine()) != null)
        {
            str += st + " ";
        }

        byte[] cleartext = null;
        cleartext = str.getBytes();
        byte[] ciphertext = null;
        ciphertext = cipher.doFinal(cleartext);
        System.out.println("the encrypted text is: " + ciphertext.toString());

        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] cleartext1 = cipher.doFinal(ciphertext);
        System.out.println("the decrypted cleartext is: " + new String(cleartext1));
        }catch(Exception e){e.printStackTrace();}
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM