有人需要我和我的公共RSA密钥。给了我一个java程序来做一个密钥对。我对Java没有经验,所以非常简单的事情对我来说非常困难。该计划如下:
import java.io.IOException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class CustomRSAKeyPairGenerator{
public void generate() {
try {
KeyPairGenerator objlRSAKeyPairGen =
KeyPairGenerator.getInstance("RSA");
objlRSAKeyPairGen.initialize(2048);
KeyPair objlRSAKeyPair = objlRSAKeyPairGen.generateKeyPair();
RSAPublicKey objlPublicKey = (RSAPublicKey) objlRSAKeyPair.getPublic();
RSAPrivateKey objlPrivateKey = (RSAPrivateKey) objlRSAKeyPair.getPrivate();
StringBuffer strblPublicKey = new StringBuffer();
strblPublicKey.append(objlPublicKey.getModulus().toString(16).toUpperCase());
strblPublicKey.append('~');
strblPublicKey.append(objlPublicKey.getPublicExponent().toString(16).toUpperCase()
);
System.out.println(strblPublicKey.toString());
StringBuffer strblPrivateKey = new StringBuffer();
strblPrivateKey.append(objlPrivateKey.getModulus().toString(16).toUpperCase());
strblPrivateKey.append('~');
strblPrivateKey.append(objlPrivateKey.getPrivateExponent().toString(16).toUpperCase());
System.out.println(strblPrivateKey.toString());
}catch (NoSuchAlgorithmException noSuchAlgrtm){
System.out.println(noSuchAlgrtm.getMessage());
}
}
}
当我编译它时,我得到一个名为“CustomRSAKeyPairGenerator.class”的文件,所以我就这样运行:
mylogin$java CustomRSAKeyPairGenerator
我收到此错误:
Exception in thread "main" java.lang.NoClassDefFoundError: CustomRSAKeyPairGenerator/class
Caused by: java.lang.ClassNotFoundException: CustomRSAKeyPairGenerator.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
我是否运行此错误或者我需要修复的java源代码存在问题?有没有办法用它来制作我需要的RSA密钥对?