Jason Shu
Jason Shu

Reputation: 139

encodeToString is not a member of Base64?

In scala, I have parts of the code that's doing Base64.encodeToString & Base64.decode

 def genKeyAES(): String = {
    val keyGen = KeyGenerator.getInstance("AES")
    keyGen.init(128)
    val key = keyGen.generateKey()
    val base64Str = Base64.encodeToString(key.getEncoded())
    base64Str
  }
  def loadKeyAES(base64Key: String): SecretKey = {
    val bytes = Base64.decode(base64Key)
    val key = new SecretKeySpec(bytes, "AES")
    return key
  }

The error says,

Type value encodeToString is not a member of object java.util.Base64
Type value decode is not a member of object java.util.Base64

The package I imported is java.util.Base64 How can I solve this problem?

Upvotes: 0

Views: 1336

Answers (1)

Harald Gliebe
Harald Gliebe

Reputation: 7564

Replace

Base64.encodeToString(key.getEncoded())

by

Base64.getEncoder.encodeToString(key.getEncoded())

and

Base64.decode(base64Key)

by

Base64.getDecoder.decode(base64Key)

Upvotes: 3

Related Questions