RoxaneFelton
RoxaneFelton

Reputation: 129

Scala syntax for RSA-OAEP encryption using PEM file reader

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.

Upvotes: 1

Views: 276

Answers (1)

RoxaneFelton
RoxaneFelton

Reputation: 129

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

Upvotes: 1

Related Questions