Android Kotlin AES Decryption returns Bad DecryptionIs there a way to run Python on Android?How do save an Android Activity state using save instance state?Encrypt and decrypt a string in C#?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?“Debug certificate expired” error in Eclipse Android pluginsIs there a unique Android device ID?What is 'Context' on Android?Proper use cases for Android UserManager.isUserAGoat()?How to add and get specific number of bytes from a file using file input and output streams?

How do I tell my manager that his code review comment is wrong?

Copy line and insert it in a new position with sed or awk

If 1. e4 c6 is considered as a sound defense for black, why is 1. c3 so rare?

Can I use 1000v rectifier diodes instead of 600v rectifier diodes?

Is Cola "probably the best-known" Latin word in the world? If not, which might it be?

Pigeonhole Principle Problem

How to scale a verbatim environment on a minipage?

Packet sniffer for MacOS Mojave and above

Why are notes ordered like they are on a piano?

Can fracking help reduce CO2?

Field Length Validation for Desktop Application which has maximum 1000 characters

If Melisandre foresaw another character closing blue eyes, why did she follow Stannis?

When do aircrafts become solarcrafts?

Pressure to defend the relevance of one's area of mathematics

Why is this a valid proof for the harmonic series?

Any examples of headwear for races with animal ears?

How to efficiently calculate prefix sum of frequencies of characters in a string?

Does higher resolution in an image imply more bits per pixel?

You look catfish vs You look like a catfish?

Which skill should be used for secret doors or traps: Perception or Investigation?

Selecting a secure PIN for building access

What is the limiting factor for a CAN bus to exceed 1Mbps bandwidth?

Is balancing necessary on a full-wheel change?

What does air vanishing on contact sound like?



Android Kotlin AES Decryption returns Bad Decryption


Is there a way to run Python on Android?How do save an Android Activity state using save instance state?Encrypt and decrypt a string in C#?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?“Debug certificate expired” error in Eclipse Android pluginsIs there a unique Android device ID?What is 'Context' on Android?Proper use cases for Android UserManager.isUserAGoat()?How to add and get specific number of bytes from a file using file input and output streams?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I currently have a problem with the decryption of my file. The problem is already holding me up for a few days. Every attempt ends in a BAD_DECRYPT exception. This is what the decrypt code looks like.



val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), Constant().SALT_KEY.toByteArray(Charset.defaultCharset()), 65536, 256)
val tmp = factory.generateSecret(spec)
val secret = SecretKeySpec(tmp.encoded, "AES")
val ivKeyBytes = Constant().IV_SECRET_KEY.toByteArray()
val finalIvs = ByteArray(16)
val len = if (ivKeyBytes.count() > 16) 16 else ivKeyBytes.count()
System.arraycopy(ivKeyBytes, 0, finalIvs, 0, len)
val ivps = IvParameterSpec(finalIvs)

val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, secret, ivps)
return cipher.doFinal(fileData)


The encryption happens via a task on my server with openssl this looks like this



openssl enc -aes-256-cbc -md md5 -in PATH_TO_MY_FILE -out PATH_TO_MY_ENC_FILE -S "MYSALT_IN_HEX" -iv "MY_IV_IN_HEX" -k MY_KEY_PASS


What i make wrong? Can anyone help me?



Thanks in advance Sebastian










