Acho que não é correto por que me mostra assim, veja a private é diferente do privado depois de fazer o deserialize:
Private: org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters@e4f972
Public: org.bouncycastle.crypto.params.RSAKeyParameters@b4d3d5
Amigos, vai o meu código completo. Acho que está certo na hora de deserializar. Pois
Já deixei comentado as linhas que gera as chave automáticas e ele consegue ler os arquivos do disco e fazer
a encriptacao e descriptação.
Assim não consigo armazenar ou persistir as chaves em arquivos. Alguém sabe??
Por ironia peguei essa parte de Serialização de um livro J2me e que na verdade não reconhece esses pacotes.
packagersa;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.math.BigInteger;importjava.security.SecureRandom;importorg.bouncycastle.crypto.AsymmetricBlockCipher;importorg.bouncycastle.crypto.AsymmetricCipherKeyPair;importorg.bouncycastle.crypto.encodings.PKCS1Encoding;importorg.bouncycastle.crypto.engines.RSAEngine;importorg.bouncycastle.crypto.generators.RSAKeyPairGenerator;importorg.bouncycastle.crypto.params.RSAKeyGenerationParameters;importorg.bouncycastle.crypto.params.RSAKeyParameters;importorg.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;publicclassEncryption{/** * @param args the command line arguments */privateRSAPrivateCrtKeyParameters_RSAPrivateKey;privateRSAKeyParameters_RSAPublicKey;publicstaticvoidmain(String[]args){// TODO code application logic hereEncryptiontheEncryption=newEncryption();StringtheStringBeforeEncryption="Boa noite cuiaba";StringtheStringAfterEncryption=null;byte[]theEncryptedString;try{System.out.println(theStringBeforeEncryption);theEncryption.generateRSAKeyPair();theEncryptedString=theEncryption.RSAEncrypt(theStringBeforeEncryption.getBytes());theStringAfterEncryption=newString(theEncryption.RSADecrypt(theEncryptedString));System.out.println(theStringAfterEncryption);}catch(Exceptione){// TODO Handle exception!e.printStackTrace();}}privateRSAPrivateCrtKeyParametersRSAprivKey_des;privateRSAKeyParametersRSApubKey_des;privatevoidDeserialize()throwsIOException,ClassNotFoundException{FileInputStreamfis=newFileInputStream("RSAmod.dat");byte[]newArrayBytes=newbyte[fis.available()];fis.read(newArrayBytes);BigIntegerRSAmod=newBigInteger(newArrayBytes);fis.close();FileInputStreamfis1=newFileInputStream("RSAprivExp.dat");byte[]newArrayBytes1=newbyte[fis1.available()];fis1.read(newArrayBytes1);BigIntegerRSAprivExp=newBigInteger(newArrayBytes1);fis1.close();FileInputStreamfis2=newFileInputStream("RSApubExp.dat");byte[]newArrayBytes2=newbyte[fis2.available()];fis2.read(newArrayBytes2);BigIntegerRSApubExp=newBigInteger(newArrayBytes2);fis2.close();FileInputStreamfis3=newFileInputStream("RSAdp.dat");byte[]newArrayBytes3=newbyte[fis3.available()];fis3.read(newArrayBytes3);BigIntegerRSAdp=newBigInteger(newArrayBytes3);fis3.close();FileInputStreamfis4=newFileInputStream("RSAdq.dat");byte[]newArrayBytes4=newbyte[fis4.available()];fis4.read(newArrayBytes4);BigIntegerRSAdq=newBigInteger(newArrayBytes4);fis4.close();FileInputStreamfis5=newFileInputStream("RSAp.dat");byte[]newArrayBytes5=newbyte[fis5.available()];fis5.read(newArrayBytes5);BigIntegerRSAp=newBigInteger(newArrayBytes5);fis5.close();FileInputStreamfis6=newFileInputStream("RSAq.dat");byte[]newArrayBytes6=newbyte[fis6.available()];fis6.read(newArrayBytes6);BigIntegerRSAq=newBigInteger(newArrayBytes6);fis6.close();FileInputStreamfis7=newFileInputStream("RSAqInv.dat");byte[]newArrayBytes7=newbyte[fis7.available()];fis7.read(newArrayBytes7);BigIntegerRSAqInv=newBigInteger(newArrayBytes7);fis7.close();RSAprivKey_des=newRSAPrivateCrtKeyParameters(RSAmod,RSApubExp,RSAprivExp,RSAp,RSAq,RSAdp,RSAdq,RSAqInv);RSApubKey_des=newRSAKeyParameters(false,RSAmod,RSApubExp);System.out.println("Privado-: "+RSAprivKey_des);System.out.println("Pupblico-: "+RSApubKey_des);}privatevoidSerializetoFile(RSAPrivateCrtKeyParametersRSAprivKey)throwsFileNotFoundException,IOException,Exception{FileOutputStreamout;BigIntegerpubExp;BigIntegermod=RSAprivKey.getModulus();out=newFileOutputStream("RSAmod.dat");out.write(mod.toByteArray());out.flush();out.close();BigIntegerprivExp=RSAprivKey.getExponent();out=newFileOutputStream("RSAprivExp.dat");out.write(privExp.toByteArray());out.flush();out.close();pubExp=RSAprivKey.getPublicExponent();if(!pubExp.equals(newBigInteger("10001",16))){thrownewException("wrong public exponent");}out=newFileOutputStream("RSApubExp.dat");out.write(pubExp.toByteArray());out.flush();out.close();BigIntegerdp=RSAprivKey.getDP();out=newFileOutputStream("RSAdp.dat");out.write(dp.toByteArray());out.flush();out.close();BigIntegerdq=RSAprivKey.getDQ();out=newFileOutputStream("RSAdq.dat");out.write(dq.toByteArray());out.flush();out.close();BigIntegerp=RSAprivKey.getP();out=newFileOutputStream("RSAp.dat");out.write(p.toByteArray());out.flush();out.close();BigIntegerq=RSAprivKey.getQ();out=newFileOutputStream("RSAq.dat");out.write(q.toByteArray());out.flush();out.close();BigIntegerqInv=RSAprivKey.getQInv();out=newFileOutputStream("RSAqInv.dat");out.write(qInv.toByteArray());out.flush();out.close();}privatevoidgenerateRSAKeyPair()throwsException{SecureRandomtheSecureRandom=newSecureRandom();BigIntegerthePublicExponent=newBigInteger("10001",16);RSAKeyGenerationParameterstheRSAKeyGenParam=newRSAKeyGenerationParameters(thePublicExponent,theSecureRandom,1024,80);RSAKeyPairGeneratortheRSAKeyPairGen=newRSAKeyPairGenerator();theRSAKeyPairGen.init(theRSAKeyGenParam);AsymmetricCipherKeyPairtheKeyPair=theRSAKeyPairGen.generateKeyPair();_RSAPrivateKey=(RSAPrivateCrtKeyParameters)theKeyPair.getPrivate();_RSAPublicKey=(RSAKeyParameters)theKeyPair.getPublic();System.out.println("Private: "+_RSAPrivateKey);System.out.println("Public: "+_RSAPublicKey);SerializetoFile(_RSAPrivateKey);Deserialize();}privatebyte[]RSAEncrypt(byte[]toEncrypt)throwsException{if(RSApubKey_des==null){thrownewException("Please generate RSA keys first in order to work");}AsymmetricBlockCiphertheEngine=newRSAEngine();theEngine=newPKCS1Encoding(theEngine);theEngine.init(true,RSApubKey_des);returntheEngine.processBlock(toEncrypt,0,toEncrypt.length);}privatebyte[]RSADecrypt(byte[]toDecrypt)throwsException{if(RSAprivKey_des==null){thrownewException("Please generate RSA keys first in order to work");}AsymmetricBlockCiphertheEngine=newRSAEngine();theEngine=newPKCS1Encoding(theEngine);theEngine.init(false,RSAprivKey_des);returntheEngine.processBlock(toDecrypt,0,toDecrypt.length);}}