SecureKeyFactory.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.epson.mobilephone.common.keyenc;
  2. import android.content.Context;
  3. import android.util.Base64;
  4. import java.nio.charset.Charset;
  5. import java.security.MessageDigest;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.security.NoSuchProviderException;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.NoSuchPaddingException;
  10. import javax.crypto.spec.SecretKeySpec;
  11. public class SecureKeyFactory {
  12. static SecureKeyFactory instance;
  13. private final String ENCRYPT_ALGORITHM = "AES";
  14. private final int ENCRYPT_KEY_BYTES = 16;
  15. private final String ENCRYPT_METHOD = "AES/ECB/PKCS5Padding";
  16. private final String ENCRYPT_PROVIDER = "AndroidOpenSSL";
  17. private final Charset charset = Charset.forName("UTF8");
  18. private byte[] key = null;
  19. protected Context mAppContext = null;
  20. String mSecurityProvider = "AndroidOpenSSL";
  21. private native byte[] navite_getAppKey(Context context);
  22. private native byte[] navite_getKey(Context context);
  23. protected SecureKeyFactory(Context context) {
  24. try {
  25. System.loadLibrary("keyenclib");
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. mAppContext = context.getApplicationContext();
  30. key = navite_getKey(mAppContext);
  31. }
  32. public static SecureKeyFactory getInstance(Context context) {
  33. return getInstance(context, (String) null);
  34. }
  35. public static SecureKeyFactory getInstance(Context context, String str) {
  36. if (instance == null) {
  37. instance = new SecureKeyFactory(context);
  38. }
  39. if (str == null) {
  40. SecureKeyFactory secureKeyFactory = instance;
  41. secureKeyFactory.getClass();
  42. secureKeyFactory.mSecurityProvider = "AndroidOpenSSL";
  43. } else if (!instance.mSecurityProvider.equals(str)) {
  44. instance.mSecurityProvider = str;
  45. }
  46. return instance;
  47. }
  48. private Cipher getCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
  49. try {
  50. return Cipher.getInstance("AES/ECB/PKCS5Padding", mSecurityProvider);
  51. } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
  52. e.printStackTrace();
  53. return Cipher.getInstance("AES/ECB/PKCS5Padding");
  54. }
  55. }
  56. /* JADX WARNING: Code restructure failed: missing block: B:6:0x0018, code lost:
  57. r2 = android.util.Base64.encodeToString((r2 = encodeAes(r2.getBytes(r1.charset), r1.key)), 10);
  58. */
  59. /* Code decompiled incorrectly, please refer to instructions dump. */
  60. public java.lang.String Encode(java.lang.String r2) {
  61. /*
  62. r1 = this;
  63. byte[] r0 = r1.key
  64. if (r0 != 0) goto L_0x0007
  65. java.lang.String r2 = ""
  66. return r2
  67. L_0x0007:
  68. java.nio.charset.Charset r0 = r1.charset
  69. byte[] r2 = r2.getBytes(r0)
  70. byte[] r0 = r1.key
  71. byte[] r2 = r1.encodeAes(r2, r0)
  72. if (r2 != 0) goto L_0x0018
  73. java.lang.String r2 = ""
  74. return r2
  75. L_0x0018:
  76. r0 = 10
  77. java.lang.String r2 = android.util.Base64.encodeToString(r2, r0)
  78. if (r2 != 0) goto L_0x0023
  79. java.lang.String r2 = ""
  80. return r2
  81. L_0x0023:
  82. return r2
  83. */
  84. throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.keyenc.SecureKeyFactory.Encode(java.lang.String):java.lang.String");
  85. }
  86. public String Decode(String str) {
  87. byte[] decode;
  88. byte[] decodeAes;
  89. if (key == null || (decode = Base64.decode(str, 10)) == null || (decodeAes = decodeAes(decode, key)) == null) {
  90. return "";
  91. }
  92. return new String(decodeAes, charset);
  93. }
  94. private byte[] encodeAes(byte[] bArr, byte[] bArr2) {
  95. if (bArr == null || bArr2 == null) {
  96. return null;
  97. }
  98. try {
  99. byte[] makeKeyBytes = makeKeyBytes(16, bArr2);
  100. Cipher cipher = getCipher();
  101. cipher.init(1, new SecretKeySpec(makeKeyBytes, "AES"));
  102. return cipher.doFinal(bArr);
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. return null;
  106. }
  107. }
  108. private byte[] makeKeyBytes(int i, byte[] bArr) {
  109. byte[] bArr2 = new byte[i];
  110. for (int i2 = 0; i2 < bArr2.length; i2++) {
  111. if (i2 < bArr.length) {
  112. bArr2[i2] = bArr[i2];
  113. } else {
  114. bArr2[i2] = 0;
  115. }
  116. }
  117. return bArr2;
  118. }
  119. private byte[] decodeAes(byte[] bArr, byte[] bArr2) {
  120. if (bArr == null || bArr2 == null) {
  121. return null;
  122. }
  123. try {
  124. byte[] makeKeyBytes = makeKeyBytes(16, bArr2);
  125. Cipher cipher = getCipher();
  126. cipher.init(2, new SecretKeySpec(makeKeyBytes, "AES"));
  127. return cipher.doFinal(bArr);
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. return null;
  131. }
  132. }
  133. public static byte[] getMd5(byte[] bArr) {
  134. byte[] bArr2 = {0};
  135. try {
  136. MessageDigest instance2 = MessageDigest.getInstance("MD5");
  137. instance2.update(bArr);
  138. return instance2.digest();
  139. } catch (NoSuchAlgorithmException e) {
  140. e.printStackTrace();
  141. return bArr2;
  142. }
  143. }
  144. }