rsa加密不适用于从xml文件获取密钥的情况

xiozqbni  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(257)

我一直在尝试用给定指数和模的rsa加密aes密钥。我正在从一个c#restful web服务获取密钥xml文件。xml键看起来有点像:

<RSAKeyValue><Modulus>vJaqEtwrfG...LFF7XACWCb6lQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

我正在解析xml,并使用以下函数将其保存在私有变量中:

public void initPublicKeyFromServer(Context context) {
    String strModulusBytes = "";
    String strExpBytes = "";

    Log.i("rsaEncryptSecretKey", "pos1");
    try {
        InputStream inputStream = context.openFileInput("puk.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_16);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String receiveString = bufferedReader.readLine();

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(receiveString));
        int eventType = xpp.getEventType();
        xpp.next();
        String xmlTag = xpp.getName();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            eventType = xpp.next();
            xmlTag = xpp.getName();
            if(eventType == XmlPullParser.START_TAG) {
                if (xmlTag.equals("RSAKeyValue")) {
                    continue;
                }
            }
            if(eventType == XmlPullParser.START_TAG) {
                if (xmlTag.equals("Modulus")) {
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                    strModulusBytes = xpp.getText();
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                }
            }
            if(eventType == XmlPullParser.START_TAG) {
                if (xmlTag.equals("Exponent")) {
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                    strExpBytes = xpp.getText();
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    byte[] modBytes = StandardCharsets.UTF_16.encode(strModulusBytes).array();
    byte[] expBytes = StandardCharsets.UTF_16.encode(strExpBytes).array();

    BigInteger modules = new BigInteger(1, modBytes);
    BigInteger exponent = new BigInteger(1, expBytes);
    try {
        KeyFactory factory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);
        publicKey = factory.generatePublic(pubSpec);
        rsaCipher = Cipher.getInstance(KeyPairInstanceType);
        rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public byte[] rsaEncrypt(final byte[] plain) throws Exception {
    rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedBytes = rsaCipher.doFinal(plain);
    return encryptedBytes;
}

public byte[] rsaEncrypt(final String plain) throws Exception {
    rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedBytes = rsaCipher.doFinal(plain.getBytes());
    return encryptedBytes;
}

我得到以下例外,我已经坚持了过去一周。你知道我怎么解决吗?
w/system.err:java.lang.runtimeexception:运行时异常:error:04000065:rsa公司routines:openssl_internal:错误的\u e \u值
这和utf-16编码有关吗?standardcharsets.utf_16.encode(strmodulusbytes).array();提前多谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题