1

I am facing error in Scala when trying init RSA Cipher:

I am trying to read a PEM file containing Public and Private keys. When reading the pem file in Bouncy castle I am getting error when passing the same as key in the init.

My PEM file:

-----BEGIN RSA Public Key-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl4pTSyS0xsC1HYGsw03+
v48OvOOkuamnEIDwXtYFdYD30GOT1JbKA9xTccFlYuARePizUO9snMzYvowOsAb+
Ichp27QgWbRq7oe+VKJ2yeKbujC6LUO/7DS/TLBGZ6V+q5X+4lFgnVrppFXZXCvp
TiwY5CDraQXexp/4PfIbutyP+AIPVshpDD4CKpCHlTyP/JUK06I6km/AYgIfPsFq
m1Fp+iTh5xu3wX5Ys3G5QkC22k19gG2+dFMm26dtoEemfkAWk7DXfFHYxWSLy1D3
aaZOIOB903bP4MK5bF9CkB//8HwPMCYE6J2XX2xTJP4X3wIdVeLGUMwu1uNZHLeR
UwIDAQAB
-----END RSA Public Key-----

What changes are needed for the error

My code :

import org.apache.commons.io.FileUtils
import org.bouncycastle.util.io.pem.PemReader


import java.io.{File, FileOutputStream, FileReader, IOException}
import java.nio.charset.StandardCharsets
import java.security._
import java.security.interfaces.RSAPublicKey
import java.security.spec.InvalidKeySpecException
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import javax.crypto.Cipher
import java.security.KeyFactory


object RSA_encrypt_decrypt extends App {

  val file = "/home/roxane/Music/ss_key"
  val readfile = new File(file)
  val key = FileUtils.readFileToString(readfile, "UTF-8")
  val sym_key = key.getBytes(StandardCharsets.UTF_8)
  println(sym_key)

  val pubfile = "/home/roxane/Music/public.pem"
  val readpubfile = new FileReader(pubfile)
  val prvfile = "/home/roxane/Music/private.pem"
  val readprvfile = new FileReader(prvfile)


  val publickey = RSAKey(readpubfile,"PUBLIC")

  val pipher = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding")
  pipher.init(Cipher.ENCRYPT_MODE, publickey)


  def RSAKey(file: FileReader, key: String) :Any = {
    key.toUpperCase() match {

      case "PUBLIC" => {
        // Generate public key from pem file
        val factory = KeyFactory.getInstance("RSA")
        val pubpemReader = new PemReader(file) // readpubfile
        val pubpemObject = pubpemReader.readPemObject()
        val content = pubpemObject.getContent()

        val pubKeySpec = new X509EncodedKeySpec(content)
        val pubkey = factory.generatePublic(pubKeySpec)

        return factory.generatePublic(pubKeySpec).asInstanceOf[RSAPublicKey]
      }
    }
  }

Error:

overloaded method value init with alternatives: (x$1: Int,x$2: java.security.cert.Certificate)Unit (x$1: Int,x$2: java.security.Key)Unit cannot be applied to (Int, Any) pipher.init(Cipher.ENCRYPT_MODE, publickey)

I am trying to write the scala equivalent code for factory.generatePublic(pubKeySpec).asInstanceOf[RSAPublicKey] as this

rsaPublicKey = (RSAPublicKey) factory.generatePublic(pubKeySpec);

might be that's where I am wrong.

1 Answer 1

1

The issue is resolved. I have used the right typecasting for the Java code: The Issue was in the method Signature for RSAKey Generation . The right signature is below:

 def RSAKey(file: FileReader, key: String) :Key
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.