Post

Replies

Boosts

Views

Activity

How to implement AES encryption in swift
Parallel Android code is below: public static String getEncryptedText(String plainText, SecretKey secretKey) { if(plainText == null) { plainText = ""; } try { byte[] ivBytes = new byte[GCM_IV_LENGTH]; SecureRandom random = new SecureRandom(); random.nextBytes(ivBytes); String iv = Base64.getEncoder().encodeToString(ivBytes); byte[] cipherText = encrypt(plainText.getBytes(), secretKey, ivBytes); String text = Base64.getEncoder().encodeToString(cipherText); text = iv+text; return text; } catch (Exception e) { e.printStackTrace(); return ""; } } private static byte[] encrypt(byte[] plaintext, SecretKey key, byte[] nonce) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES"); GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, nonce);
1
0
1.5k
Nov ’22