0

My question is that on how to use RSA 512 bit in Android; I was able to generate the public and private keys by looking up on the internet but not sure if its with NO padding as I could not find any solution for it.The public key would be sent to the server also with the integer encrypted with the private key. I am new to it so it would be great to have some help. Thanks!

genreateKeys("RSA",512)

The following code:

    private static void generateKeys(String keyAlgorithm, int numBits) {

    try {
        // Get the public/private key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
        keyGen.initialize(numBits);
        KeyPair keyPair = keyGen.genKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

        System.out.println("\n" + "Generating key/value pair using " + privateKey.getAlgorithm() + " algorithm");

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes = publicKey.getEncoded();

        // Get the formats of the encoded bytes
        String formatPrivate = privateKey.getFormat(); // PKCS#8
        String formatPublic = publicKey.getFormat(); // X.509

        System.out.println("Private Key : " + HttpRequest.Base64.encode(String.valueOf(privateKeyBytes)));
        System.out.println("Public Key : " + HttpRequest.Base64.encode(String.valueOf(publicKeyBytes)));

        // The bytes can be converted back to public and private key objects
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);

        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);

        // The original and new keys are the same
        System.out.println("  Are both private keys equal? " + privateKey.equals(privateKey2));
        System.out.println("  Are both public keys equal? " + publicKey.equals(publicKey2));
    } catch (InvalidKeySpecException specException) {
        System.out.println("Exception");
        System.out.println("Invalid Key Spec Exception");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception");
        System.out.println("No such algorithm: " + keyAlgorithm);
    }

}

It gives me the output of public and private keys but its not with NO PADDING also how can I use the private key to encrypt my integer value?

  • Padding mechanisms come into picture when you perform some action with the Key (Sign / Encrypt ... etc.). What exactly are you trying to achieve ? – Exception_al Feb 12 '18 at 15:42
  • I will be sending the public key to the server, and after somepoint will send the integer value encrypted with the private key to the server. The server API will decrypt the value itself. My work is just to send the public key and encrypted private key to the server. – Umaima Khurshid Ahmad Feb 12 '18 at 16:08
  • Where is the code where you send the encrypted Integer to the server ? – Exception_al Feb 12 '18 at 16:09
  • The requirement is stated as below "the amount (Integer) is encrypted with the local private key and shared on server" AND " For Androids,RSA512 bit encryption will be used with the NO_PADDING option" – Umaima Khurshid Ahmad Feb 12 '18 at 16:10
  • That's what I need to find out how encrypt the integer value with teh help of the private key – Umaima Khurshid Ahmad Feb 12 '18 at 16:12
  • In principle you don't encrypt with private key but 'sign' with it ... That's what private key is meant for .... But still if your situation requires encrypting with private key, try this answer from another user for a different post : https://stackoverflow.com/a/21647527 – Exception_al Feb 12 '18 at 16:34
  • Possible duplicate of [Encrypting with RSA private key in Java](https://stackoverflow.com/questions/1391692/encrypting-with-rsa-private-key-in-java) – Exception_al Feb 12 '18 at 16:35
  • I don't remember after which OS version was applied, but privateKey.getEncoded() should return null (if the key is generated from the KeyPairGenerator). The OS will not let you to extract the private key material out side of it. Only will let you use it with Chiper etc. – Galeen Apr 16 '18 at 15:05

0 Answers0