Forum Stats

  • 3,733,650 Users
  • 2,246,800 Discussions
  • 7,856,826 Comments

Discussions

How to encrypt a key which is in the form of DER

imation3m
imation3m Member Posts: 177

I have a public key for asymmetric encryption in the form of DER (Distinguished Encoding Rules) encoded binary data, which is a restricted form of the ASN.1 (Abstract Syntax Notation One) encoding.

MIIB/DCCAWmgAwIBAgIQpbarEXfe8rVDlWlg2T+ixzAJBgUrDgMCHQUAMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwIBcNMTMwNzEwMDkxMjU5WhgPMjA5OTEyMjkyMDAwMDBaMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALZTB/2vKxWwCGhUdywVvikj8klvlzpZTJbVd0bRIN82bTTzp53SDXczc7mkto4vsqelGqnyjZcigyhj5y60SWYggc83d89I+i2Vo77am6aW8tfx1p/x9Op6bDLIN8V0uyoBK8IhRbuiugHmbP69Fyq4vXQ4+D2EzmmOuPRQfg4BAgMBAAGjTTBLMEkGA1UdAQRCMECAEAMkZd7uwQQG7803GjCmF7yhGjAYMRYwFAYDVQQDEw1CQU5LREhPRkFSMDAxghCltqsRd97ytUOVaWDZP6LHMAkGBSsOAwIdBQADgYEAhau3OD9QPoJm+H8v70WQmGUwJaS2IZORo/f8sMgUnVA6qoiD7BRkv8VVT0No4H+77YnYR2mtlCkU1BenKM3bC4WQXsXawMDSOoJcqBVLBFpYzl/8xpNrRyA8yyLUX37kXmH6mdioGLiNSKhQvX/XBYkTeOnsS2umt+zjS2JDS+g=

How I can encrypt this key to generate the cipher for text 1234.

I tried the below sample code from internet:

