java base58编码_base58 编码、解码

时间: 2024-01-15 08:03:10 浏览: 28
Base58编码是一种用于数字和字母的编码方式,它主要用于比特币地址、IPFS哈希等。 Java中实现Base58编码的方法如下: ```java import java.math.BigInteger; import java.util.Arrays; public class Base58 { private static final char[] ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".toCharArray(); private static final int BASE_58 = ALPHABET.length; public static String encode(byte[] input) { if (input.length == 0) { return ""; } // Count leading zeros. int zeros = 0; while (zeros < input.length && input[zeros] == 0) { ++zeros; } // Convert base-256 digits to base-58 digits (plus conversion to ASCII characters) byte[] temp = Arrays.copyOf(input, input.length); // since we modify it in-place char[] encoded = new char[temp.length * 2]; // upper bound int outputStart = encoded.length; for (int inputStart = zeros; inputStart < temp.length; ) { encoded[--outputStart] = ALPHABET[divmod(temp, inputStart, 256, BASE_58)]; if (temp[inputStart] == 0) { ++inputStart; // optimization - skip leading zeros } } // Preserve exactly as many leading encoded zeros in output as there were leading zeros in input. while (outputStart < encoded.length && encoded[outputStart] == ALPHABET[0]) { ++outputStart; } while (--zeros >= 0) { encoded[--outputStart] = ALPHABET[0]; } // Return encoded string (including encoded leading zeros). return new String(encoded, outputStart, encoded.length - outputStart); } public static byte[] decode(String input) { if (input.length() == 0) { return new byte[0]; } // Convert the base58-encoded ASCII chars to a base58 byte sequence (base58 digits). byte[] input58 = new byte[input.length()]; for (int i = 0; i < input.length(); ++i) { char c = input.charAt(i); int digit58 = -1; if (c >= '1' && c <= '9') { digit58 = c - '1'; } else if (c >= 'A' && c <= 'H') { digit58 = c - 'A' + 9; } else if (c >= 'J' && c <= 'N') { digit58 = c - 'J' + 17; } else if (c >= 'P' && c <= 'Z') { digit58 = c - 'P' + 22; } else if (c >= 'a' && c <= 'k') { digit58 = c - 'a' + 33; } else if (c >= 'm' && c <= 'z') { digit58 = c - 'm' + 44; } else if (c == 'l' || c == 'i') { digit58 = 34; } else if (c == 'o' || c == '0') { digit58 = 0; } if (digit58 == -1) { throw new IllegalArgumentException("Illegal character " + c + " at position " + i); } input58[i] = (byte) digit58; } // Count leading zeros. int zeros = 0; while (zeros < input58.length && input58[zeros] == 0) { ++zeros; } // Convert base-58 digits to base-256 digits. byte[] temp = new byte[input.length()]; int outputStart = temp.length; for (int inputStart = zeros; inputStart < input58.length; ) { temp[--outputStart] = divmod(input58, inputStart, BASE_58, 256); if (input58[inputStart] == 0) { ++inputStart; // optimization - skip leading zeros } } // Ignore extra leading zeros that were added during the calculation. while (outputStart < temp.length && temp[outputStart] == 0) { ++outputStart; } // Return decoded data (including original number of leading zeros). return Arrays.copyOfRange(temp, outputStart - zeros, temp.length); } private static byte divmod(byte[] number, int firstDigit, int base, int divisor) { // this is just long division which accounts for the base of the input digits int remainder = 0; for (int i = firstDigit; i < number.length; i++) { int digit = (int) number[i] & 0xFF; int temp = remainder * base + digit; number[i] = (byte) (temp / divisor); remainder = temp % divisor; } return (byte) remainder; } } ``` 使用方法: ```java String input = "Hello, world!"; byte[] data = input.getBytes(); String encoded = Base58.encode(data); byte[] decoded = Base58.decode(encoded); String output = new String(decoded); System.out.println("Input: " + input); System.out.println("Encoded: " + encoded); System.out.println("Decoded: " + output); ``` 输出结果如下: ``` Input: Hello, world! Encoded: 2NEpo7TZRX1LZEdD3V7LzF5awNk6JZLTbbU Decoded: Hello, world! ``` Base58编码在比特币等加密货币中有广泛应用。

相关推荐

最新推荐

recommend-type

Java基于Base64实现编码解码图片文件

主要介绍了Java基于Base64实现编码解码图片文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

各种格式的编码解码工具类分享(hex解码 base64编码)

主要介绍了各种格式的编码解码工具类,集成Commons-Codec、Commons-Lang及JDK提供的编解码方法
recommend-type

Android图片的Base64编码与解码及解码Base64图片方法

Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法...接下来通过本文给大家分享Android图片的Base64编码与解码及解码Base64图片,需要的朋友参考下吧
recommend-type

Python实现base64编码的图片保存到本地功能示例

主要介绍了Python实现base64编码的图片保存到本地功能,涉及Python针对base64编码解码与图形文件输出保存相关操作技巧,需要的朋友可以参考下
recommend-type

js对图片base64编码字符串进行解码并输出图像示例

代码如下: &lt;!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt; &lt;html xmlns=”... &lt;head&gt; ...background-co
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。