簡體   English   中英

如何使用Ionic對IOS進行RSA加密

[英]How to do RSA encryption for IOS using Ionic

我正在開發一個原生的Android應用程序和混合IOS應用程序。 我在向BL發送請求之前加密密碼。 以下是我的原生代碼。

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();
    }

我的密鑰在encryption.der文件中。 在android中一切正常。 現在來到IOS,我正在使用Ionic開發。 我無法實現加密部分。 我使用過“cryptico”:鏈接: https//github.com/wwwtyro/cryptico/blob/master/README.md 最后像這樣轉換為Base64。

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

但是我從BL得到了ArrayIndexOutOfBound Exception。 你能建議完全相同的解決方案有android for angularjs。 因此RSA加密適用於IOS和Android。

  1. 創建一個服務並將您的公鑰放在其中。

    .service('Settings',function(){

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

    1. 現在在你的JS加密中使用公鑰和JSEncrypt。

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

EncryptionPin是最后一把鑰匙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM