Android KeyStore parameters building












0















I'm trying to use RSA encryption with KeyStore and I need to specify Parameters for KeyPairGenerator and I'm lost here. KeyPairGeneratorPair is kinda straightforward, but I don't understand KeyGenParameterSpec for API>=23



That's what I did, I think I got everything in else part, but now I'm confused about KeyGenParameterSpec



What exactly public exponent in RSAKeyGenParameterSpec is?



What Digests in .setDigests should i specify?



There's also .setBlockMode() method to call, and since I'm using RSA and RSA/None/OAEPWithSHA1AndMGF1Paddingwhich block mode to set? ECB, CBC?



if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
generator.initialize(new KeyGenParameterSpec.Builder("PrivateKey", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4))
.setDigests(KeyProperties.DIGEST_SHA1,
KeyProperties.DIGEST_SHA256)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
.setCertificateSerialNumber(BigInteger.ONE)
.setCertificateSubject(new X500Principal("CN=" + "PrivateKey"))
.setCertificateNotBefore(calendar.getTime())
.setCertificateNotAfter(endCalendar.getTime())
.setKeySize(2048).build());
} else {
generator.initialize(new KeyPairGeneratorSpec.Builder(MainActivity.this)
.setAlias("PrivateKey")
.setSerialNumber(BigInteger.ONE)
.setSubject(new X500Principal("CN=" + "PrivateKey"))
.setStartDate(calendar.getTime())
.setEndDate(endCalendar.getTime())
.setKeySize(2048).build()

);
}

Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding");









