I need to encrypt a message to be sent to a cloud service by using a public key provided by the same service. This a the code snippet in question:
// Create public key from string
def publicKeyContent = "..."
def kf = java.security.KeyFactory.getInstance("RSA");
def keySpecX509 = new java.security.spec.X509EncodedKeySpec(java.util.Base64.getDecoder().decode(publicKeyContent))
def pubKey = kf.generatePublic(keySpecX509);
// Encrypt the message
def message = "Hello, World!"
def cipher = javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1Padding")
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, pubKey);
def encryptedBytes = cipher.doFinal(message);
Unfortunately Hubitat doesn't allow "java.security.spec.X509EncodedKeySpec" to be imported... Not sure why that is the case; are there alternatives I can use?