IOS中的AES解密

时间:2014-06-26 21:07:34

标签: java ios encryption aes

我有以下Java代码正确解密base64编码的有效负载:

private static byte[] decryptPBKDF2WithBC(char[] password, byte[] data, byte[] salt, byte[] iv)
      throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
      InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

    PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
    generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password), salt,1024);
    KeyParameter params = (KeyParameter)generator.generateDerivedParameters(256);

    byte[] endcoded = params.getKey();
    SecretKey key = new SecretKeySpec(endcoded, "AES");

    Cipher ciph = Cipher.getInstance("AES/CBC/PKCS5Padding");

    ciph.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
    return ciph.doFinal(data);
  }

  public String decrypt(String encrypted){
    String salt = SALT;
    String password = PASSWORD;
    String[] parts = encrypted.split("--");
    if (parts.length != 2) return null;

    byte[] encryptedData = Base64.decode(parts[0], Base64.DEFAULT);
    byte[] iv = Base64.decode(parts[1], Base64.DEFAULT);

    byte[] result = null;
    try {
      result = decryptPBKDF2WithBC(password.toCharArray(), encryptedData, salt.getBytes(), iv);
    } catch (InvalidKeyException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (InvalidKeySpecException e) {
      e.printStackTrace();
    } catch (NoSuchPaddingException e) {
      e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
      e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
      e.printStackTrace();
    } catch (BadPaddingException e) {
      e.printStackTrace();
    }
    catch(Exception e){
      e.printStackTrace();
    }

    try {
      return new String(result, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      return null;
    }
    catch(Exception e){
      e.printStackTrace();
      return null;
    }
  }

我遇到的问题是我需要将其移植到IOS。我在网上搜索过几个例子,但是我找不到一个使用密码,盐和IV的例子。是否有任何安全/ IOS专家对IOS加密库有足够的了解,至少指出了我正确的方向(使用哪些库或任何代码示例)?

1 个答案:

答案 0 :(得分:0)

尽可能使用RNCryptor

在iOS上使用CommonCrypto库进行解密以及NSData Base64方法。

您需要添加Security.framework,您可以查看方法的头文件以及更多信息。

相关问题