# Java×以太坊 1批量生成以太坊钱包

By [先圣](https://paragraph.com/@120.show) · 2023-07-11

---

### 观前提示：

©版权所有：本教程由[先圣](https://120.show/)编写，如需转载，请标明出处。

本文受众：Web3从业人员、程序员

浏览推荐：为了最佳观感，本文地址为：[https://blog.120.show/2023/5](https://blog.120.show/2023/5)

### 正文开始：

#### 准备环境：

> Java-JDK 1.8以上版本（建议IDEA集成）
> 
> Maven（建议IDEA集成）
> 
> IDEA编辑器（也可以使用VScode或其他编辑器）

#### 创建Maven项目：

本文使用[IDEA](https://www.jetbrains.com/idea/)版本为2022版，如有界面差异，请选择对应的版本。

打开IDEA、新建空项目、选择Maven+JDK1.8以上、创建。

![image-20230710232653248](https://storage.googleapis.com/papyrus_images/b41bf7ed79d05c133f8e1b2a1dd113c6a248b7dd74a4f9cde47ed99a636513b1.png)

image-20230710232653248

#### 添加Web3j依赖库：

在pom.xml文件中，添加以下依赖：

    <dependencies>
            <!-- web3J依赖 -->
            <dependency>
                <groupId>org.web3j</groupId>
                <artifactId>core</artifactId>
                <version>5.0.0</version>
            </dependency>
    </dependencies>
    

#### 复制主要代码：

创建一个名为GenerateEtherWallet的Java类，然后复制下面代码：

    import java.math.BigInteger;
    import java.security.*;
    import org.web3j.crypto.*;
    import org.web3j.utils.Numeric;
    
    public class GenerateEtherWallet {
        public static void main(String[] args) throws Exception {
            // 生成随机的私钥
            SecureRandom secureRandom = new SecureRandom();
            byte[] privateKeyBytes = new byte[32];
            secureRandom.nextBytes(privateKeyBytes);
            String privateKey = Numeric.toHexStringNoPrefix(privateKeyBytes);
    
            // 通过私钥生成公钥和地址
            ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
            String publicKey = keyPair.getPublicKey().toString(16);
            String address = Keys.getAddress(keyPair.getPublicKey());
    
            // 输出私钥、公钥和地址
            System.out.println("私钥：" + privateKey);
            System.out.println("公钥：" + publicKey);
            System.out.println("地址：" + address);
        }
    }
    

#### 运行项目：

按F10+Shift 或者 点击绿色三角形 开始运行项目。

控制台正常应该显示：

> 私钥：dce017abd29d8ba884794e72157718a90e2f72af1f6cc6329b8333e49e874f25 公钥：a583e78eb4aa39f1a7581e8febc9401aa2e66bc40d10e26f7cfc0af0e14dfedb80b57eed8180f485d0504a5ae796ee2d973831ecbbdaf0ca860d9951b1858e1c 地址：17a04150123b23cfb2c21b599e68d2a0d35dbf6b

#### 修改代码：

如果需要生成多个钱包，加入循环即可。如果不懂代码，可以简单复制下面的代码（for循环）：

    import java.math.BigInteger;
    import java.security.*;
    import org.web3j.crypto.*;
    import org.web3j.utils.Numeric;
    
    public class GenerateEtherWallet {
        public static void main(String[] args) throws Exception {
            // 修改中间的 i < 10 这一项，需要1000个就改成 i<1000
            for (int i = 0; i < 10; i++) {
                System.out.println("第" + (i+1) + "个钱包：");
                GenerateEtherWallet.newWallet();
            }
        }
    
        public static void newWallet(){
            // 生成随机的私钥
            SecureRandom secureRandom = new SecureRandom();
            byte[] privateKeyBytes = new byte[32];
            secureRandom.nextBytes(privateKeyBytes);
            String privateKey = Numeric.toHexStringNoPrefix(privateKeyBytes);
    
            // 通过私钥生成公钥和地址
            ECKeyPair keyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
            String publicKey = keyPair.getPublicKey().toString(16);
            String address = Keys.getAddress(keyPair.getPublicKey());
    
            // 输出私钥、公钥和地址
            System.out.println("私钥：" + privateKey);
            System.out.println("公钥：" + publicKey);
            System.out.println("地址：" + address);
        }
    }
    

具体看//后面的注释内容，i<10表示将运行10次方法，即生成10个钱包。

如果你需要10000个钱包，改成i<10000即可。

### 参考资料：

[https://chat.openai.com/](https://chat.openai.com/)

[https://ethereum.org/zh/developers/docs/programming-languages/java/](https://ethereum.org/zh/developers/docs/programming-languages/java/)

---

*Originally published on [先圣](https://paragraph.com/@120.show/java-1)*
