- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 SECP-256K1(比特币的 ECC 曲线)对某些数据进行 SHA256withECDSA 签名,代码如下。这些代码并不总是能正常工作,有时可能会相当不稳定。
我需要帮助稳定下面的代码。
签名者端生成ECC key 对的代码:
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256k1"));
ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParameterSpec);
KeyPair ecKeyPair = keyGen.generateKeyPair();
ECPublicKey deviceEcPub = (ECPublicKey) ecKeyPair.getPublic();
ECPrivateKey deviceEcPriv = (ECPrivateKey) ecKeyPair.getPrivate();
将ECC公钥转换为04<Public X Axis><Public Y Axis>
按照比特币公钥规范的格式:
int cursor = 0;
byte[] devicePubKey = new byte[65];
devicePubKey[cursor] = (byte) 0x04;
cursor++;
System.arraycopy(deviceEcPub.getW().getAffineX().toByteArray(), 0, devicePubKey, cursor, 32);
cursor += 32;
System.arraycopy(deviceEcPub.getW().getAffineY().toByteArray(), 0, devicePubKey, cursor, 32);
在签名者端签署一些数据:
Signature sig = Signature.getInstance("SHA256withECDSA");
sig.initSign(deviceEcPriv);
sig.update(data, 0, data.length);
byte[] signature = sig.sign();
验证者端收到04<Public X Axis><Public Y Axis>
编码公钥并将 X,Y 坐标转换为实际的 ECC 公钥以供使用:
byte[] x = new byte[32];
byte[] y = new byte[32];
// Skips the first byte containing the 0x04 byte and copies the rest
System.arraycopy(devicePubKey, 1, x, 0, 32);
System.arraycopy(devicePubKey, 33, y, 0, 32);
验证者使用 X 和 Y 坐标并重建 ECPublicKey:
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256k1"));
ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(x), new BigInteger(y)), ecParameterSpec);
ECPublicKey publicKey = (ECPublicKey) KeyFactory.getInstance("EC").generatePublic(ecPublicKeySpec);
验证者对提供的数据和签名执行验证:
Signature sig = Signature.getInstance("SHA256withECDSA");
sig.initVerify(publicKey);
sig.update(data, offset, length);
return sig.verify(signature);
验证结果有时是True
有时False
看似随机的事件。
我想对上述代码提出建议,以确保它没有不稳定的行为。
最佳答案
好的,我注意到如果第一个字节以 0x00 开头,则 X 或 Y 的长度将为 33 个字节,因此我无法正确复制 X 或 Y 字节。
为了缓解上述情况,其中 X 和 Y 中的一个或两个的第一个字节可以是 0x00,我将读取 X 和 Y 的第一个字节,如果第一个字节是 0x00,我将跳过复制第一个字节并读取尽可能多的字节,直到完全复制 X 和 Y 的每个 32 个字节。
在某些情况下,X 和/或 Y 可能会小于 32 字节(即只有 31 字节长)
另一种方法是测量长度。如果长度超过 32,则跳过字节直到最后 32 个字节。如果长度小于32,则复制字节并将0x00字节放在字节数组的前面。
下面的代码显示了我提取公共(public) X 和 Y 值的重新实现。
int cursor = 0;
int copy = 0;
// Get X
byte[] X = deviceEcPub.getW().getAffineX().toByteArray();
// Evaluate length
if (X.length > 32) {
copy = 1;
System.arraycopy(X, copy, devicePubKey, cursor, 32);
} else if (X.length < 32) {
System.arraycopy(X, copy, devicePubKey, cursor + (32 - X.length), X.length);
} else {
System.arraycopy(X, copy, devicePubKey, cursor, 32);
}
// Increment cursor
cursor += 32;
// Reset copy offset status
copy = 0;
// Get Y
byte[] Y = deviceEcPub.getW().getAffineY().toByteArray();
// Evaluate length
if (Y.length > 32) {
copy = 1;
System.arraycopy(Y, copy, devicePubKey, cursor, 32);
} else if (Y.length < 32) {
System.arraycopy(Y, copy, devicePubKey, cursor + (32 - Y.length), Y.length);
} else {
System.arraycopy(Y, copy, devicePubKey, cursor, 32);
}
结果是签名验证变得稳定。
关于java - ECC 数字签名代码中的不稳定行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745269/
我正在尝试找出如何检查安装的内存 (RAM) 是 ECC 还是非 ECC我需要通过使用 WMI 类的 C# 来做到这一点。 你们有什么想法吗? 提前谢谢 最佳答案 您可以查询 WMI Win32_Ph
Kotlin 中是否有任何关于椭圆曲线加密的信息? 用于生成 key 对和加密、解密消息。 关于这个主题的信息很少甚至没有。 例如,我想实现 ECC P-521 椭圆曲线。 是否可以在 Kotlin
这里是我用 Java 编写的代码,但签名生成和验证的过程给出了不同的结果。 请任何人帮助解决这个问题。我将非常感谢您的支持。 package ecdsa.draft; import java.math
大家好,请原谅我的英语我开发了一个大整数函数 c : #ifndef __BIGINTEGER_H #define __BIGINTEGER_H #include #include #include
我目前正在使用双相加算法编写椭圆曲线加密的 C 代码。我面临着我不明白的段错误问题。我希望你们中有人能有一个想法。 #include "lib/include/gmp.h" #include #in
我正在尝试使用 SECP-256K1(比特币的 ECC 曲线)对某些数据进行 SHA256withECDSA 签名,代码如下。这些代码并不总是能正常工作,有时可能会相当不稳定。 我需要帮助稳定下面的代
我正在阅读 Christoffer Paares 书中关于椭圆曲线密码学的部分(“理解密码学”)。我决定在 python 中实现一个用于椭圆曲线点加法和点加倍的函数。对于我的测试,我使用了书中的示例,
想起来很久没写博客了,刚好今天要写实验报告,随便把之前的也完成吧 1.椭圆曲线概念 椭圆曲线在经过化解后,可以用这条式子表达:E:y²=x³+ax+b 其背后的密码学原理,是基
我有 48 字节 ECC secp192r1 签名,可以在其他环境中工作: byte[] signature = new byte[]{(byte)0x08, (byte)0x33, (byte)0x
您好,我正在尝试解码包含 ECC 公钥的对象 但我去 umarshaling 错误说不能正确解码对象。 我正在尝试执行以下操作: var privateKey *ecdsa.PrivateKey va
最新版本的 Java 不支持 ECC Brainpool 曲线。当我读取包含 EC Curve Brainpool 的 X509Certificate 时,出现异常。 我发现 Java 不支持带有 B
java.security.PublicKey#getEncoded() 返回 key 的 X509 表示,与原始 ECC 值相比,在 ECC 的情况下增加了大量开销。 我希望能够以最紧凑的表示形式(
是否有针对 Java 的 ECC(纠错代码)库(例如 Reed-Solomon)的众所周知的实现,它具有友好的开源许可(非 GPL)? 最佳答案 zxing Apache 许可证(不确定这是否符合您对
大多数可用的桌面(廉价)x86 平台现在仍然不支持 ECC 内存 (Error Checking & Correction)。但内存位翻转错误率仍在增长(not the best SO thread,
我正在尝试使用 iOS 上的 KeyChain 创建 Curve25519 key 。我知道 CryptoKit 的存在,不幸的是,它不适用于 iOS 12。有没有办法在 CryptoKit 之前创建
我们需要将数据从 ECC 表实时提取到 Azure 数据湖。Azure 数据工厂具有与 SAP ECC 系统的连接选项,但不支持实时摄取。 请告诉我 Azure/SAP 中是否有任何可用的 nativ
我想要(或创建)基于椭圆 key 加密的序列 key 。我想做的是将信息编码在序列中,这些信息可以公开验证,但只能由我创建。最初的想法来自http://www.ssware.com/cryptolic
我在我的项目中使用 php OpenSSL。如何使用单个私钥创建多个公钥? 在 RSA 中我们不能做这样的事情。但是 ECC 呢? 最佳答案 根据定义,对于一般椭圆曲线密码系统中的每个私有(priva
我想(或创建)基于椭圆 key 加密的串行 key 。我想做的是在序列中编码信息,这些信息可以公开验证但只能由我创建。最初的想法来自http://www.ssware.com/cryptolicens
我在我的项目中使用 php OpenSSL。如何使用单个私钥创建多个公钥? 在 RSA 中我们不能做这样的事情。但是 ECC 呢? 最佳答案 根据定义,对于一般椭圆曲线密码系统中的每个私有(priva
我是一名优秀的程序员,十分优秀!