Android RSA解密使用私钥,无意义的结果

时间:2017-08-24 09:57:15

标签: java android rsa private-key

我正在我的Android项目中进行RSA解密,不知怎的,结果没有意义。我在这里展示我的代码:

private static final int MAX_DECRYPT_BLOCK = 256;


private static RSAPrivateKey loadPrivateKey(InputStream in) throws Exception {
    RSAPrivateKey priKey;
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String readLine = null;
        StringBuilder sb = new StringBuilder();
        while ((readLine = br.readLine()) != null) {
            if (readLine.charAt(0) == '-') {
                continue;
            } else {
                sb.append(readLine);
                sb.append('\r');
            }
        }
        byte[] priKeyData = Base64.decode(new String(sb), Base64.NO_WRAP);



        PKCS8EncodedKeySpec keySpec= new PKCS8EncodedKeySpec(priKeyData);
        KeyFactory keyFactory= KeyFactory.getInstance("RSA",new BouncyCastleProvider());
        priKey= (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
    } catch (IOException e) {
        throw new Exception("error reading the key");
    } catch (NullPointerException e) {
        throw new Exception("inputstream is null");
    }
    return priKey;
}


/**
 * decrypt with a private key
 *
 * @param privateKey
 * @param cipherData
 * @return
 * @throws Exception
 */
private static byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws Exception {
    if (privateKey == null) {
        throw new Exception("key is null");
    }
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        int inputLen = cipherData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;
        byte[] cache;
        int i = 0;
        while (inputLen - offSet > 0) {
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(cipherData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(cipherData, offSet, inputLen - offSet);
            }
            out.write(cache, 0, cache.length);
            i++;
            offSet = i * MAX_DECRYPT_BLOCK;
        }
        byte[] decryptedData = out.toByteArray();
        out.close();
        return decryptedData;

    } catch (NoSuchAlgorithmException e) {
        throw new Exception("no such algorithm");
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        return null;
    } catch (InvalidKeyException e) {
        throw new Exception("InvalidKeyException");
    } catch (IllegalBlockSizeException e) {
        throw new Exception("IllegalBlockSizeException");
    } catch (BadPaddingException e) {
        throw new Exception("BadPaddingException");
    }
}

public static String RSADecrypt(Context context, String KeyFileNameInAssetFolder, byte[] content) {
    try {
        InputStream inputStream = context.getResources().getAssets().open(KeyFileNameInAssetFolder);
        RSAPrivateKey privateKey = loadPrivateKey(inputStream);
        byte[] b = decrypt(privateKey, content);
        return new String(b,"utf-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "error";
}

我打电话给这句话:

String result = RSAUtils.RSADecrypt(getApplicationContext(), Constant.PRIVATE_KEY_PKCS8_FILE_NAME,Base64.decode(qrcode_result,Base64.NO_WRAP));

这是私钥:

> -----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCsQTbkXviKG1od
wVp0t15JB4OkdDUo8O36/yzLoJmsmPf3yFzAJ3YzEAaWAKKpUx92c9yfW4vW849F
pno/GUJLwPkIG5ss1YR55P5UI76kpLM74ZXume9+WfEuQ3B/HHkcnmCVl4CV/Xqq
4X4d3Rc7zGTbGSHdVui1QJ5ubQoqHjSGaO9j2SvMsqDSU20S1DjrL/8sb0e7B63a
fJVzOZFi7E62zqYzfBOEupY+24Ac8i7s8tyX3Bd4OJ1zdQ+si5yeIkaRiD8ZeUbJ
3yS2MlUohvuwmJy7uxpP8mYXmaXM1ZtMFLaLRUc4leA7KDRlDXHJLSYAkFxCyw8J
RTK2QOcTAgMBAAECggEARRrUnsHLC/z1JkLPu0tlM/8jvPIx8X7Wun9sxTRk8m1b
7bggHaa3ML0ZJ0yR9UQ3txm8ROJBM7b6n4KuQGotwp5kSfBpTI9MWmqX7cF5ViwN
C9TwhYyUHCiRLXI4y4XswKJ5NQpWt9W9RJi6M9ji3Uaen5dxko6vRSfrZ3mvP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D
-----END PRIVATE KEY-----

这是我试图解密的字符串:

JRhdX3DLGbMVcxN6jV3697yUkfZUBfz/ee6P8pGlgHFnOo5OWgXH0fc10Ps4li3UKkyVqo1+iz10/zzhjVTbSKC5Fai6dLnQgrFVz6lOqOeR83+x4ezyF75kORdgAyEp5MiW0LHsK13ryYEqZ1GHiZdJ6E54nbLZsZGKJJkRZ2OVQW9hovqBQXAP3M8dGk1liruiY+xAHfKkeS73m+IPYPTNGQT+y5IDACoq8dLUvV4nw76p2ZUfyYFgoYOir9KBN2SbfcndR71DZUPWdRnZoDLNFCfvMjC2Ui27j3CxcQLzJSt4K4K+kmU6n5t8Xb6YyGb2j+5pduXcnZMgc5cjsm6NBIXUv+DQzXeRo61vHrXKWeamJ+Whl0RnA9HjIl6medxfE64xHgF+aD6lqbQcwWwHWm/s3f5XkS0xP21bYuOt8mgmHC90qhJNRGKmTGgyxxls/18aV4eZEIRUg82wCIXavvQGLA3hk5UWl4YkrpaGYh+SX1t3yfi+wQ8f30lQBrKl4A0iKk4WHKNCka6nd3sc8bDwD42cvQiFSPCp7m4DRtXy1CRzNmRG7FmSK8SgkQOWSWE+6KRKat88j0Nw+Rg6U1YaMBofJOLKfIswLHgW44Bpf2c0eynJEZdLi94oOh7lkTHcaqwBiO1MWg9eiYT/j7qXCPjoi9q3PzPhxoA=

,解密结果如下:

result image

0 个答案:

没有答案