share|improve this question



























    0















    I'm trying to use RSA encryption with KeyStore and I need to specify Parameters for KeyPairGenerator and I'm lost here. KeyPairGeneratorPair is kinda straightforward, but I don't understand KeyGenParameterSpec for API>=23



    That's what I did, I think I got everything in else part, but now I'm confused about KeyGenParameterSpec



    What exactly public exponent in RSAKeyGenParameterSpec is?



    What Digests in .setDigests should i specify?



    There's also .setBlockMode() method to call, and since I'm using RSA and RSA/None/OAEPWithSHA1AndMGF1Paddingwhich block mode to set? ECB, CBC?



    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    generator.initialize(new KeyGenParameterSpec.Builder("PrivateKey", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
    .setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4))
    .setDigests(KeyProperties.DIGEST_SHA1,
    KeyProperties.DIGEST_SHA256)
    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
    .setCertificateSerialNumber(BigInteger.ONE)
    .setCertificateSubject(new X500Principal("CN=" + "PrivateKey"))
    .setCertificateNotBefore(calendar.getTime())
    .setCertificateNotAfter(endCalendar.getTime())
    .setKeySize(2048).build());
    } else {
    generator.initialize(new KeyPairGeneratorSpec.Builder(MainActivity.this)
    .setAlias("PrivateKey")
    .setSerialNumber(BigInteger.ONE)
    .setSubject(new X500Principal("CN=" + "PrivateKey"))
    .setStartDate(calendar.getTime())
    .setEndDate(endCalendar.getTime())
    .setKeySize(2048).build()

    );
    }

    Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding");









    share|improve this question

























      0












      0








      0








      I'm trying to use RSA encryption with KeyStore and I need to specify Parameters for KeyPairGenerator and I'm lost here. KeyPairGeneratorPair is kinda straightforward, but I don't understand KeyGenParameterSpec for API>=23



      That's what I did, I think I got everything in else part, but now I'm confused about KeyGenParameterSpec



      What exactly public exponent in RSAKeyGenParameterSpec is?



      What Digests in .setDigests should i specify?



      There's also .setBlockMode() method to call, and since I'm using RSA and RSA/None/OAEPWithSHA1AndMGF1Paddingwhich block mode to set? ECB, CBC?



      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      generator.initialize(new KeyGenParameterSpec.Builder("PrivateKey", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
      .setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4))
      .setDigests(KeyProperties.DIGEST_SHA1,
      KeyProperties.DIGEST_SHA256)
      .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
      .setCertificateSerialNumber(BigInteger.ONE)
      .setCertificateSubject(new X500Principal("CN=" + "PrivateKey"))
      .setCertificateNotBefore(calendar.getTime())
      .setCertificateNotAfter(endCalendar.getTime())
      .setKeySize(2048).build());
      } else {
      generator.initialize(new KeyPairGeneratorSpec.Builder(MainActivity.this)
      .setAlias("PrivateKey")
      .setSerialNumber(BigInteger.ONE)
      .setSubject(new X500Principal("CN=" + "PrivateKey"))
      .setStartDate(calendar.getTime())
      .setEndDate(endCalendar.getTime())
      .setKeySize(2048).build()

      );
      }

      Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding");









      share|improve this question














      I'm trying to use RSA encryption with KeyStore and I need to specify Parameters for KeyPairGenerator and I'm lost here. KeyPairGeneratorPair is kinda straightforward, but I don't understand KeyGenParameterSpec for API>=23



      That's what I did, I think I got everything in else part, but now I'm confused about KeyGenParameterSpec



      What exactly public exponent in RSAKeyGenParameterSpec is?



      What Digests in .setDigests should i specify?



      There's also .setBlockMode() method to call, and since I'm using RSA and RSA/None/OAEPWithSHA1AndMGF1Paddingwhich block mode to set? ECB, CBC?



      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      generator.initialize(new KeyGenParameterSpec.Builder("PrivateKey", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
      .setAlgorithmParameterSpec(new RSAKeyGenParameterSpec(2048, RSAKeyGenParameterSpec.F4))
      .setDigests(KeyProperties.DIGEST_SHA1,
      KeyProperties.DIGEST_SHA256)
      .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
      .setCertificateSerialNumber(BigInteger.ONE)
      .setCertificateSubject(new X500Principal("CN=" + "PrivateKey"))
      .setCertificateNotBefore(calendar.getTime())
      .setCertificateNotAfter(endCalendar.getTime())
      .setKeySize(2048).build());
      } else {
      generator.initialize(new KeyPairGeneratorSpec.Builder(MainActivity.this)
      .setAlias("PrivateKey")
      .setSerialNumber(BigInteger.ONE)
      .setSubject(new X500Principal("CN=" + "PrivateKey"))
      .setStartDate(calendar.getTime())
      .setEndDate(endCalendar.getTime())
      .setKeySize(2048).build()

      );
      }

      Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding");






      android encryption rsa keystore






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:21









      WiktorWiktor

      13711




      13711
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Method setDigests() sets digest method for your padding mode and setBlockMode() sets encryption mode which depends on your work.



          I think you have set a lot of unnecessary field. For example I use this method to create my own RSA key:



          public boolean createKey() {
          try {
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
          KeyProperties.KEY_ALGORITHM_RSA,
          "AndroidKeyStore"
          );

          mKeyStore.load(null);
          KeyGenParameterSpec.Builder builder =
          new KeyGenParameterSpec.Builder(
          MY_KEY,
          KeyProperties.PURPOSE_DECRYPT).
          setKeySize(MY_KEYLEN).
          setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).
          setDigests(KeyProperties.DIGEST_SHA256);

          keyPairGenerator.initialize(builder.build());
          keyPairGenerator.generateKeyPair();
          } catch (NoSuchAlgorithmException | CertificateException | IOException |
          InvalidAlgorithmParameterException | NoSuchProviderException e) {
          return false;
          }

          return true;
          }


          I created this key to use with RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm.






          share|improve this answer


























          • Oh, so i don't need to specify those fields at all. One more thing, how do I know which Digest to use? Like SHA256 or SHA512? If i use OAEPWithSHA1 does that mean that i should use SHA1?

            – Wiktor
            Nov 14 '18 at 18:42






          • 1





            @Wiktor depends on cipher that you want to use. For example I created this key to use in RSA/ECB/OAEPWithSHA-256AndMGF1Padding. This algorithm needs SHA-256 digest.

            – Afshin
            Nov 14 '18 at 18:54













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306534%2fandroid-keystore-parameters-building%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Method setDigests() sets digest method for your padding mode and setBlockMode() sets encryption mode which depends on your work.



          I think you have set a lot of unnecessary field. For example I use this method to create my own RSA key:



          public boolean createKey() {
          try {
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
          KeyProperties.KEY_ALGORITHM_RSA,
          "AndroidKeyStore"
          );

          mKeyStore.load(null);
          KeyGenParameterSpec.Builder builder =
          new KeyGenParameterSpec.Builder(
          MY_KEY,
          KeyProperties.PURPOSE_DECRYPT).
          setKeySize(MY_KEYLEN).
          setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).
          setDigests(KeyProperties.DIGEST_SHA256);

          keyPairGenerator.initialize(builder.build());
          keyPairGenerator.generateKeyPair();
          } catch (NoSuchAlgorithmException | CertificateException | IOException |
          InvalidAlgorithmParameterException | NoSuchProviderException e) {
          return false;
          }

          return true;
          }


          I created this key to use with RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm.






          share|improve this answer


























          • Oh, so i don't need to specify those fields at all. One more thing, how do I know which Digest to use? Like SHA256 or SHA512? If i use OAEPWithSHA1 does that mean that i should use SHA1?

            – Wiktor
            Nov 14 '18 at 18:42






          • 1





            @Wiktor depends on cipher that you want to use. For example I created this key to use in RSA/ECB/OAEPWithSHA-256AndMGF1Padding. This algorithm needs SHA-256 digest.

            – Afshin
            Nov 14 '18 at 18:54


















          1














          Method setDigests() sets digest method for your padding mode and setBlockMode() sets encryption mode which depends on your work.



          I think you have set a lot of unnecessary field. For example I use this method to create my own RSA key:



          public boolean createKey() {
          try {
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
          KeyProperties.KEY_ALGORITHM_RSA,
          "AndroidKeyStore"
          );

          mKeyStore.load(null);
          KeyGenParameterSpec.Builder builder =
          new KeyGenParameterSpec.Builder(
          MY_KEY,
          KeyProperties.PURPOSE_DECRYPT).
          setKeySize(MY_KEYLEN).
          setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).
          setDigests(KeyProperties.DIGEST_SHA256);

          keyPairGenerator.initialize(builder.build());
          keyPairGenerator.generateKeyPair();
          } catch (NoSuchAlgorithmException | CertificateException | IOException |
          InvalidAlgorithmParameterException | NoSuchProviderException e) {
          return false;
          }

          return true;
          }


          I created this key to use with RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm.






          share|improve this answer


























          • Oh, so i don't need to specify those fields at all. One more thing, how do I know which Digest to use? Like SHA256 or SHA512? If i use OAEPWithSHA1 does that mean that i should use SHA1?

            – Wiktor
            Nov 14 '18 at 18:42






          • 1





            @Wiktor depends on cipher that you want to use. For example I created this key to use in RSA/ECB/OAEPWithSHA-256AndMGF1Padding. This algorithm needs SHA-256 digest.

            – Afshin
            Nov 14 '18 at 18:54
















          1












          1








          1







          Method setDigests() sets digest method for your padding mode and setBlockMode() sets encryption mode which depends on your work.



          I think you have set a lot of unnecessary field. For example I use this method to create my own RSA key:



          public boolean createKey() {
          try {
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
          KeyProperties.KEY_ALGORITHM_RSA,
          "AndroidKeyStore"
          );

          mKeyStore.load(null);
          KeyGenParameterSpec.Builder builder =
          new KeyGenParameterSpec.Builder(
          MY_KEY,
          KeyProperties.PURPOSE_DECRYPT).
          setKeySize(MY_KEYLEN).
          setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).
          setDigests(KeyProperties.DIGEST_SHA256);

          keyPairGenerator.initialize(builder.build());
          keyPairGenerator.generateKeyPair();
          } catch (NoSuchAlgorithmException | CertificateException | IOException |
          InvalidAlgorithmParameterException | NoSuchProviderException e) {
          return false;
          }

          return true;
          }


          I created this key to use with RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm.






          share|improve this answer















          Method setDigests() sets digest method for your padding mode and setBlockMode() sets encryption mode which depends on your work.



          I think you have set a lot of unnecessary field. For example I use this method to create my own RSA key:



          public boolean createKey() {
          try {
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
          KeyProperties.KEY_ALGORITHM_RSA,
          "AndroidKeyStore"
          );

          mKeyStore.load(null);
          KeyGenParameterSpec.Builder builder =
          new KeyGenParameterSpec.Builder(
          MY_KEY,
          KeyProperties.PURPOSE_DECRYPT).
          setKeySize(MY_KEYLEN).
          setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).
          setDigests(KeyProperties.DIGEST_SHA256);

          keyPairGenerator.initialize(builder.build());
          keyPairGenerator.generateKeyPair();
          } catch (NoSuchAlgorithmException | CertificateException | IOException |
          InvalidAlgorithmParameterException | NoSuchProviderException e) {
          return false;
          }

          return true;
          }


          I created this key to use with RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 19:17

























          answered Nov 14 '18 at 18:31









          AfshinAfshin

          3,0161625




          3,0161625













          • Oh, so i don't need to specify those fields at all. One more thing, how do I know which Digest to use? Like SHA256 or SHA512? If i use OAEPWithSHA1 does that mean that i should use SHA1?

            – Wiktor
            Nov 14 '18 at 18:42






          • 1





            @Wiktor depends on cipher that you want to use. For example I created this key to use in RSA/ECB/OAEPWithSHA-256AndMGF1Padding. This algorithm needs SHA-256 digest.

            – Afshin
            Nov 14 '18 at 18:54





















          • Oh, so i don't need to specify those fields at all. One more thing, how do I know which Digest to use? Like SHA256 or SHA512? If i use OAEPWithSHA1 does that mean that i should use SHA1?

            – Wiktor
            Nov 14 '18 at 18:42






          • 1





            @Wiktor depends on cipher that you want to use. For example I created this key to use in RSA/ECB/OAEPWithSHA-256AndMGF1Padding. This algorithm needs SHA-256 digest.

            – Afshin
            Nov 14 '18 at 18:54



















          Oh, so i don't need to specify those fields at all. One more thing, how do I know which Digest to use? Like SHA256 or SHA512? If i use OAEPWithSHA1 does that mean that i should use SHA1?

          – Wiktor
          Nov 14 '18 at 18:42





          Oh, so i don't need to specify those fields at all. One more thing, how do I know which Digest to use? Like SHA256 or SHA512? If i use OAEPWithSHA1 does that mean that i should use SHA1?

          – Wiktor
          Nov 14 '18 at 18:42




          1




          1





          @Wiktor depends on cipher that you want to use. For example I created this key to use in RSA/ECB/OAEPWithSHA-256AndMGF1Padding. This algorithm needs SHA-256 digest.

          – Afshin
          Nov 14 '18 at 18:54







          @Wiktor depends on cipher that you want to use. For example I created this key to use in RSA/ECB/OAEPWithSHA-256AndMGF1Padding. This algorithm needs SHA-256 digest.

          – Afshin
          Nov 14 '18 at 18:54




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306534%2fandroid-keystore-parameters-building%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Xamarin.form Move up view when keyboard appear

          JBPM : POST request for execute process go wrong