Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate Bitcoin wallet address in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to generate bitcoin wallet address in Java, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can learn something.

What is a Bitcoin address?

A Bitcoin address is a randomly found hexadecimal string that is used to send and receive bitcoin in a bitcoin network. It is the public part of a public-private asymmetric ECDSA key. The corresponding private key is used to sign the bitcoin transaction as confirmation and proof from you at the time of the transaction.

Technically, the bitcoin address is generated from the public part of the ECDSA key, using SHA-256 and RIPEMD-160 for hash, as described below, the resulting hash, and finally the key is encoded using Base58 parity coding.

Let's take a look at how to do all this using JCE (java encryption extension), Bouncy Castle (RIPEMD-160), and finally Base58 encoding in the bitcoinj library.

Generate ECDSA key pair

We have previously described generating RSA public and private keys. Bitcoin uses ECDSA instead of RSA as the key algorithm. It is generated as follows:

Create a KeyPairGenerator for the Elliptic Curve algorithm.

KeyPairGenerator keyGen = KeyPairGenerator.getInstance ("EC")

Use the specified elliptic curve to be secp256k1.

ECGenParameterSpec ecSpec = new ECGenParameterSpec ("secp256k1"); keyGen.initialize (ecSpec)

Once you have the KeyPairGenerator, you can create the KeyPair, the key pair, from which you can get the public and private keys.

KeyPair kp = keyGen.generateKeyPair (); PublicKey pub = kp.getPublic (); PrivateKey pvt = kp.getPrivate (); ECDSA private key

You can store only the private portion of the key because the public key can be derived from the private key.

ECPrivateKey epvt = (ECPrivateKey) pvt;String sepvt = adjustTo64 (epvt.getS (). ToString (16)) .toUpperCase (); System.out.println ("s [" + sepvt.length () + "]:" + sepvt)

The static method adjustTo64 () populates only hexadecimal strings with leading zeros, so the total length is 64 characters.

Static private String adjustTo64 (String s) {switch (s.length ()) {case 62: return "00" + s; case 63: return "0" + s; case 64: return s; default: throw new IllegalArgumentException ("not a valid key:" + s);}}

This is the sample private key generated by the above code.

S [64]: 024C8E05018319CED4BB04E184C307BFF115976A05F974C7D945B5151E490ADE

This value is usually stored by a digital wallet.

ECDSA public key

The public part of the key generated above is encoded as a bitcoin address. First, the ECDSA key is represented by points on the elliptic curve. The X and Y coordinates of the point include the public key. They are connected to "04" at the beginning to represent the public key.

ECPublicKey epub = (ECPublicKey) pub;ECPoint pt = epub.getW (); String sx = adjustTo64 (pt.getAffineX (). ToString (16)). ToUpperCase (); String sy = adjustTo64 (pt.getAffineY (). ToString (16)). ToUpperCase (); String bcPub = "04" + sx + sy;System.out.println ("bcPub:" + bcPub); # printsbcPub: 04CAAA5C0BDDAA22C9D3C0DDAEC8550791891BB2C2FB0F9084D02F927537DE4F443ACED7DEB488E9BFE60D6C68596E6C78D95E20622CC05474FD962392BDC6AF29 performs SHA-256 and RIPEMD-160 hashes

We now need to execute SHA-256 on the public key, then RIPEMD-160.

MessageDigest sha = MessageDigest.getInstance ("SHA-256"); byte [] S1 = sha.digest (bcPub.getBytes ("UTF-8")); System.out.println ("sha:" + bytesToHex (S1). ToUpperCase ()); # prints sha: 7524DC35AEB4B62A0F1C90425ADC6732A7C5DF51A72E8B90983629A7AEC656A0

We use the Bouncy Castle provider to execute RIPEMD-160 because JCE does not implement this algorithm.

MessageDigest rmd = MessageDigest.getInstance ("RipeMD160", "BC"); byte [] R1 = rmd.digest (S1)

