title: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag when encrypt RSA public key - passing from the other device
body: <p>I've receivced RSA public key from another device.
  and I create public key with these byte arrays for encrypt any strings.
  but my public key is invalid.</p>
  
  <p>retrevie many questions but i don't know why it happens.
  please help me.</p>
  
  <p>step is below.</p>
  
  <ol>
  <li>passing from public key - byte arrays - another device.</li>
  <li>generate PublicKey object with X509EncodedKeySpec.</li>
  </ol>
  
  <blockquote>
  <pre><code> StringBuilder builder = new StringBuilder();
   for ( byte key : mKey ) builder.append(key);
   byte[] keyBytes = Base64.decode(builder.toString().getBytes("utf-8"), Base64.DEFAULT);
  
   PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new
   X509EncodedKeySpec(keyBytes));
  </code></pre>
  </blockquote>
  
  <p>mKey are byte arrays of public key.</p>
  
  <p>but now, exception occured.</p>
  
  <p>exception :</p>
  
  <pre><code>03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
  03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:101)
  03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
  03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ at java.security.KeyFactory.generatePublic(KeyFactory.java:171)
  03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ at updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator.MainActivity.processGenerateKeyForUpdate(MainActivity.java:253)
  03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ at updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator.MainActivity.access$1600(MainActivity.java:53)
  03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ at updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator.MainActivity$5.handleMessage(MainActivity.java:411)
  03-23 15:23:53.468    5641-5641/updator.firmware.mpaio.samilcts.com.mpaiofirmwareupdator W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
  </code></pre>
  
  <p>cf.
  received PublicKey byte arrays :</p>
  
  <blockquote>
    <p>'0x47, 0x95, 0x1a, 0xe6, 0xf1, 0xba, 0x31, 0x3b, 0x73, 0xc7, 0x79,
    0x75, 0x72, 0xc6, 0x6d, 0xcc, 0xf0, 0x6b, 0x0f, 0xfa, 0x16, 0x0e,
    0xed, 0x2d, 0x85, 0xeb, 0x17, 0x89, 0x91, 0x83, 0x59, 0x64, 0xda,
    0x04, 0x69, 0x31, 0x45, 0x16, 0x73, 0x1b, 0x46, 0xab, 0xe0, 0x2d,
    0x6e, 0xda, 0x7d, 0x51, 0xc9, 0x2b, 0xc6, 0xe9, 0x73, 0x19, 0x2e,
    0x3d, 0x39, 0x12, 0xd6, 0x44, 0xf8, 0x71, 0xe0, 0x49, 0xc4, 0x54,
    0xd5, 0xd1, 0xe9, 0x1f, 0x03, 0x6f, 0xd6, 0x2b, 0xf9, 0x77, 0xd8,
    0x83, 0x7e, 0x9d, 0x1d, 0xf7, 0x46, 0x5d, 0xf4, 0xac, 0x9f, 0x6b,
    0xe7, 0x2a, 0x68, 0xb9, 0x55, 0x01, 0xd3, 0xde, 0xbb, 0x1b, 0x93,
    0x3b, 0x0a, 0x78, 0xe2, 0x06, 0x1c, 0x5d, 0x18, 0x61, 0x2d, 0x8f,
    0x23, 0x4e, 0x3c, 0xbb, 0x35, 0xa5, 0x99, 0xb4, 0x2f, 0x9b, 0x9d,
    0x2b, 0x67, 0xfb, 0x89, 0xba, 0x1c, 0x8f, 0x48, 0x9c, 0xa7, 0x99,
    0x5d, 0xa0, 0x11, 0xe6, 0x1d, 0x5a, 0xd2, 0x18, 0x03, 0xc5, 0x98,
    0xf5, 0x26, 0xb2, 0x14, 0xaa, 0xfd, 0x77, 0x8e, 0x78, 0x36, 0xc4,
    0x46, 0xb2, 0xea, 0xd0, 0xa2, 0x5d, 0xb9, 0xc5, 0xae, 0x47, 0x17,
    0xc1, 0x93, 0x42, 0x12, 0x9c, 0x3a, 0xa0, 0x71, 0xb5, 0x32, 0x51,
    0x74, 0xa0, 0x6d, 0x2b, 0xbe, 0xc8, 0x4f, 0xc4, 0xf3, 0x5f, 0xd3,
    0x4f, 0x6b, 0xf9, 0x7a, 0x5d, 0xfa, 0x93, 0x8f, 0xf2, 0x00, 0x1a,
    0x92, 0x18, 0x41, 0xd5, 0x83, 0x06, 0x98, 0xeb, 0x38, 0x86, 0xbe,
    0x3c, 0x20, 0xe5, 0x19, 0xdf, 0x8c, 0xac, 0x38, 0x83, 0x5e, 0xde,
    0x33, 0x62, 0x33, 0x14, 0xee, 0xe3, 0x6c, 0x6b, 0x02, 0xee, 0x0a,
    0xfe, 0x79, 0x5e, 0x32, 0x8c, 0x2e, 0x20, 0x05, 0x59, 0xed, 0x98,
    0x89, 0xf1, 0x9b, 0xbf, 0x5a, 0x21, 0x25, 0x1d, 0x51, 0x65, 0x0e,
    0xbd, 0x03, 0xde, '</p>
  </blockquote>
  
  <p>thank you.</p>