如何从base64编码的字符串构造java.security.PublicKey对象? [英] How can I construct a java.security.PublicKey object from a base64 encoded string?

查看:748
本文介绍了如何从base64编码的字符串构造java.security.PublicKey对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自外部源(Android Store)的bse64encoded字符串公钥,我需要使用它来验证签名内容。如何将字符串转换为java.security.PublicKey接口的实例。如果这有所作为,我在Java 6上。

I have a bse64encoded string Public key from external source (Android Store) and I need to use it to verify signed content. How can I convert the string into an instance of the java.security.PublicKey interface. I am on Java 6 if that makes a difference.

关键是(可能)使用标准java lib而不是bouncy castle(它来自远程团队,所以我不确定)生成。他们的示例代码说使用Security.generatePublicKey(base64EncodedPublicKey);但是标准java中的Security对象没有这样的方法。

The key is (probably) generated using standard java lib and not bouncy castle (its from a remote team so I am not sure). Their sample code says to use Security.generatePublicKey(base64EncodedPublicKey); but the Security object in standard java has no such method.

推荐答案

好的笑话......试试这个

Ok for grins ... try this


  • base64解码密钥数据以获取字节数组(byte [])

  • 使用字节数组创建新的X509EncodedKeySpec

  • 使用KeyFactory.getInstance(RSA)获取KeyFactory实例,假设此处为RSA

  • 使用X509EncodedKeySpec

  • 结果调用方法generatePublic(KeySpec) /应该/是您使用的公钥。

  • base64 decode the key data to get a byte array (byte[])
  • Create a new X509EncodedKeySpec using the byte array
  • Get an instance of KeyFactory using KeyFactory.getInstance("RSA") assuming RSA here
  • call the method generatePublic(KeySpec) with the X509EncodedKeySpec
  • Result /should/ be a public key for your usage.

这篇关于如何从base64编码的字符串构造java.security.PublicKey对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