Reputation: 310
I try to send with ObjectOutpuStream this RSA custom Object that I have done Serializable. Why ObjectInputStream receives a null pointer and throw exception NullPointer because No class founded
import java.io.Serializable;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAKeyGenParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class NewRSA implements Serializable{
private KeyPair keys ;
public NewRSA(){
this.keys=null;
}
public NewRSA(KeyPair keys)
{
this.keys=keys;
}
public KeyPair getKPair()
{
return keys;
}
public void setKPair(KeyPair keys)
{
this.keys=keys;
}
public KeyPair generateRsaKeyPair(int keySize, BigInteger publicExponent)
{
try
{
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(keySize, publicExponent);
keyGen.initialize(spec);
keys = keyGen.generateKeyPair();
}
catch(Exception e)
{
}
return keys;
}
public byte[] rsaEncrypt(byte[] original, PublicKey key) throws InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(original);
}
public static byte[] rsaDecrypt(byte[] encrypted, PrivateKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(encrypted);
}
}
Error is: No classe
Output PrintStackTrace java.lang.ClassNotFoundException: com.android.org.conscrypt.OpenSSLRSAPrivateCrtKey at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:626) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1613) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at stefano.Server$ServerThread.attemptLog(Server.java:268) at stefano.Server$ServerThread.handshake(Server.java:402) at stefano.Server$ServerThread.run(Server.java:428)
Upvotes: 0
Views: 212
Reputation:
Serializable
classes can't really be converted from the Android JVM to the usual Oracle/OpenJDK JVM, you can use the Externalizable
interface to manually serialize your class and retrieve it on the backend. There is however a solution that might work, it's to write your backend API in an IDE (using the Oracle Java) and compile it. It might work because the class signatures and headers are the same (it's a 50/50 Chance).
Upvotes: 0