share|improve this question




























    0















    I currently have a problem with the decryption of my file. The problem is already holding me up for a few days. Every attempt ends in a BAD_DECRYPT exception. This is what the decrypt code looks like.



    val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
    val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), Constant().SALT_KEY.toByteArray(Charset.defaultCharset()), 65536, 256)
    val tmp = factory.generateSecret(spec)
    val secret = SecretKeySpec(tmp.encoded, "AES")
    val ivKeyBytes = Constant().IV_SECRET_KEY.toByteArray()
    val finalIvs = ByteArray(16)
    val len = if (ivKeyBytes.count() > 16) 16 else ivKeyBytes.count()
    System.arraycopy(ivKeyBytes, 0, finalIvs, 0, len)
    val ivps = IvParameterSpec(finalIvs)

    val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
    cipher.init(Cipher.DECRYPT_MODE, secret, ivps)
    return cipher.doFinal(fileData)


    The encryption happens via a task on my server with openssl this looks like this



    openssl enc -aes-256-cbc -md md5 -in PATH_TO_MY_FILE -out PATH_TO_MY_ENC_FILE -S "MYSALT_IN_HEX" -iv "MY_IV_IN_HEX" -k MY_KEY_PASS


    What i make wrong? Can anyone help me?



    Thanks in advance Sebastian










    share|improve this question
























      0












      0








      0








      I currently have a problem with the decryption of my file. The problem is already holding me up for a few days. Every attempt ends in a BAD_DECRYPT exception. This is what the decrypt code looks like.



      val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
      val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), Constant().SALT_KEY.toByteArray(Charset.defaultCharset()), 65536, 256)
      val tmp = factory.generateSecret(spec)
      val secret = SecretKeySpec(tmp.encoded, "AES")
      val ivKeyBytes = Constant().IV_SECRET_KEY.toByteArray()
      val finalIvs = ByteArray(16)
      val len = if (ivKeyBytes.count() > 16) 16 else ivKeyBytes.count()
      System.arraycopy(ivKeyBytes, 0, finalIvs, 0, len)
      val ivps = IvParameterSpec(finalIvs)

      val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
      cipher.init(Cipher.DECRYPT_MODE, secret, ivps)
      return cipher.doFinal(fileData)


      The encryption happens via a task on my server with openssl this looks like this



      openssl enc -aes-256-cbc -md md5 -in PATH_TO_MY_FILE -out PATH_TO_MY_ENC_FILE -S "MYSALT_IN_HEX" -iv "MY_IV_IN_HEX" -k MY_KEY_PASS


      What i make wrong? Can anyone help me?



      Thanks in advance Sebastian










      share|improve this question














      I currently have a problem with the decryption of my file. The problem is already holding me up for a few days. Every attempt ends in a BAD_DECRYPT exception. This is what the decrypt code looks like.



      val factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
      val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), Constant().SALT_KEY.toByteArray(Charset.defaultCharset()), 65536, 256)
      val tmp = factory.generateSecret(spec)
      val secret = SecretKeySpec(tmp.encoded, "AES")
      val ivKeyBytes = Constant().IV_SECRET_KEY.toByteArray()
      val finalIvs = ByteArray(16)
      val len = if (ivKeyBytes.count() > 16) 16 else ivKeyBytes.count()
      System.arraycopy(ivKeyBytes, 0, finalIvs, 0, len)
      val ivps = IvParameterSpec(finalIvs)

      val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
      cipher.init(Cipher.DECRYPT_MODE, secret, ivps)
      return cipher.doFinal(fileData)


      The encryption happens via a task on my server with openssl this looks like this



      openssl enc -aes-256-cbc -md md5 -in PATH_TO_MY_FILE -out PATH_TO_MY_ENC_FILE -S "MYSALT_IN_HEX" -iv "MY_IV_IN_HEX" -k MY_KEY_PASS


      What i make wrong? Can anyone help me?



      Thanks in advance Sebastian







      android encryption kotlin openssl aes






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 19:50









      Sebastian R.Sebastian R.

      198218




      198218






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The algorithm for SecretKeyFactory doesn't seem right. You are using openssl without pbkdf2 flag and with md5 hash. I think the corresponding algorithm would be "PBEWITHMD5AND256BITAES-CBC-OPENSSL" from bouncy castle provider.



          You also need to extract cipher text from the file - it starts with Salted__ string, followed by the salt, followed by the cipher text.



          With these changes decryption works fine for me:



           Security.addProvider(BouncyCastleProvider())
          val factory = SecretKeyFactory.getInstance("PBEWITHMD5AND256BITAES-CBC-OPENSSL", "BC")
          val salt = Arrays.copyOfRange(fileData, 8, 16)
          val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), salt, 65536, 256)
          ...
          val ct = Arrays.copyOfRange(fileData, 16, fileData.size)
          return cipher.doFinal(ct)





          share|improve this answer

























          • nope i changed the SecretKeyFactory algorithm to the PBEWITHMD5AND256BITAES-CBC-OPENSSL but nothing changed it returns BAD_DECRYPT

            – Sebastian R.
            Mar 23 at 13:09











          • Thx for you're edit, i get no BAD_DECRYPT Error the only think what didn't work is the data are half decrypted and half normal it's a mix of both :/

            – Sebastian R.
            Mar 28 at 14:11











          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%2f55306904%2fandroid-kotlin-aes-decryption-returns-bad-decryption%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









          0














          The algorithm for SecretKeyFactory doesn't seem right. You are using openssl without pbkdf2 flag and with md5 hash. I think the corresponding algorithm would be "PBEWITHMD5AND256BITAES-CBC-OPENSSL" from bouncy castle provider.



          You also need to extract cipher text from the file - it starts with Salted__ string, followed by the salt, followed by the cipher text.



          With these changes decryption works fine for me:



           Security.addProvider(BouncyCastleProvider())
          val factory = SecretKeyFactory.getInstance("PBEWITHMD5AND256BITAES-CBC-OPENSSL", "BC")
          val salt = Arrays.copyOfRange(fileData, 8, 16)
          val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), salt, 65536, 256)
          ...
          val ct = Arrays.copyOfRange(fileData, 16, fileData.size)
          return cipher.doFinal(ct)





          share|improve this answer

























          • nope i changed the SecretKeyFactory algorithm to the PBEWITHMD5AND256BITAES-CBC-OPENSSL but nothing changed it returns BAD_DECRYPT

            – Sebastian R.
            Mar 23 at 13:09











          • Thx for you're edit, i get no BAD_DECRYPT Error the only think what didn't work is the data are half decrypted and half normal it's a mix of both :/

            – Sebastian R.
            Mar 28 at 14:11















          0














          The algorithm for SecretKeyFactory doesn't seem right. You are using openssl without pbkdf2 flag and with md5 hash. I think the corresponding algorithm would be "PBEWITHMD5AND256BITAES-CBC-OPENSSL" from bouncy castle provider.



          You also need to extract cipher text from the file - it starts with Salted__ string, followed by the salt, followed by the cipher text.



          With these changes decryption works fine for me:



           Security.addProvider(BouncyCastleProvider())
          val factory = SecretKeyFactory.getInstance("PBEWITHMD5AND256BITAES-CBC-OPENSSL", "BC")
          val salt = Arrays.copyOfRange(fileData, 8, 16)
          val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), salt, 65536, 256)
          ...
          val ct = Arrays.copyOfRange(fileData, 16, fileData.size)
          return cipher.doFinal(ct)





          share|improve this answer

























          • nope i changed the SecretKeyFactory algorithm to the PBEWITHMD5AND256BITAES-CBC-OPENSSL but nothing changed it returns BAD_DECRYPT

            – Sebastian R.
            Mar 23 at 13:09











          • Thx for you're edit, i get no BAD_DECRYPT Error the only think what didn't work is the data are half decrypted and half normal it's a mix of both :/

            – Sebastian R.
            Mar 28 at 14:11













          0












          0








          0







          The algorithm for SecretKeyFactory doesn't seem right. You are using openssl without pbkdf2 flag and with md5 hash. I think the corresponding algorithm would be "PBEWITHMD5AND256BITAES-CBC-OPENSSL" from bouncy castle provider.



          You also need to extract cipher text from the file - it starts with Salted__ string, followed by the salt, followed by the cipher text.



          With these changes decryption works fine for me:



           Security.addProvider(BouncyCastleProvider())
          val factory = SecretKeyFactory.getInstance("PBEWITHMD5AND256BITAES-CBC-OPENSSL", "BC")
          val salt = Arrays.copyOfRange(fileData, 8, 16)
          val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), salt, 65536, 256)
          ...
          val ct = Arrays.copyOfRange(fileData, 16, fileData.size)
          return cipher.doFinal(ct)





          share|improve this answer















          The algorithm for SecretKeyFactory doesn't seem right. You are using openssl without pbkdf2 flag and with md5 hash. I think the corresponding algorithm would be "PBEWITHMD5AND256BITAES-CBC-OPENSSL" from bouncy castle provider.



          You also need to extract cipher text from the file - it starts with Salted__ string, followed by the salt, followed by the cipher text.



          With these changes decryption works fine for me:



           Security.addProvider(BouncyCastleProvider())
          val factory = SecretKeyFactory.getInstance("PBEWITHMD5AND256BITAES-CBC-OPENSSL", "BC")
          val salt = Arrays.copyOfRange(fileData, 8, 16)
          val spec = PBEKeySpec(Constant().SECRET_KEY.toCharArray(), salt, 65536, 256)
          ...
          val ct = Arrays.copyOfRange(fileData, 16, fileData.size)
          return cipher.doFinal(ct)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 10:38

























          answered Mar 23 at 11:24









          yachooryachoor

          7841616




          7841616












          • nope i changed the SecretKeyFactory algorithm to the PBEWITHMD5AND256BITAES-CBC-OPENSSL but nothing changed it returns BAD_DECRYPT

            – Sebastian R.
            Mar 23 at 13:09











          • Thx for you're edit, i get no BAD_DECRYPT Error the only think what didn't work is the data are half decrypted and half normal it's a mix of both :/

            – Sebastian R.
            Mar 28 at 14:11

















          • nope i changed the SecretKeyFactory algorithm to the PBEWITHMD5AND256BITAES-CBC-OPENSSL but nothing changed it returns BAD_DECRYPT

            – Sebastian R.
            Mar 23 at 13:09











          • Thx for you're edit, i get no BAD_DECRYPT Error the only think what didn't work is the data are half decrypted and half normal it's a mix of both :/

            – Sebastian R.
            Mar 28 at 14:11
















          nope i changed the SecretKeyFactory algorithm to the PBEWITHMD5AND256BITAES-CBC-OPENSSL but nothing changed it returns BAD_DECRYPT

          – Sebastian R.
          Mar 23 at 13:09





          nope i changed the SecretKeyFactory algorithm to the PBEWITHMD5AND256BITAES-CBC-OPENSSL but nothing changed it returns BAD_DECRYPT

          – Sebastian R.
          Mar 23 at 13:09













          Thx for you're edit, i get no BAD_DECRYPT Error the only think what didn't work is the data are half decrypted and half normal it's a mix of both :/

          – Sebastian R.
          Mar 28 at 14:11





          Thx for you're edit, i get no BAD_DECRYPT Error the only think what didn't work is the data are half decrypted and half normal it's a mix of both :/

          – Sebastian R.
          Mar 28 at 14:11



















          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%2f55306904%2fandroid-kotlin-aes-decryption-returns-bad-decryption%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







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript