我正在尝试用Java实现Diffie-Hellman密钥交换,但我很难理解规范:
根据JWA(RFC 7518)在直接密钥协商模式下使用曲线P-256,dT和QC完成Diffie-Hellman密钥交换过程作为本地机制,以生成一对CEK(每个方向一个),这些CEK由Transaction标识ID.此版本规范支持的参数值为:
- "alg":ECDH-ES
- "apv":SDK参考编号
- "epk":QC,采用JSON Web Key(JWK)格式
- {"kty":"EC""crv":"P-256"}
- 所有其他参数:不存在
- CEK:"kty":oct - 256位
创建以下数据的JSON对象作为要签名的JWS有效内容:
{"MyPublicKey":"QT","SDKPublicKey":"QC"}
使用JWS Compact Serialization根据JWS(RFC 7515)生成完整JSON对象的数字签名.此版本规范支持的参数值为:
- "alg":PS256或ES256
- "x5c":X.5C v3:Cert(MyPb)以及可选的链接证书
根据我的理解,ECDH将生成一个密钥.在共享我的短暂公钥(QT)之后,SDK生成相同的密钥,因此我们以后可以交换使用相同密钥加密的JWE消息.
JSON {"MyPublicKey":"QT","SDKPublicKey":"QC"}将被签名并发送,但我不明白我将如何使用apv和epk,因为这些标题参数在JWE中使用而不是在第一个JWS将被共享.
在相同的规范中,他们谈论这些JWE消息,但他们没有这些apv和epk参数.
根据JWE(RFC 7516)使用SDK使用的相同"enc"算法加密JSON对象,获得的CEK由"kid"和JWE Compact Serialization识别.此版本规范支持的参数值为:
- "alg":dir
- "enc":A128CBC-HS256或A128GCM
- "kid":交易ID
- 所有其他参数:不存在
我还阅读了RFC 7518中的示例,其中我可以看到标题params apv和epk正在使用但是我不确定哪个标题参数,JWE或JWS?
关于如何使用nimbus-jose-jwt或任何其他java库实现这一点的任何想法都会非常有用.谢谢
iOS是否公开了用于密钥生成的API以及使用ECDH进行密钥派生?
从我看到的,苹果在内部使用它(特别是x25519),但我不认为它通过公共加密或其他方式暴露为公共API.
谢谢,
ž
我正在尝试使用 webcrypto 运行代码,但似乎无法导入 ECDH 公钥。我错过了什么?
我收到此错误:无法使用指定的密钥用法创建密钥。
浏览器:Google Chrome 版本 71.0.3578.98(官方版本)(64 位)
(在 Firefox 上工作正常)。
window.crypto.subtle
.generateKey(
{
name: 'ECDH',
namedCurve: 'P-256',
},
true,
['deriveKey', 'deriveBits']
)
.then(function(key) {
return window.crypto.subtle
.exportKey('raw', key.publicKey)
.then(function(ecdhPub) {
return window.crypto.subtle
.importKey(
'raw',
ecdhPub,
{
name: 'ECDH',
namedCurve: 'P-256',
},
false,
['deriveKey', 'deriveBits']
)
.then(function(ecdhPubKey) {
console.log('DONE !!', ecdhPubKey)
})
.catch(function(err) {
console.log('COULD NOT IMPORT...')
console.error(err)
})
})
.catch(function(err) {
console.log('COULD NOT EXPORT...')
console.error(err)
})
})
.catch(function(err) {
console.log('COULD NOT GENERATE KEYS...')
console.error(err) …Run Code Online (Sandbox Code Playgroud)有谁知道 React Native 是否有椭圆曲线 Diffie Hellman 加密(ECDH)的实现?
我为此找到了一些库。他们每个人都有一些问题:
Golang的椭圆曲线库可以在给定具有X和Y值(未压缩坐标)的公共坐标的情况下导出秘密密钥.
但是,当给定的点是具有给定y位的X9.62压缩形式的单个值时,如何解压缩它?
OpenSSL使用此方法处理此方案:
似乎还有一个类似的问题涉及数学,但不是Go的最佳实践,具体来说:
怎么在Go中完成?
我一直在尝试在 iOS 中为 secp224k1 曲线生成公钥和私钥。我们使用 ECDH 方法在移动端和后端之间进行 api 握手。在 Java 中,它是使用以下代码完成的。
public static KeyPair getECKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp224k1");
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH", "SC");
kpg.initialize(ecSpec);
return kpg.generateKeyPair();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在 swift 中生成具有特定曲线(secp224k1)类型的密钥?我尝试使用苹果提供的 EC 算法通过以下代码进行握手。
//Generates public and private key with EC algorithm
public static func getKey() -> [String: SecKey]? {
let attributes: [String: Any] =
[kSecAttrKeySizeInBits as String: 256,
kSecAttrKeyType as String: kSecAttrKeyTypeEC,
kSecPrivateKeyAttrs as String:
[kSecAttrIsPermanent as String: false]
]
var error: Unmanaged<CFError>?
guard …Run Code Online (Sandbox Code Playgroud) 在我的场景中,Alice 和 Bob 就使用哪条曲线达成了一致。
我的问题是 Alice 的公钥实际上是一个点,因此它具有 xy 格式。
我需要将 x,y 坐标字节转换为 ECPublicKey。
这是我正在使用的源代码
// outerPublicKey is the raw bytes from x,y coordinates in hex format
KeyFactory kf = KeyFactory.getInstance("EC");
PublicKey remoteAlicePub = kf.generatePublic(new X509EncodedKeySpec(outerPublicKey));
KeyPairGenerator bobKeyGen = KeyPairGenerator.getInstance("ECDH", "BC");
bobKeyGen.initialize(new ECGenParameterSpec(properties.getCurveName()), new SecureRandom());
KeyPair bobPair = bobKeyGen.generateKeyPair();
ECPublicKey bobPub = (ECPublicKey)bobPair.getPublic();
ECPrivateKey bobPvt = (ECPrivateKey)bobPair.getPrivate();
byte[] bobPubEncoded = bobPub.getEncoded();
byte[] bobPvtEncoded = bobPvt.getEncoded();
KeyAgreement bobKeyAgree = KeyAgreement.getInstance("ECDH");
bobKeyAgree.init(bobPvt);
bobKeyAgree.doPhase(remoteAlicePub, true); …Run Code Online (Sandbox Code Playgroud) 我使用 Xcode11 Beta 作为 IDE 和 Swift。
下面是我的代码,我的代码在Xcode10.3上可以很好地工作,但在Xcode11上却不能工作。当我调用函数 SecKeyCopyKeyExchangeResult() 时,堆栈总是返回错误“kSecKeyKeyExchangeParameterRequestedSize is Missing”,但在 Xcode10.3 上没问题。我检查了苹果开发者门户,没有任何线索。
`
函数主() {
testECDH()
}
private func testECDH(){
generateKeyPair()
let alicePublicKey = getPublicKey()
let alicePrivateKey = getPrivateKey()
print(alicePublicKey)
print(alicePrivateKey)
generateKeyPair()
let bobPublicKey = getPublicKey()
let bobPrivateKey = getPrivateKey()
print(bobPublicKey)
print(bobPrivateKey)
let alice_bob_ecdhsecret = ecdhSecretCalculation(publicKey: alicePublicKey, privateKey: bobPrivateKey)!
let bob_alice_ecdhsecret = ecdhSecretCalculation(publicKey: bobPublicKey, privateKey: alicePrivateKey)!
os_log("alice_bob_ecdhsecret = %@", alice_bob_ecdhsecret)
os_log("bob_alice_ecdhsecret = %@", bob_alice_ecdhsecret)
}
private func generateKeyPair(){
let attributes: [String: Any] = [kSecAttrKeySizeInBits as String: 256,
kSecAttrKeyType …Run Code Online (Sandbox Code Playgroud) 我有一个关于 ECDH(椭圆曲线 Diffie-Hellman)的基本问题。
整个想法是双方交换自己的公钥并获得相同的私钥。但是,您可以轻松拦截这两个键。输入另一个公钥是微不足道的。
所以主要问题是生成你自己的公钥。这是否意味着重新生成给定的公钥并非易事,即在您可以输入另一个公钥并获得相同的私钥之前,无法恢复用于生成给定公钥的原始参数?
我使用 golang 生成 P-521 公钥。源代码看起来像这样:
curve:=elliptic.P521()
priv, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
xBytes:=x.Bytes()
yBytes:=y.bytes()
//len(xBytes) some time is 65 bytes ,some time is 66 bytes
Run Code Online (Sandbox Code Playgroud)
为什么 P-521 公钥 X、Y 不像 P-256 或 P-384 那样具有固定公钥长度?
ecdh ×10
cryptography ×6
go ×2
ios ×2
java ×2
security ×2
swift ×2
bouncycastle ×1
javascript ×1
jwe ×1
public-key ×1
react-native ×1
secret-key ×1
xcode11 ×1