【发布时间】:2017-12-27 02:30:50
【问题描述】:
我尝试在 Android 上编写代码来生成 RSA 密钥对,然后生成如下图所示的证书请求文件 (.csr):
-
首先我使用 spongycastle lib 生成密钥对(公钥和私钥)
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024,new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); publicKey = keyPair.getPublic(); privateKey = keyPair.getPrivate(); -
然后我使用在此link 上找到的
CSRHelper类生成:byte CSRder[]:byte CSRder[] = csr.getEncoded(); -
我编写代码将 byte[] 写入文件:
File file; FileOutputStream outputStream; try { file = new File(getCacheDir(),"csr.txt"); outputStream = new FileOutputStream(file); outputStream.write(CSRder); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } -
最后我写代码再次读取文件:
BufferedReader input = null; File file = null; try { file = new File(getCacheDir(), "csr.txt"); // Pass getFilesDir() and "MyFile" to read file input = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line; StringBuffer buffer = new StringBuffer(); while ((line = input.readLine()) != null) { buffer.append(line); } Log.d(TAG, buffer+""); } catch (IOException e) { e.printStackTrace(); }
但我的 logcat 显示不可读的字符。
07-21 13:48:35.163 16157-16157/com.example.napoleon.test_2 D/MainActivity: 0��0���0;10Unapoleon.com10UAralink10UOrgUnit0��0 *�H��������0�������Qt��G�]�ܪ�0�'�I^�Q��[�r5ڢ_!|������ZC��~<��*o�?�d+-����)��V�<߹��m��(��ѐxDcx��NhƬF��Ҵvq+���0�Iq�-Eoe,���"0 *�H�� 100U�0�0 *�H���������YPT3��?��P5MY��hs)��$1Gv�r_��76ߞ;���ҽ� t�kI�I��Z��tg����O�W��Gt�=���V���#G1�$z�$�V����_^7_x�?�0�#�;��f?�
如何将这个 csr 字节写入文件并再次读取?
【问题讨论】:
-
有关如何在 Android 上验证 CSR 内容的更多信息,请参阅 stackoverflow.com/questions/21912390/…。
标签: java android certificate csr spongycastle