gpt4 book ai didi

java - 加密和解密(java)

转载 作者:行者123 更新时间:2023-11-30 11:42:27 25 4
gpt4 key购买 nike

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

但我遇到了一些错误。在 (str) 上,它一直说要启动变量。当我将其更正为

字符串 st,str = null;

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

封装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));
}
}

最佳答案

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

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

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

关于java - 加密和解密(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11715019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com