I want to perform RSA encryption&Decryption using SecKeyCreateEncryptedData() Method allows public key as SecKey, My back end Java code has been set as encryption using private key & decryption using public key. Here is a java code... @SuppressLint("NewApi") public static String encryptSecretKeyByPrivateKey(String data, String privatekeyPath) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { byte[] plaintext = data.getBytes(); PrivateKey privateKey = getPrivatekey(privatekeyPath); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] encryptedByte = cipher.doFinal(plaintext); return Base64.getEncoder().encodeToString(encryptedByte); }
public static PrivateKey getPrivatekey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
PrivateKey privateKey = null;
KeyFactory keyFactory = null;
byte[] encoded = DatatypeConverter.parseBase64Binary(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
Can I achieve the same in swift with the help of SecKeyEncrypt() or SecKeyCreateEncryptedData() ?