Pyd
Pyd

Reputation: 6159

Error while trying to decrypt RSA

Trying to decrypt the below value which is already encrypted using RSA PrivateKey I'm using same key pair publicKey to decrypt.

Encrypted: Mfb5ano1MmrPX0gliGld/h1T6XegLq4P6G52fdr1vCwWlle5K1Y6FSshJ8E495sVjhpC9M10zDzqymkkxSOxbNz5qpLCcQQcfgkTIwALspWr18SyyfuKwO4H6TxpV6+eohgn4n+gt9aos4Tx/l4AKWeI7mpTR5TzzBUMgV3cpfM=

import java.io.File
import java.nio.file.Files
import java.security.KeyFactory
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.PrivateKey
import java.security.PublicKey
import java.security.SecureRandom
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import scala.io.Source
import org.apache.commons.codec.binary.Base64
import javax.crypto.Cipher

object AsymmetricCryptography {

  var cipher: Cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
  var keyGen: KeyPairGenerator = KeyPairGenerator.getInstance("RSA")
  var pair: KeyPair = keyGen.generateKeyPair()
  var privateKey: PrivateKey = pair.getPrivate()
  var publicKey: PublicKey = pair.getPublic()

def main(args: Array[String]) {

var random: SecureRandom = SecureRandom.getInstance("SHA1PRNG")
this.keyGen.initialize(1024, random)

var plaintext = new Array[Byte](117)
random.nextBytes(plaintext)

this.privateKey = getPrivate("./src/main/resources/privateKeyLicenceGenerator")
this.publicKey = getPublic("./src/main/resources/publicKeyLicenceGenerator")
 val msg: Array[String] = Source.fromFile("encryptedfile").getLines().toArray
val inputs = msg.mkString("").split("<TOKEN>")
val encrypted: String = inputs(1).replaceAll("</TOKEN>", "")
println(encrypted) //encrypted = above mensioned
val decrypted_msg: String = decryptText(encrypted, publicKey)
println(decrypted_msg)
}

def decryptText(msg: String, key: PublicKey): String = {
this.cipher.init(Cipher.DECRYPT_MODE, key)
val cipher = new String(this.cipher.doFinal(Base64.decodeBase64(msg)), "UTF-8")
return cipher
}

def getPublic(filename: String): PublicKey = {
val keyBytes = Files.readAllBytes(new File(filename).toPath())
val spec = new X509EncodedKeySpec(keyBytes)
val kf: KeyFactory = KeyFactory.getInstance("RSA")
val a = kf.generatePublic(spec)
return a
}
}

Error:

 Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at wordcount.AsymmetricCryptography$.decryptText(AsymmetricCryptography.scala:49)
at wordcount.AsymmetricCryptography$.main(AsymmetricCryptography.scala:43)
at wordcount.AsymmetricCryptography.main(AsymmetricCryptography.scala)

Upvotes: 0

Views: 483

Answers (1)

Gijs Overvliet
Gijs Overvliet

Reputation: 2691

The cipher you are using is probably incorrect. The javax.crypto library does not have all ciphers available, and you may need to use an external library. At this moment there is no way for me to tell which cipher you need to use, or where you can find the correct algorithm. The following might help you to figure that out for yourself.

For example, rsa-oaep works with a digest method and a mask generation function (MGF). If you use the following cipher, which was available on our system, it didn't work:

Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");

We got the same exception that you mentioned.

Inspecting the cipher used for encryption, we discovered that the MGF was incorrect:

    <xenc:EncryptionMethod Algorithm="http://www.w3.org/2009/xmlenc11#rsa-oaep">
       <ds:DigestMethod xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
       <xenc11:MGF xmlns:xenc11="http://www.w3.org/2009/xmlenc11#" Algorithm="http://www.w3.org/2009/xmlenc11#mgf1sha256"/>
    </xenc:EncryptionMethod>

Notice mgf1sha256 in the xenc11:MFG tag, which is different from MGF1Padding. This function was not available in our system, but it is available in Apache Santuario (http://santuario.apache.org/)

Upvotes: 1

Related Questions