在scala中,我有部分代码在做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
}
错误说,
Type value encodeToString is not a member of object java.util.Base64
Type value decode is not a member of object java.util.Base64
我导入的包是java.util.Base64
我该如何解决这个问题?
答案 0 :(得分:1)
替换
Base64.encodeToString(key.getEncoded())
通过
Base64.getEncoder.encodeToString(key.getEncoded())
和
Base64.decode(base64Key)
通过
Base64.getDecoder.decode(base64Key)