- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试在 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();
}
有没有办法在 swift 中生成具有特定曲线(secp224k1)类型的 key ?我尝试使用苹果提供的 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 let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
let err = error!.takeRetainedValue() as Error
print(err.localizedDescription)
return nil
}
guard let publicKey = SecKeyCopyPublicKey(privateKey) else {
print("Error occured while creating public key")
return nil
}
return ["publicKey": publicKey, "privateKey": privateKey]
}
当我发送通过上述方法生成的公钥时,我从服务器收到一条错误消息:
"error":"java.security.InvalidKeyException: ECDH key agreement requires ECPublicKey for doPhase","exception":"InvalidAuthException"
我尝试了 VirgilCrypto 来快速解决问题。但它在库中没有我需要的特定曲线类型。它仅支持 secp256r1。另外,我尝试过以下帖子的答案,但没有成功。
Elliptic Curve Diffie Hellman in ios/swift
任何建议或帮助都会很棒,谢谢。
最佳答案
iOS 不支持 Koblitz 224 位曲线。一种解决方案可能是使用不同的曲线类型或支持 secp224k1 的第三方库。
根据您的评论可以得出结论,secp224k1 曲线类型是必需的。
可能使用的第三方库是 Virgil Crypto,可通过 github https://github.com/VirgilSecurity/virgil-crypto 获取。 。它是一个 C++ 库。 (Virgil Security 还提供了一个名为 virgil-crypto-x 的 Swift 包装器库,但该库在当前版本中不再支持 secp224k1)。
通过创建具有已定义接口(interface)的 Objective-C++ 包装器,可以在 Swift 中间接使用 C++ 库。
构建 VSCCrypto.framework
在命令行输入:
git clone https://github.com/VirgilSecurity/virgil-crypto
cd virgil-crypto
utils/build.sh --target=ios
这构建了 iOS 的框架。
将 VSCCrypto.framework 添加到 Xcode 项目
在 Finder 中将 virgil-crypto/build/ios/lib 文件夹中的 VSCCrypto.framework 拖放到“Frameworks”组
在 XCode 中,点击右侧“嵌入式二进制文件”下的加号
Objective-C++ 包装器
#import "ECDHCrypto.h"
添加到桥接 header ECDHCrypto.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ECDHCrypto : NSObject
@property(nonatomic, strong) NSString *ownPrivateKey;
@property(nonatomic, strong) NSString *ownPublicKey;
- (void)generateKeyPair;
- (NSString *)shared:(NSString *)otherPublicKey;
@end
NS_ASSUME_NONNULL_END
ECDHCrypto.mm
#import "ECDHCrypto.h"
#import <VSCCrypto/VirgilCrypto.h>
using virgil::crypto::VirgilKeyPair;
using virgil::crypto::VirgilByteArray;
using virgil::crypto::VirgilCipherBase;
using virgil::crypto::str2bytes;
using virgil::crypto::bytes2str;
using virgil::crypto::bytes2hex;
@implementation ECDHCrypto
- (void)generateKeyPair {
VirgilKeyPair keyPair = VirgilKeyPair::generate(VirgilKeyPair::Type::EC_SECP224K1);
VirgilByteArray ownPublicKeyBates = keyPair.publicKey();
self.ownPublicKey = [NSString stringWithCString:bytes2str(ownPublicKeyBates).c_str()
encoding:[NSString defaultCStringEncoding]];
VirgilByteArray ownPrivateKeyBytes = keyPair.privateKey();
self.ownPrivateKey = [NSString stringWithCString:bytes2str(ownPrivateKeyBytes).c_str()
encoding:[NSString defaultCStringEncoding]];
}
- (NSString *)shared:(NSString *)otherPublicKey {
NSAssert(self.ownPrivateKey, @"private key must be set, e.g. use generateKeyPair");
std::string otherPKString([otherPublicKey cStringUsingEncoding:NSASCIIStringEncoding]);
VirgilByteArray pubKey = str2bytes(otherPKString);
std::string ownPrivateKeyString([self.ownPrivateKey cStringUsingEncoding:NSASCIIStringEncoding]);
VirgilByteArray ownPrivateKeyBytes = str2bytes(ownPrivateKeyString);
VirgilByteArray shared_ba = VirgilCipherBase::computeShared(pubKey, ownPrivateKeyBytes);
std::string hex = bytes2hex(shared_ba);
NSString *shared = [NSString stringWithCString:hex.c_str()
encoding:[NSString defaultCStringEncoding]];
return shared;
}
@end
Swift 中的用法
let otherPK = """
-----BEGIN PUBLIC KEY-----
ME4wEAYHKoZIzj0CAQYFK4EEACADOgAEgeW/foqxCDOd1y6lnXONkRThS6xhjLHP
SEXs7jHSpoaPQH4vArcGmIb1cAZcepEh7WDQxCyfQXg=
-----END PUBLIC KEY-----
"""
let ecdhCrypto = ECDHCrypto()
ecdhCrypto.generateKeyPair();
print("ecdhCrypto.ownPublicKey: \n" + ecdhCrypto.ownPublicKey);
print("shared secret: " + ecdhCrypto.shared(otherPK));
使用 Java 对应程序进行测试
要测试 key 交换是否成功,可以执行以下测试:
Java中会生成secp224k1 key 对,并将公钥输出到控制台。
使用复制/粘贴将公钥复制到 iOS 应用程序的 Swift 代码中。然后,应用程序生成 key 对并将其自己的公钥以及计算出的共享 key 写入控制台。然后,iOS 公钥将作为输入插入到 Java 程序中(以绿色显示)。
最后,我们可以比较 iOS 应用程序和 Java 程序的共享 key 。这里也是一样,所以 key 交换成功。
在上部区域中,您可以看到带有 iOS 源代码的 Xcode,在下部区域中可以看到 Java 程序的输出:
关于iOS - swift - 为 secp224k1 曲线(ECDH)生成 key 对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58138190/
我正在尝试创建 treasury yield curve 的图表比较两个不同日期的汇率。我很难将两条曲线组合起来并创建一个干净的图形。 我的问题:如何将两条 yield 曲线绘制在一起, yield
我在 R 平台中使用 randomForest 包进行分类任务。 rf_object<-randomForest(data_matrix, label_factor, cutoff=c(k,1-k))
我的设计师给我设计了这个设计,但我不知道如何最好地处理图像上方和下方的曲线。 我考虑过 clip-path 但不知道如何 flex 它。如果可以的话,我不想使用图像。 最佳答案 您可以使用 borde
我正在使用 Canvas 中的笔触和路径来制作两条线,我希望它们像波浪效果一样弯曲。而不是在 Photoshop 中创建实际图像来实现此目的。 谁能帮忙得到如下图所示的曲线? 我还想在末端实现圆 An
我正在尝试开发一种可以处理图像骨架的路径/曲线的代码。我想要一个来自两点之间骨架的点 vector 。 这段代码加了点就结束了,没找到解决办法。 #include "opencv2/highgui/
现在需要帮助。我可以用MKPolyline和MKPolylineView画线,但是如何在MKMapView上的两个坐标之间画弧线或曲线呢?非常感谢。 最佳答案 在回答问题之前,重要的是要提到 MKOv
我正在尝试应用 sklearn 的想法 ROC extension to multiclass到我的数据集。我的每类 ROC 曲线看起来都找到了一条直线,取消显示曲线波动的 sklearn 示例。 我
我有以下概念问题,我无法理解。 以下是调查数据示例,其中我有一个时间列,指示某人需要多长时间才能回答某个问题。 现在,我感兴趣的是清洁量将如何根据此阈值发生变化,即如果我增加阈值会发生什么,如果我降低
如何为使用视频的对象检测应用绘制每个窗口的误报率与未命中率(或误报概率)和 ROC(接收器操作曲线)的图表?如何确定误报和命中的数量?一个例子是很有用。 最佳答案 它很简单。将所有真正 (H0) 值存
我正在尝试绘制随机森林分类的 ROC 曲线。绘图有效,但我认为我绘制了错误的数据,因为生成的绘图只有一个点(准确性)。 这是我使用的代码: set.seed(55) data.controls <
我有如下两个模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <-
是否可以仅通过查看其 ROC 曲线来了解分类器是否过度拟合?我看到如果它的 AUC 太高(例如 98%)可能会过度拟合,但这也可能意味着分类器非常好。有没有办法区分这两种情况? 最佳答案 简短的回答:
我正在 JavaFX 中创建一个图形,它应该由有向边连接。最好是双三次曲线。有谁知道如何添加箭头? 箭头当然应该根据曲线的末端进行旋转。 这是一个没有箭头的简单示例: import javafx.ap
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我试图在曲线下方绘制阴影区域,但阴影区域位于曲线上方。谁能告诉我我的代码有什么问题? x=seq(0,30) y1=exp(-0.1*x) plot(x,y1,type="l",lwd=2,col="
我需要对我正在尝试的技术进行一些说明。我正在尝试将一个实体从 A 点移动到 B 点,但我不希望该实体沿直线移动。 例如,如果实体位于 x: 0, y:0 并且我想到达点 x:50, y: 0,我希望实
我有一个如下所示的模型: library(mlbench) data(Sonar) library(caret) set.seed(998) my_data <- Sonar fitControl <
有没有办法从pyspark中的Spark ML获取ROC曲线上的点?在文档中,我看到了一个 Scala 的例子,但不是 python:https://spark.apache.org/docs/2.1
我正在尝试使用Local Outlier Factor (LOF)算法,并想绘制 ROC 曲线。问题是,scikit-learn 提供的库不会为每个预测生成分数。 那么,有什么办法可以解决这个问题吗?
我目前正在使用 GDI+ 绘制折线图,并使用 Graphics.DrawCurve 来平滑线条。问题是曲线并不总是与我输入的点匹配,这使得曲线在某些点上超出了图形框架,如下所示(红色是 Graph
我是一名优秀的程序员,十分优秀!