2

已改编此代码 - http://www.roseindia.net/answers/viewqa/Java-Beginners/7551-encryption-and-decryption.html

但我有一些错误。在 (str) 上,它一直说要启动变量。当我把它改正为

字符串 st,str = null;

并运行,它给了我“错误:无法找到或加载主类 tryoutEncryption.encryptingfile”

包试用加密;

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));
}
}
4

5 回答 5

1

它给了我“错误:无法找到或加载主类 tryoutEncryption.encryptingfile”

这给我的印象是您的问题在于您尝试启动程序的方式。您的班级名称是EncryptAndDecrypt,但错误表明您正在指定encryptingfile

另外,一个批评:
让你的main方法声明抛出的异常通常是一种不好的做法。您应该始终练习设置 try/catch 块并了解您正在编写的代码会引发哪些类型的异常。

于 2012-07-30T03:57:16.987 回答
0

你当然想要:

String st;
String str = "";
于 2012-07-30T03:48:14.223 回答
0

@你好我试过你的代码,它对我来说很好。请提供您获得的异常跟踪,以便我们为您提供帮助。

于 2012-07-30T04:48:04.323 回答
0

对于错误“错误:无法找到或加载主类 tryoutEncryption.encryptingfile”,这意味着您拼错了需要在java命令中运行的 java 类(如果这是来自命令行),或者您正在使用一些IDE 中损坏的运行时配置(如果您正在使用)。

此外,在一行中声明两个变量是非常糟糕的风格。有些人可能会鼓励它以节省您需要键入的代码量并减少行数,但这样做可能会导致难以检测的错误,例如未初始化的变量。另外,我认为在一行中声明多个变量是难以阅读的代码(太混乱了)。根据您的情况,我建议:

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

或者,不要st初始化,因为它将由br.readLine()调用设置。由你决定。我更喜欢将我声明的所有字符串设置为“”以避免 NullPointerExceptions……但我通常只这样做,这取决于我的情况。`

于 2012-07-30T04:01:22.667 回答
0

公开类并将空值分配给 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();}
    }

}
于 2017-07-06T12:25:55.187 回答