Next, we need to add a version byte of 0x00 at the beginning of the hash.

Byte [] R2 = new byte [r1.length + 1]; R2 [0] = 0 for (int I = 0; I < r1.length; iSum +) R2 [iPre1] = R1 [I]; System.out.println ("rmd:" + bytesToHex (R2). ToUpperCase ()); # prints rmd: 00C5FAE41AB21FA56CFBAFA3AE7FB5784441D11CEC repeats the SHA-256 hash twice

We now need to perform two SHA-256 hashes on the above results.

Byte [] S2 = sha.digest (R2); System.out.println ("sha:" + bytesToHex (S2). ToUpperCase (); byte [] S3 = sha.digest (S2); System.out.println ("sha:" + bytesToHex (S3). ToUpperCase ())

The first 4 bytes of the second hash result are used as the address checksum. It is attached to the RIPEMD160 hash above. This is a 25-byte bitcoin address.

Byte [] A1 = new byte [25]; for (int I = 0; I < r2.length; iTunes +) A1 [I] = R2 [I]; for (int I = 0; I < 5; iLike +) A1 [20 + I] = S3 [I]; use Base58 to encode the address

We now use the Base58.encode () method in the bitcoinj library to get the final bitcoin address.

System.out.println ("adr:" + Base58.encode (A1)); # prints adr: 1K3pg1JFPtW7NvKNA77YCVghZRq2s1LwVF

This is the address to which Bitcoin should be sent in the transaction.

This is a demonstration of how to generate bitcoin addresses in java. We generate an ECDSA key pair that uses the public part of the SHA256 and RIPEMD160 hash keys. Finally, we calculate the checksum by executing SHA256 twice and selecting the first four bytes, which are appended to the above RIPEMD160 hash. The results were encoded using Base58 coding.

If you think it's a little complicated, you can also look at this Java to generate bitcoin addresses offline.

It is recommended that you visit our Huizhi Network's blockchain tutorials and blockchain technology blogs in various programming languages to learn more about blockchain, Bitcoin, cryptocurrency, ethernet, and smart contracts.

Java Bitcoin Development course, for beginners, covers the core concepts of Bitcoin, such as blockchain storage, decentralized consensus mechanisms, keys and scripts, transactions and UTXO, etc., as well as how to integrate Bitcoin support functions into Java code, such as creating addresses, managing wallets, constructing naked transactions, etc., is a rare bitcoin development course for Java engineers.

Php Bitcoin Development course, for beginners, covers the core concepts of Bitcoin, such as blockchain storage, decentralized consensus mechanisms, keys and scripts, transactions and UTXO, etc., as well as how to integrate Bitcoin support functions into Php code, such as creating addresses, managing wallets, constructing naked transactions, etc., is a rare bitcoin development course for Php engineers.

Php Ethernet Square, mainly introduces the use of php for intelligent contract development interaction, account creation, transaction, transfer, token development, filter and transaction and so on.

The java ethernet development tutorial is mainly for java and android programmers to conduct a detailed web3j explanation of blockchain ethernet development.

Introduction to Ethernet Square tutorial, mainly introduces the intelligent contract and dapp application development, suitable for entry.

Yi Tai Fang development advanced tutorial, mainly introduces the use of node.js, mongodb, block chain, ipfs to achieve decentralized e-commerce DApp practice, suitable for advanced.

Python ethernet, mainly for python engineers to use web3.py for block chain ethernet development of the detailed explanation.

C # Ethernet Square, mainly explains how to use C # to develop .net-based Ethernet Square applications, including account management, status and transactions, intelligent contract development and interaction, filters and transactions, etc.

EOS introduction course, this course helps you quickly get started with the development of EOS block chain decentralized applications, covering core knowledge points such as EOS tool chain, accounts and wallets, issuing tokens, smart contract development and deployment, using code and intelligent contract interaction, and finally using all knowledge points to complete the development of a note DApp.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report