尝试解密RSA时出错

问题描述 投票:0回答:1

试图解密已使用RSA PrivateKey加密的下面的值我正在使用相同的密钥对publicKey来解密。

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
}
}

错误:

 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)
java scala encryption cryptography rsa
1个回答
1
投票

您使用的密码可能不正确。 javax.crypto库没有可用的所有密码,您可能需要使用外部库。此时我无法分辨您需要使用哪种密码,或者您可以在哪里找到正确的算法。以下内容可以帮助您自己解决这个问题。

例如,rsa-oaep使用摘要方法和掩码生成函数(MGF)。如果您使用我们系统上提供的以下密码,则它无效:

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

我们得到了你提到的同样的例外。

检查用于加密的密码,我们发现MGF不正确:

    <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>

请注意xenc11:MFG标签中的mgf1sha256,它与MGF1Padding不同。此功能在我们的系统中不可用,但在Apache Santuario(http://santuario.apache.org/)中可用

© www.soinside.com 2019 - 2024. All rights reserved.