将Java 120字符的ECDSA公钥转换为130字符的以太坊地址。

huangapple go评论85阅读模式
英文:

Java 120 char ECDSA public key to 130 char or Ethereum address

问题

我从外部系统获得了一个由ECDSA生成的120个字符的X509公钥。我现在想在以太坊中使用它,方法是将其转换为一个地址。

(这不是真实的密钥,只是内容的示例(120个字符))
MFYwEAYHKoZIzj0CAQYFK4EE123456789n9DSxZh3wfq0BIL5LDF5B54e07bxFiKc89K/GaKj4qrGC/Mb/KnakQBrN4khMQHLnxm7TjaxXQPxtJMV5b+A==

我无法看到使用web3j轻松完成这个任务的方法,也许还有其他方法?
我认为,通过查看测试,org.web3j.crypto.Keys.getAddress(String) 函数预期的是130个字符的十六进制版本。

如何将这120个字符转换为130个字符的十六进制表示,以便我能调用getAddress方法,或者也许有一种直接将这120个字符的公钥转换为以太坊地址的方法?

英文:

I am given a 120 char ECDSA generated X509 public key by an external system. I now want to use it in Ethereum by converting it to an address.

(not the real key but an example of the content (120 chars))
MFYwEAYHKoZIzj0CAQYFK4EE123456789n9DSxZh3wfq0BIL5LDF5B54e07bxFiKc89K/GaKj4qrGC/Mb/KnakQBrN4khMQHLnxm7TjaxXQPxtJMV5b+A==

I can't see an easy way of doing this with web3j, perhaps there is another way?
I think, looking at the tests, org.web3j.crypto.Keys.getAddress(String) expects the 130 character hex version.

How do I convert the 120 chars to a 130 char hex representation to allow me to call the getAddress method or maybe there is a direct way of converting the 120 char pub key to Ethereum address?

答案1

得分: 2

如果您有一个有效的Base64编码的SECP-256k1公钥,以下代码将帮助您使用Web3j获取以太坊地址。

String encodedKey = "MFYwEAYHKoZIzj0CAQYFK4EE123456789n9DSxZh3wfq0BIL5LDF5B54e07bxFiKc89K/GaKj4qrGC/Mb/KnakQBrN4khMQHLnxm7TjaxXQPxtJMV5b+A==";
byte[] decoded = Base64.getDecoder().decode(encodedKey);
String address = Keys.getAddress(Numeric.toHexString(decoded));

然而,正如在注释中提到的,似乎输入的编码密钥不是128位的公钥,而是X.509证书。因此,您需要执行类似以下的操作:

String encodedKey = "MFYwEAYHKoZIzj0CAQYFK4EE123456789n9DSxZh3wfq0BIL5LDF5B54e07bxFiKc89K/GaKj4qrGC/Mb/KnakQBrN4khMQHLnxm7TjaxXQPxtJMV5b+A==";

byte[] decoded = Base64.getDecoder().decode(encodedKey);

CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(decoded));

// TODO: Figure out how to convert this PublicKey into a byte array or BigInteger
byte[] publicKey = cert.getPublicKey();

String address = Keys.getAddress(Hex.toHexString(publicKey));

同时值得注意的是,OpenSSL命令行工具对于将证书转换为不同格式也非常有帮助。

英文:

If you had a valid Base64 encoded SECP-256k1 public key, the following code would enable you to get the Ethereum address using Web3j.

String encodedKey = "MFYwEAYHKoZIzj0CAQYFK4EE123456789n9DSxZh3wfq0BIL5LDF5B54e07bxFiKc89K/GaKj4qrGC/Mb/KnakQBrN4khMQHLnxm7TjaxXQPxtJMV5b+A==";
byte[] decoded = Base64.getDecoder().decode(encodedKey);
String address = Keys.getAddress(Numeric.toHexString(decoded));

However, as mentioned in the comments, it appears that the input encoded key is not a 128 bit public key, but an X.509 cert. Hence you'll need to do something along these lines:

String encodedKey = "MFYwEAYHKoZIzj0CAQYFK4EE123456789n9DSxZh3wfq0BIL5LDF5B54e07bxFiKc89K/GaKj4qrGC/Mb/KnakQBrN4khMQHLnxm7TjaxXQPxtJMV5b+A==";

byte[] decoded = Base64.getDecoder().decode(encodedKey);

CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(decoded));

// TODO: Figure out how to convert this PublicKey into a byte array or BigInteger
byte[] publicKey = cert.getPublicKey();

String address = Keys.getAddress(Hex.toHexString(publicKey));

It's also worth mentioning that the OpenSSL command line tool can be very helpful too for converting certificates into different formats.

huangapple
  • 本文由 发表于 2020年5月5日 17:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61609501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定