<span class="pun" style="color: #303336;">}</span><span class="kwd" style="color: #101094;">public</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">class</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">ATMPINClient</span><span class="pun" style="color: #303336;">{</span><span class="pln" style="color: #303336;"><br/><br/><br/>   </span><span class="kwd" style="color: #101094;">public</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">static</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">void</span><span class="pln" style="color: #303336;"> main</span><span class="pun" style="color: #303336;">(</span><span class="typ" style="color: #2b91af;">String</span><span class="pun" style="color: #303336;">[]</span><span class="pln" style="color: #303336;"> args</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">throws</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">Exception</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">{</span><span class="pln" style="color: #303336;"><br/><br/>   </span><span class="typ" style="color: #2b91af;">String</span><span class="pln" style="color: #303336;"> pkey </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> </span><span class="str" style="color: #7d2727;">"MIIB/DCCAWmgAwIBAgIQpbarEXfe8rVDlWlg2T+ixzAJBgUrDgMCHQUAMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwIBcNMTMwNzEwMDkxMjU5WhgPMjA5OTEyMjkyMDAwMDBaMBgxFjAUBgNVBAMTDUJBTktESE9GQVIwMDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALZTB/2vKxWwCGhUdywVvikj8klvlzpZTJbVd0bRIN82bTTzp53SDXczc7mkto4vsqelGqnyjZcigyhj5y60SWYggc83d89I+i2Vo77am6aW8tfx1p/x9Op6bDLIN8V0uyoBK8IhRbuiugHmbP69Fyq4vXQ4+D2EzmmOuPRQfg4BAgMBAAGjTTBLMEkGA1UdAQRCMECAEAMkZd7uwQQG7803GjCmF7yhGjAYMRYwFAYDVQQDEw1CQU5LREhPRkFSMDAxghCltqsRd97ytUOVaWDZP6LHMAkGBSsOAwIdBQADgYEAhau3OD9QPoJm+H8v70WQmGUwJaS2IZORo/f8sMgUnVA6qoiD7BRkv8VVT0No4H+77YnYR2mtlCkU1BenKM3bC4WQXsXawMDSOoJcqBVLBFpYzl/8xpNrRyA8yyLUX37kXmH6mdioGLiNSKhQvX/XBYkTeOnsS2umt+zjS2JDS+g="</span><span class="pun" style="color: #303336;">;</span><span class="pln" style="color: #303336;"><br/><br/>   </span><span class="typ" style="color: #2b91af;">String</span><span class="pln" style="color: #303336;"> atmPin </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> </span><span class="str" style="color: #7d2727;">"1234"</span><span class="pun" style="color: #303336;">;</span><span class="pln" style="color: #303336;"><br/>   </span><span class="kwd" style="color: #101094;">byte</span><span class="pun" style="color: #303336;">[]</span><span class="pln" style="color: #303336;"> key </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> pkey</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getBytes</span><span class="pun" style="color: #303336;">();</span><span class="pln" style="color: #303336;"><br/>  encrypt</span><span class="pun" style="color: #303336;">(</span><span class="pln" style="color: #303336;">atmPin</span><span class="pun" style="color: #303336;">,</span><span class="pln" style="color: #303336;"> key</span><span class="pun" style="color: #303336;">);</span><span class="pln" style="color: #303336;"><br/><br/><br/>   </span><span class="pun" style="color: #303336;">}</span><span class="pln" style="color: #303336;"><br/><br/>   </span><span class="kwd" style="color: #101094;">public</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">static</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">byte</span><span class="pun" style="color: #303336;">[]</span><span class="pln" style="color: #303336;"> encrypt</span><span class="pun" style="color: #303336;">(</span><span class="typ" style="color: #2b91af;">String</span><span class="pln" style="color: #303336;"> atmPin</span><span class="pun" style="color: #303336;">,</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">byte</span><span class="pun" style="color: #303336;">[]</span><span class="pln" style="color: #303336;"> keyCode</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"><br/>   </span><span class="kwd" style="color: #101094;">throws</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">Exception</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">{</span><span class="pln" style="color: #303336;"><br/>  X509Certificate cert </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> X509Certificate</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getInstance</span><span class="pun" style="color: #303336;">(</span><span class="pln" style="color: #303336;">keyCode</span><span class="pun" style="color: #303336;">);</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">RSAPublicKey</span><span class="pln" style="color: #303336;"> rk </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">(</span><span class="typ" style="color: #2b91af;">RSAPublicKey</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"> cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getPublicKey</span><span class="pun" style="color: #303336;">();</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Algorithm: "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> rk</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getAlgorithm</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Modules : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> rk</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getModulus</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Hex - Modules : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> rk</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getModulus</span><span class="pun" style="color: #303336;">().</span><span class="pln" style="color: #303336;">toString</span><span class="pun" style="color: #303336;">(</span><span class="lit" style="color: #7d2727;">16</span><span class="pun" style="color: #303336;">).</span><span class="pln" style="color: #303336;">toUpperCase</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Exponent : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> rk</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getPublicExponent</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Name : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getSubjectDN</span><span class="pun" style="color: #303336;">().</span><span class="pln" style="color: #303336;">getName</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Issuer Name : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getIssuerDN</span><span class="pun" style="color: #303336;">().</span><span class="pln" style="color: #303336;">getName</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Not After : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getNotAfter</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Not Before : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getNotBefore</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Format : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getPublicKey</span><span class="pun" style="color: #303336;">().</span><span class="pln" style="color: #303336;">getFormat</span><span class="pun" style="color: #303336;">());</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">Cipher</span><span class="pln" style="color: #303336;"> cipher </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">Cipher</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getInstance</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"RSA"</span><span class="pun" style="color: #303336;">);</span><span class="pln" style="color: #303336;"><br/><br/>  cipher</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">init</span><span class="pun" style="color: #303336;">(</span><span class="typ" style="color: #2b91af;">Cipher</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">ENCRYPT_MODE</span><span class="pun" style="color: #303336;">,</span><span class="pln" style="color: #303336;"> rk</span><span class="pun" style="color: #303336;">);</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Actual PIN : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> atmPin</span><span class="pun" style="color: #303336;">);</span><span class="pln" style="color: #303336;"><br/>   </span><span class="kwd" style="color: #101094;">byte</span><span class="pun" style="color: #303336;">[]</span><span class="pln" style="color: #303336;"> PIN </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> cipher</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">doFinal</span><span class="pun" style="color: #303336;">(</span><span class="pln" style="color: #303336;">atmPin</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getBytes</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"UTF-8"</span><span class="pun" style="color: #303336;">));</span><span class="pln" style="color: #303336;"><br/><br/>   </span><span class="typ" style="color: #2b91af;">String</span><span class="pln" style="color: #303336;"> encPIN </span><span class="pun" style="color: #303336;">=</span><span class="pln" style="color: #303336;"> </span><span class="kwd" style="color: #101094;">new</span><span class="pln" style="color: #303336;"> sun</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">misc</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">BASE64Encoder</span><span class="pun" style="color: #303336;">().</span><span class="pln" style="color: #303336;">encode</span><span class="pun" style="color: #303336;">(</span><span class="pln" style="color: #303336;">PIN</span><span class="pun" style="color: #303336;">);</span><span class="pln" style="color: #303336;"><br/>   </span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">out</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">println</span><span class="pun" style="color: #303336;">(</span><span class="str" style="color: #7d2727;">"Encrypted Pin : "</span><span class="pln" style="color: #303336;"> </span><span class="pun" style="color: #303336;">+</span><span class="pln" style="color: #303336;"> encPIN</span><span class="pun" style="color: #303336;">);</span><span class="pln" style="color: #303336;"><br/><br/>   </span><span class="kwd" style="color: #101094;">return</span><span class="pln" style="color: #303336;"> PIN</span><span class="pun" style="color: #303336;">;</span><span class="pln" style="color: #303336;"><br/><br/>   </span><span class="pun" style="color: #303336;">}</span><span class="pln" style="color: #303336;"><br/></span><span class="pun" style="color: #303336;"></span>

Getting below error:

<span class="typ" style="color: #2b91af;">Exception</span><span class="pln" style="color: #303336;"> in thread </span><span class="str" style="color: #7d2727;">"main"</span><span class="pln" style="color: #303336;"> javax</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">security</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">cert</span><span class="pun" style="color: #303336;">.</span><span class="typ" style="color: #2b91af;">CertificateException</span><span class="pun" style="color: #303336;">:</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">InvocationTargetException</span><span class="pun" style="color: #303336;">:</span><span class="pln" style="color: #303336;"> javax</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">security</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">cert</span><span class="pun" style="color: #303336;">.</span><span class="typ" style="color: #2b91af;">CertificateException</span><span class="pun" style="color: #303336;">:</span><span class="pln" style="color: #303336;"> invalid DER</span><span class="pun" style="color: #303336;">-</span><span class="pln" style="color: #303336;">encoded certificate data<br/>  at javax</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">security</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">X509Certificate</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getInst</span><span class="pun" style="color: #303336;">(</span><span class="pln" style="color: #303336;">X509Certificate</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">java</span><span class="pun" style="color: #303336;">:</span><span class="lit" style="color: #7d2727;">226</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"><br/>  at javax</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">security</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">cert</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">X509Certificate</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">getInstance</span><span class="pun" style="color: #303336;">(</span><span class="pln" style="color: #303336;">X509Certificate</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">java</span><span class="pun" style="color: #303336;">:</span><span class="lit" style="color: #7d2727;">186</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"><br/>  at atmpinclient</span><span class="pun" style="color: #303336;">.</span><span class="typ" style="color: #2b91af;">ATMPINClient</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">encrypt</span><span class="pun" style="color: #303336;">(</span><span class="typ" style="color: #2b91af;">ATMPINClient</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">java</span><span class="pun" style="color: #303336;">:</span><span class="lit" style="color: #7d2727;">33</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"><br/>  at atmpinclient</span><span class="pun" style="color: #303336;">.</span><span class="typ" style="color: #2b91af;">ATMPINClient</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">main</span><span class="pun" style="color: #303336;">(</span><span class="typ" style="color: #2b91af;">ATMPINClient</span><span class="pun" style="color: #303336;">.</span><span class="pln" style="color: #303336;">java</span><span class="pun" style="color: #303336;">:</span><span class="lit" style="color: #7d2727;">26</span><span class="pun" style="color: #303336;">)</span><span class="pln" style="color: #303336;"><br/></span><span class="typ" style="color: #2b91af;">Java</span><span class="pln" style="color: #303336;"> </span><span class="typ" style="color: #2b91af;">Result</span><span class="pun" style="color: #303336;">:</span><span class="pln" style="color: #303336;"> </span>

<span class="lit" style="color: #7d2727;">1</span>

Please advise how to achieve this encryption.

Tagged:

Answers

Sign In or Register to comment.