首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Ionic对IOS进行RSA加密

如何使用Ionic对IOS进行RSA加密
EN

Stack Overflow用户
提问于 2016-07-05 14:01:51
回答 1查看 1.5K关注 0票数 1

我正在开发一个本地android应用程序和混合IOS应用程序。我在给BL发送请求之前对密码进行加密。下面是我的本地代码。

代码语言:javascript
复制
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“:link:https://github.com/wwwtyro/cryptico/blob/master/README.md。最后像这样转换成Base64。

代码语言:javascript
复制
var EncryptionPassword = cryptico.encrypt($scope.userInfo.Password, publicKey);
$scope.encPass = base64.encode(EncryptionPassword.cipher);

但是我得到了BL的ArrayIndexOutOfBound异常。你能建议同样的解决方案也适用于angularjs。因此RSA加密既适用于IOS,也适用于Android。

EN

回答 1

Stack Overflow用户

发布于 2017-01-20 07:26:57

  1. 创建一个服务并将您的公钥放在其中。 .service(“设置”,函数()){ ( 'MIIBIjANBgdcssvsvsfvsfvsfvrefvfvfviuwoihijwfoiw278499080989i09M+KC8MYYOu/NRLmFg85LRrfRszyI/vZ/k8982789uiwbgchdbhU+3joQZoJ3Sxq/GbIIFf/3y4f9DuKI53y1qR2qD4xIskfa9rPVqvBtAu2KSNRd8V4J8RKI2gT2YEA+A3Z0mQq4GBRS8iYmGLqRQyPfNUSankylBrTpOIVFBZORdZehjJMmwl98UynyfnyMIHUIFuhefuibiufbeufbsoijn93fD7nxt+siZryfazn3EAgBaTKTV/U5xIepzDN6ZYJ4qnC93u6erdb1X4m1zU6RGapwzCOPOORTyzw/uWJ8twcODNt0cqVp+sYQIDAQAB';})
代码语言:javascript
复制
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是最终的关键。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38205468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档