How to do the RSA-2048 Encryption in iOS

Discussion in 'iOS Programming' started by Machunan, Dec 17, 2016.

  1. Machunan macrumors newbie

    Joined:
    Dec 17, 2016
    #1
    Is there any iOS alternative available for below Android Code
    public static String EncryptString(String value, String mod, String exp) {
    String strEncryInfoData = "";
    try {
    BigInteger modulus = new
    color:black">BigInteger(mod, 10);
    BigInteger pubExp = new
    color:black">BigInteger(exp, 10);

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
    RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherData = cipher.doFinal(value.getBytes());
    strEncryInfoData = new String(org.apache.commons.codec.binary.Base64.encodeBase64(cipherData));


    } catch
    (Exception e) {
    e.printStackTrace();
    }
    String result = strEncryInfoData.replaceAll("(\\r|\\n)", "");
    int count =result.length() ;
    return result;
    }
     
  2. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
    Code:
    public static String EncryptString(String value, String mod, String exp) {
    String strEncryInfoData = "";
        try {
            BigInteger modulus = new
    color:black">BigInteger(mod, 10);
            BigInteger pubExp = new
    color:black">BigInteger(exp, 10);
    
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
            RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] cipherData = cipher.doFinal(value.getBytes());
            strEncryInfoData = new String(org.apache.commons.codec.binary.Base64.encodeBase64(cipherData));
    
    
        } catch
    (Exception e) {
            e.printStackTrace();
        }
        String result = strEncryInfoData.replaceAll("(\\r|\\n)", "");
        int count =result.length() ;
        return result;
    }
    The menu bar at the top has a '+' button, that has an 'add code' option. If you post code, plz use the add code option to make it easy to read.

    I'm pretty sure iOS has all the encryption that Andoid has, you can also write in c/c++/ObjC/Swift. All kinds of encryption.

    Apple does have rules about what encryption can be used, IIRC they have a process to go thru.
     

Share This Page