1 2 Previous Next 15 Replies Latest reply on Aug 29, 2010 11:55 AM by 843811 Go to original post
      • 15. Re: Sending Public key from client to server
        Shane, you are a genius, brother! Thank you!
        I did as you directed. Turns out I was wrong about using the .equals() method to check that both pub keys were the same. Look at my codes! It encrypts and decrypts as normal!!!!!
        RSAKeyPairGenerator adminKeyGen = new RSAKeyPairGenerator();
                  RSAKeyGenerationParameters genGenParam = 
                       new RSAKeyGenerationParameters(BigInteger.valueOf(0x11), new SecureRandom(),1024,25);
                  AsymmetricCipherKeyPair pair = adminKeyGen.generateKeyPair();
                  RSAKeyParameters adminPubKey = (RSAKeyParameters) pair.getPublic(); //need to store
                  RSAKeyParameters adminPrivKey = (RSAKeyParameters) pair.getPrivate(); //need to store
                  byte[] exponent = adminPubKey.getExponent().toByteArray();
                  byte[] modulus = adminPubKey.getModulus().toByteArray();
                  byte[] toEncrypt = "Encrypt this, people!".getBytes("UTF-8");
                  RSAPublicKeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(exponent));
                  KeyFactory factory = KeyFactory.getInstance("RSA");
                  PublicKey pubKey = factory.generatePublic(spec);
                  Cipher rsaCipher = Cipher.getInstance("RSA", new BouncyCastleProvider());
                  rsaCipher.init(Cipher.ENCRYPT_MODE, pubKey);
                byte[] encrypted = rsaCipher.doFinal(toEncrypt);
                System.out.println("The text encrypted by java.security: " + encrypted);
                RSAKeyParameters adminPubKey2 = new RSAKeyParameters(false, new BigInteger(modulus), new BigInteger(exponent));
                RSAEngine adminSignerEngine2 = new RSAEngine();
                  adminSignerEngine2.init(true, adminPubKey2);
                  byte[] encrypted2 = adminSignerEngine2.processBlock(toEncrypt, 0, toEncrypt.length);
                System.out.println("The text encrypted by new adminPubKey: " + encrypted2);
                RSAEngine adminSignerEngine = new RSAEngine();
                  adminSignerEngine.init(false, adminPrivKey);
                  byte[] decrypted = adminSignerEngine.processBlock(encrypted, 0, encrypted.length);
                System.out.println("\nThe decrypted text is by java.security encyrption: " + new String(decrypted));
                byte[] decrypted2 = adminSignerEngine.processBlock(encrypted2, 0, encrypted2.length);
                System.out.println("\nThe decrypted text is by adminPubKey2 encyrption: " + new String(decrypted2));
        Thank you, man!
        1 2 Previous Next