-2

helo. any help can be hopeful please. it's about 1 month that i am tring to solve the problem. i have below private key by size 2048 and a function to sign some data.

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDMsFHds+5f72yW
hhzhCL8svzk7IAoSFZqPnTYoGNz8nN82sKwY5dhkB2x0Hw7dlJhs2V2+Y8nlS13E
fQQVT3NofdPVbZdCnwzrzz4VsT/0Yhg10FXNwXRzk4QTECQWaz5ZUqm2oXOBKG9R
DvgLKiuQVIgnRPrh57o7njkCxtnrJg3fOqRTAIopODrZbpDd1WRpYGJMD1eRLzfj
ZfXcuDypb4hRZ4P4qm7W7OIuwjzmaDiG9uvf7SYn8oLaGNMaX9TmiaPVqBWH5BLO
vtR+eRfq6LFpq/pyj8q7C8ODF3n6G8dhEXmScrkOn3QxWQUmlG6Fu5KItbJW72TD
iz/CflfPAgMBAAECggEACu+AmtpPafYIqcy7I5VWtrVLVsqnab/tugPULjWQg459
/mtXRnKWf6xF3GOsZPouCo1dDMbtX8AJtRhYwiq0fRNRNfMyRETrE84cAFyv5gaM
9JMPgnv2FMFU1Q/lCFhCbyiTbfEXkqxDsOOegjoxNEzP2V6TaMOl7M7PN5ltcGVH
jxG7mii3gj16czLX+R5XF8/exoEvrsA951gj8ZoetkRwBsPDgARgrlUr++CMd+pB
PX2YvHkLulPRmjzYjSNG9KcK4IdIqVz3yCUDkBwi03Oj6BvTLLU93nj5VpVnrnJt
AkDx4EFjz8wVZ5E2oS6UAbmsy/fUppVi1mrkLjXSQQKBgQDV6w29D581bV3nH/pT
3xQEBUZdUk+PNvg+5nBC8mTz7MITAqe7qsI1oHsAkuW4FepucY4c0UEnskb6mu6m
L7ohv+oc1ZKC/ERJs/xy2uHOfAjFNN2volB8A3D2EEhYJ/M1x8IlMXNhucWhUg2G
SlAq//3yEXcoljBvNLarWH+j2wKBgQD09HiNfGL2vESicbTut1LPqTKwedl06I0B
ZvkpuL+qldeyfXXqu6+hU0NO6YTzVJToIM2LcfpbzNs1VIfA84f1pFUU+OhAahEt
JFowhWRhDhXIQIYbZV8YPjiMv+pcOezZZZi6Qx9oytFBJ7sm8oC3UHzPCrIcrwym
UMzJK9jYHQKBgB4zBMl//xXnzq3mUcytXYIDzJ84E4tAU2Nt3MNWvPHCptNQOKXB
wZEYisTV+CA419OnrExdLjmQbXzLio1jh39kPIS9keVz/5/4DNeaFu6zi/lrMTCu
VXZsdfIG8m+flDIvdTUS+hr9wuy3TLl8nDy1zv/xf0T3jvbkFjWyVLKpAoGBALZv
ZWGq5GHzMgLyQNkRvCFhWJlLBwQlNu92a9gA+u7njrAm9K2Qf2vdgWHCVfyMYCLK
W4uhpd69If4EKN8yAW6Xp65o+0EOPiCq1GkS0AHg5aQxOjApPbvmi2N4g+mQUrBP
FIKU/02i0FqAZsjJD8h3r5zb0G/Ah+FOiFbDy0qZAoGAAezuaIyUIpdlZnZiroOh
ji4CWx3mxTwWSC7HNm8e85v+QFv/p/aIrH9Xhm2KboLVWPUGugsvd1E/jQMhiDwk
B7X1qlvbVpGvkE2nssP08uLpvfoUr53ONwRmaTEM5VvM150qY+dC5yn1bJSjcRjT
Kj460FDLWtMiX7uhKq1HkS8=
-----END PRIVATE KEY-----

hear is the function

public static String getSignedText(
        String text, String algorithm, PrivateKey privateKey) 
          throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, SignatureException { 
      byte[] data = text.getBytes("UTF8"); 
      Signature sig = Signature.getInstance(algorithm == null ? "SHA256WITHRSA" : algorithm); 
      sig.initSign(privateKey); 
      sig.update(data); 
      byte[] signatureBytes = sig.sign(); 
      return Base64.getEncoder().encodeToString(signatureBytes); 
}

i have tried some string to privatekey functions but non of them can convert the string key to privatekey type

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • 1
    Please tag the question with the programming language you are using... it looks like Java to me but I don't know. – Andy Preston Apr 29 '23 at 12:52
  • I've now seen this apparently malformed (according to lapo.it) ASN.1 private key format at least 3 times. Where is it coming from? – President James K. Polk Apr 29 '23 at 14:40
  • 1
    @PresidentJamesK.Polk: I don't see anything malformed (either in lapo.it or local openssl asn1parse), although lapo.it doesn't seem to have -- at least it doesn't offer me in the pulldown -- PKCS8-unencrypted, which is the (only) correct definition for this PEM-type (see RFC7468 section 10). – dave_thompson_085 Apr 29 '23 at 18:02
  • @PresidentJamesK.Polk: yes that dupe is better focussed on the specific issue than the ones my search found -- even though it should have matched my criteria. I've have had this problem before, where Stack's search doesn't find posts that exist and are good matches. I may give up using it. – dave_thompson_085 Apr 30 '23 at 21:29

1 Answers1

0

Although it's used with a public-key-cryptography algorithm, that's a private key (as the labels say) not a public key. It is almost in the format used by Java crypto: it is PKCS8 and unencrypted, but is PEM rather than 'DER' (binary). To un-PEM you need to remove the BEGIN and END lines, and decode the base64 to binary with the linebreaks either removed or ignored. You then put the result in PKCS8EncodedKeySpec and feed it to the .generatePrivate method of a KeyFactory for the correct algorithm, which is "RSA".

I'm certain I've seen this case posted at least a dozen times, but on searching I can find only two decent duplicates; see
https://stackoverflow.com/a/48173910/2868801 (but note Java 9 up moves javax.xml.bind outside of 'base', so using java.util.Base64 instead may be more convenient)
https://stackoverflow.com/a/66362542/2868801 (but skip the parts about JJWT, and note it assumes all linebreaks have been removed -- on the BEGIN and END lines and on the base64)

dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70
  • thanks alot. it worked. you saved the day for me. as you sed i removed line break and begin/end private.... and after 1 month by your help it worked :)))))))). – nima tehrani Apr 29 '23 at 19:47