简体   繁体   中英

How to do RSA encryption for IOS using Ionic

I'm developing a native android app and hybrid IOS app. I'm encrypting the password before sending the request to BL. Below is my native code.

public String Encrypt (String plain) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
    try {
        AssetManager assets = context.getAssets();
        byte[] key = readFully(
                assets.open("encryption.der", AssetManager.ACCESS_BUFFER));
        KeySpec publicKeySpec = new X509EncodedKeySpec(key);

        KeyFactory kf = KeyFactory.getInstance("RSA");
        Key pk = kf.generatePublic(publicKeySpec);

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pk);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        CipherOutputStream cout = new CipherOutputStream(out, cipher);
        try {
            cout.write(plain.getBytes(UTF_8));
            cout.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                cout.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        }
        encrypted  = new String(encode(out.toByteArray(), DEFAULT), "UTF-8");

        return encrypted;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }

    return null;

}

static byte[] readFully(InputStream inputStream) throws IOException {
        InputStream in = new BufferedInputStream(inputStream);
        byte[] tmp = new byte[1024];
        int readLen, size = 0;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while ((readLen = in.read(tmp)) != -1) {
            if (((long) size + readLen) > Integer.MAX_VALUE) {
                // woah! did we just ship an app of 2GB?
                throw new IllegalStateException("Invalid file. File size exceeds expected "
                        + "maximum of 2GB");
            }
            size += readLen;
            out.write(tmp, 0, readLen);
        }
        return out.toByteArray();
    }

I have my key in encryption.der file. Everything works fine in android. Now coming to IOS which I'm using Ionic to develop. I'm not able to achieve the encryption part. I have used the "cryptico" : link : https://github.com/wwwtyro/cryptico/blob/master/README.md . And finally converting to Base64 like these.

var EncryptionPassword = cryptico.encrypt($scope.userInfo.Password, publicKey);
$scope.encPass = base64.encode(EncryptionPassword.cipher);

But I'm getting ArrayIndexOutOfBound Exception from BL. Can you suggest exact same solution has android for angularjs too. So RSA encrytion works on both IOS and Android.

  1. Create a Service and place your public Key inside that.

    .service('Settings', function(){

    this.publicKey = 'MIIBIjANBgdcssvsvsfvsfvsfvrefvfvfviuwoihijwfoiw278499080989i09M+KC8MYYOu/NRLmFg85LRrfRszyI/vZ/k8982789uiwbgchdbhU+3joQZoJ3Sxq/GbIIFf/3y4f9DuKI53y1qR2qD4xIskfa9rPVqvBtAu2KSNRd8V4J8RKI2gT2YEA+A3Z0mQq4GBRS8iYmGLqRQyPfNUSankylBrTpOIVFBZORdZehjJMmwl98UynyfnyMIHUIFuhefuibiufbeufbsoijn93fD7nxt+siZryfazn3EAgBaTKTV/U5xIepzDN6ZYJ4qnC93u6erdb1X4m1zU6RGapwzCOPOORTyzw/uWJ8twcODNt0cqVp+sYQIDAQAB'; })

    1. Now in your JS encrypt using public key and JSEncrypt.

    var encrypt = new JSEncrypt(); encrypt.setPublicKey(Settings.publicKey); EncryptionPin = encrypt.encrypt($scope.customerInfo.Pin);

EncryptionPin is the final key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM