使用javaandroid将base64编码字符串转换为p12文件的正确方法

mlmc2os5  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(604)

我有一个base64编码的字符串p12文件,它是由api返回的。我试图保存到文件,但无法工作时,我打开它在我的手机。
哪里出错了:在解码字符串并将其保存到.p12文件之后。文件似乎已损坏,无法打开。我正在试图找到另一种方法来解码base64字符串以使其正常工作。
我试过的:
调用api以获取base64编码的字符串
解码它使用以下代码,我在搜索时得到(学分的所有者)。

  1. public static byte[] decode(String data)
  2. {
  3. int[] tbl = {
  4. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  5. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  6. -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
  7. 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2,
  8. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  9. 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30,
  10. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  11. 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  12. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  13. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  14. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  15. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  16. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  17. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  18. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  19. byte[] bytes = data.getBytes();
  20. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  21. for (int i = 0; i < bytes.length; ) {
  22. int b = 0;
  23. if (tbl[bytes[i]] != -1) {
  24. b = (tbl[bytes[i]] & 0xFF) << 18;
  25. }
  26. // skip unknown characters
  27. else {
  28. i++;
  29. continue;
  30. }
  31. int num = 0;
  32. if (i + 1 < bytes.length && tbl[bytes[i+1]] != -1) {
  33. b = b | ((tbl[bytes[i+1]] & 0xFF) << 12);
  34. num++;
  35. }
  36. if (i + 2 < bytes.length && tbl[bytes[i+2]] != -1) {
  37. b = b | ((tbl[bytes[i+2]] & 0xFF) << 6);
  38. num++;
  39. }
  40. if (i + 3 < bytes.length && tbl[bytes[i+3]] != -1) {
  41. b = b | (tbl[bytes[i+3]] & 0xFF);
  42. num++;
  43. }
  44. while (num > 0) {
  45. int c = (b & 0xFF0000) >> 16;
  46. buffer.write((char)c);
  47. b <<= 8;
  48. num--;
  49. }
  50. i += 4;
  51. }
  52. return buffer.toByteArray();
  53. }

使用此代码将其保存到文件中。

  1. public void writeBytesToFile(String encodedString)
  2. throws IOException {
  3. byte[] decodedBytes = decode(encodedString);
  4. File directory = new File("storage/emulated/0/", "Certificates");
  5. if(!directory.exists()){
  6. directory.mkdir();
  7. }
  8. Log.d("BYTS", new String(decodedBytes));
  9. String fileFilname = certTypeStr.concat("p12file.pem");
  10. //Toast.makeText(MyApplication.getAppContext(), fileFilname, Toast.LENGTH_SHORT).show();
  11. File file = new File("storage/emulated/0/Certificates", fileFilname);
  12. if(!file.exists()){
  13. boolean createfile = file.createNewFile();
  14. }else{
  15. boolean deletefile = file.delete();
  16. boolean createfile = file.createNewFile();
  17. }
  18. try (FileOutputStream fos = new FileOutputStream(file)) {
  19. fos.write(decodedBytes);
  20. }
  21. }

但似乎我没有运气让它工作。请帮助我正确地将base64字符串转换成p12文件。

67up9zun

67up9zun1#

对于那些试图做同样事情的人来说,经过几个小时的反复试验。我通过使用JavaProcessBuilder运行shell命令来解码base64字符串来实现这一点。使用的命令:

  1. base64 -d test.txt | base64 -d > test.p12

我不知道为什么base64解码的输出不同于windows和linux命令行,但是这个解决方法很有用。

相关问题