加密和解密(java)

时间:2012-07-30 03:15:25

标签: java encryption

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

但我有一些错误。 在(str)上,它一直在说启动变量。 当我纠正它是

String st,str = null;

并运行,它给了我“错误:无法找到或加载主类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));
}
}

5 个答案:

答案 0 :(得分:1)

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

这让我觉得您的问题与您尝试启动该计划的方式有关。您的班级名称为EncryptAndDecrypt,但错误表明您指定的是encryptingfile

此外,批评:
使用main方法声明抛出异常通常是一种不好的做法。您应该始终在设置try / catch块并了解您正在编写的代码抛出的异常类型。

答案 1 :(得分:0)

当然你想要:

String st;
String str = "";

答案 2 :(得分:0)

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

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

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

或者,让st保持未初始化状态,因为它将由br.readLine()电话设置。由你决定。我更喜欢将所有声明的字符串设置为“”以避免NullPointerExceptions ...但我只是通常执行此操作,具体取决于我的情况。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

将类设为public并将空值赋给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();}
    }

}