使用ObjectOutputStream发送RSA对象

时间:2016-03-01 13:58:55

标签: java rsa objectoutputstream

我尝试使用ObjectOutpuStream发送这个我已经完成Serializable的RSA自定义对象。为什么ObjectInputStream接收空指针并抛出异常NullPointer因为没有类建立

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


  }
}

错误是: 没有classe

输出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)     在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)     在java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)     在stefano.Server $ ServerThread.attemptLog(Server.java:268)     在stefano.Server $ ServerThread.handshake(Server.java:402)     在stefano.Server $ ServerThread.run(Server.java:428)

0 个答案:

没有答案