Android Question java.lang.RuntimeException: error:0c074079:ASN.1 encoding routines:ASN1_get_object:HEADER_TOO_LONG

Angelo Messina

Active Member
Licensed User
Longtime User
B4X:
 nativeMe.InitializeContext
 Dim Signature As String=nativeMe.RunMethod("getSHA256RSA",Array(Canonico,$"4N4DfFoKq9Cg25AVmCk3V5VfKypdinHE/nUWfBmXxhA="$))
 Log(Signature)

#If Java
import android.util.Base64;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.MessageDigest;
import java.util.Locale;
import android.text.TextUtils;
 public static String getSHA256RSA(String input, String strPk) throws Exception
   {
        // Remove markers and new line characters in private key
        String realPK = strPk.replaceAll("-----END PRIVATE KEY-----", "")
                             .replaceAll("-----BEGIN PRIVATE KEY-----", "")
                             .replaceAll("\n", "");
 byte[] b1 = realPK.getBytes("UTF-8");
 
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(b1);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        Signature privateSignature = Signature.getInstance("SHA256withRSA");
        privateSignature.initSign(kf.generatePrivate(spec));
        privateSignature.update(input.getBytes("UTF-8"));
        byte[] s = privateSignature.sign();
  return Base64.encodeToString(s, Base64.DEFAULT);
    }
#End If
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…