Guest User

Untitled

a guest
Aug 7th, 2026
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.55 KB | None | 0 0
  1. package com.mishiranu.dashchan.chan.dvach;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.security.keystore.KeyGenParameterSpec;
  5. import android.util.Base64;
  6. import chan.util.StringUtils;
  7. import com.mishiranu.dashchan.content.MainApplication;
  8. import java.io.IOException;
  9. import java.nio.charset.StandardCharsets;
  10. import java.security.GeneralSecurityException;
  11. import java.security.InvalidAlgorithmParameterException;
  12. import java.security.KeyStore;
  13. import java.security.KeyStoreException;
  14. import java.security.MessageDigest;
  15. import java.security.NoSuchAlgorithmException;
  16. import java.security.NoSuchProviderException;
  17. import java.security.cert.CertificateException;
  18. import java.util.Arrays;
  19. import javax.crypto.Cipher;
  20. import javax.crypto.KeyGenerator;
  21. import javax.crypto.SecretKey;
  22. import javax.crypto.spec.GCMParameterSpec;
  23.  
  24. /* JADX INFO: compiled from: r8-map-id-69b0d98fa0c655d6ba582ff17a9bb2b718caed2d43ea8140768c75d96d37a9a5 */
  25. /* JADX INFO: loaded from: classes.dex */
  26. final class DvachNoCaptchaSecretStore {
  27.     private static final String KEYSTORE_PROVIDER = "AndroidKeyStore";
  28.     private static final String KEY_ALIAS = "slooop_dvach_appid_demo_key_v1";
  29.     private static final String KEY_CIPHERTEXT = "ciphertext";
  30.     private static final String KEY_IV = "iv";
  31.     static final String PREFERENCES_NAME = "dvach_no_captcha_secret";
  32.     private static final String TRANSFORMATION = "AES/GCM/NoPadding";
  33.  
  34.     private DvachNoCaptchaSecretStore() {
  35.     }
  36.  
  37.     public static void clear() {
  38.         getPreferences().edit().clear().commit();
  39.         try {
  40.             KeyStore keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER);
  41.             keyStore.load(null);
  42.             if (keyStore.containsAlias(KEY_ALIAS)) {
  43.                 keyStore.deleteEntry(KEY_ALIAS);
  44.             }
  45.         } catch (IOException | RuntimeException | GeneralSecurityException unused) {
  46.         }
  47.     }
  48.  
  49.     private static byte[] decrypt() throws Throwable {
  50.         byte[] bArrDecode;
  51.         Throwable th;
  52.         byte[] bArrDecode2;
  53.         SharedPreferences preferences = getPreferences();
  54.         String string = preferences.getString(KEY_IV, null);
  55.         String string2 = preferences.getString(KEY_CIPHERTEXT, null);
  56.         if (string != null && string2 != null) {
  57.             try {
  58.                 bArrDecode = Base64.decode(string, 2);
  59.                 try {
  60.                     bArrDecode2 = Base64.decode(string2, 2);
  61.                     try {
  62.                         try {
  63.                             SecretKey key = getKey(false);
  64.                             if (key == null) {
  65.                                 if (bArrDecode != null) {
  66.                                     Arrays.fill(bArrDecode, (byte) 0);
  67.                                 }
  68.                                 if (bArrDecode2 != null) {
  69.                                     Arrays.fill(bArrDecode2, (byte) 0);
  70.                                 }
  71.                                 return null;
  72.                             }
  73.                             Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  74.                             cipher.init(2, key, new GCMParameterSpec(128, bArrDecode));
  75.                             byte[] bArrDoFinal = cipher.doFinal(bArrDecode2);
  76.                             if (bArrDecode != null) {
  77.                                 Arrays.fill(bArrDecode, (byte) 0);
  78.                             }
  79.                             if (bArrDecode2 != null) {
  80.                                 Arrays.fill(bArrDecode2, (byte) 0);
  81.                             }
  82.                             return bArrDoFinal;
  83.                         } catch (IOException | RuntimeException | GeneralSecurityException unused) {
  84.                             if (bArrDecode != null) {
  85.                                 Arrays.fill(bArrDecode, (byte) 0);
  86.                             }
  87.                             if (bArrDecode2 != null) {
  88.                                 Arrays.fill(bArrDecode2, (byte) 0);
  89.                             }
  90.                             return null;
  91.                         }
  92.                     } catch (Throwable th2) {
  93.                         th = th2;
  94.                         if (bArrDecode != null) {
  95.                             Arrays.fill(bArrDecode, (byte) 0);
  96.                         }
  97.                         if (bArrDecode2 != null) {
  98.                             Arrays.fill(bArrDecode2, (byte) 0);
  99.                         }
  100.                         throw th;
  101.                     }
  102.                 } catch (IOException | RuntimeException | GeneralSecurityException unused2) {
  103.                     bArrDecode2 = null;
  104.                 } catch (Throwable th3) {
  105.                     th = th3;
  106.                     bArrDecode2 = null;
  107.                 }
  108.             } catch (IOException | RuntimeException | GeneralSecurityException unused3) {
  109.                 bArrDecode2 = null;
  110.                 bArrDecode = null;
  111.             } catch (Throwable th4) {
  112.                 bArrDecode = null;
  113.                 th = th4;
  114.                 bArrDecode2 = null;
  115.             }
  116.         }
  117.         return null;
  118.     }
  119.  
  120.     private static SecretKey getKey(boolean z) throws NoSuchAlgorithmException, IOException, KeyStoreException, CertificateException, NoSuchProviderException, InvalidAlgorithmParameterException {
  121.         KeyStore keyStore = KeyStore.getInstance(KEYSTORE_PROVIDER);
  122.         keyStore.load(null);
  123.         SecretKey secretKey = (SecretKey) keyStore.getKey(KEY_ALIAS, null);
  124.         if (secretKey != null || !z) {
  125.             return secretKey;
  126.         }
  127.         KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", KEYSTORE_PROVIDER);
  128.         keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_ALIAS, 3).setKeySize(256).setBlockModes("GCM").setEncryptionPaddings("NoPadding").setRandomizedEncryptionRequired(true).build());
  129.         return keyGenerator.generateKey();
  130.     }
  131.  
  132.     private static SharedPreferences getPreferences() {
  133.         return MainApplication.getInstance().getSharedPreferences(PREFERENCES_NAME, 0);
  134.     }
  135.  
  136.     public static boolean hasPrivateKey() throws Throwable {
  137.         byte[] bArrDecrypt = decrypt();
  138.         if (bArrDecrypt == null) {
  139.             return false;
  140.         }
  141.         try {
  142.             return bArrDecrypt.length > 0;
  143.         } finally {
  144.             Arrays.fill(bArrDecrypt, (byte) 0);
  145.         }
  146.     }
  147.  
  148.     public static boolean put(String str) {
  149.         byte[] bytes = str != null ? str.getBytes(StandardCharsets.UTF_8) : null;
  150.         if (bytes != null && bytes.length != 0) {
  151.             try {
  152.                 try {
  153.                     Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  154.                     cipher.init(1, getKey(true));
  155.                     byte[] bArrDoFinal = cipher.doFinal(bytes);
  156.                     try {
  157.                         boolean zCommit = getPreferences().edit().putString(KEY_IV, Base64.encodeToString(cipher.getIV(), 2)).putString(KEY_CIPHERTEXT, Base64.encodeToString(bArrDoFinal, 2)).commit();
  158.                         Arrays.fill(bArrDoFinal, (byte) 0);
  159.                         Arrays.fill(bytes, (byte) 0);
  160.                         return zCommit;
  161.                     } catch (Throwable th) {
  162.                         Arrays.fill(bArrDoFinal, (byte) 0);
  163.                         throw th;
  164.                     }
  165.                 } catch (Throwable th2) {
  166.                     Arrays.fill(bytes, (byte) 0);
  167.                     throw th2;
  168.                 }
  169.             } catch (IOException | RuntimeException | GeneralSecurityException unused) {
  170.                 Arrays.fill(bytes, (byte) 0);
  171.             }
  172.         }
  173.         return false;
  174.     }
  175.  
  176.     public static String sign(String str) throws Throwable {
  177.         Throwable th;
  178.         byte[] bArrDigest;
  179.         byte[] bArrDecrypt = decrypt();
  180.         if (bArrDecrypt != null && bArrDecrypt.length != 0) {
  181.             byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
  182.             byte[] bArr = new byte[bytes.length + 1 + bArrDecrypt.length];
  183.             System.arraycopy(bytes, 0, bArr, 0, bytes.length);
  184.             bArr[bytes.length] = 124;
  185.             System.arraycopy(bArrDecrypt, 0, bArr, bytes.length + 1, bArrDecrypt.length);
  186.             try {
  187.                 bArrDigest = MessageDigest.getInstance("SHA-256").digest(bArr);
  188.                 try {
  189.                     String hex = StringUtils.formatHex(bArrDigest);
  190.                     Arrays.fill(bArrDecrypt, (byte) 0);
  191.                     Arrays.fill(bArr, (byte) 0);
  192.                     if (bArrDigest != null) {
  193.                         Arrays.fill(bArrDigest, (byte) 0);
  194.                     }
  195.                     return hex;
  196.                 } catch (RuntimeException | GeneralSecurityException unused) {
  197.                     Arrays.fill(bArrDecrypt, (byte) 0);
  198.                     Arrays.fill(bArr, (byte) 0);
  199.                     if (bArrDigest != null) {
  200.                         Arrays.fill(bArrDigest, (byte) 0);
  201.                     }
  202.                     return null;
  203.                 } catch (Throwable th2) {
  204.                     th = th2;
  205.                     Arrays.fill(bArrDecrypt, (byte) 0);
  206.                     Arrays.fill(bArr, (byte) 0);
  207.                     if (bArrDigest != null) {
  208.                         Arrays.fill(bArrDigest, (byte) 0);
  209.                     }
  210.                     throw th;
  211.                 }
  212.             } catch (RuntimeException | GeneralSecurityException unused2) {
  213.                 bArrDigest = null;
  214.             } catch (Throwable th3) {
  215.                 th = th3;
  216.                 bArrDigest = null;
  217.             }
  218.         }
  219.         return null;
  220.     }
  221. }
Add Comment
Please, Sign In to add comment