- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试使用 Diffie Hellman key 交换和椭圆曲线加密快速加密和解密字符串。
以下是我遵循的代码。
SWIFT 代码:
let attributes: [String: Any] = [kSecAttrKeySizeInBits as String: 256,
kSecAttrKeyType as String: kSecAttrKeyTypeEC,
kSecPrivateKeyAttrs as String: [kSecAttrIsPermanent as String: false]]
var error: Unmanaged<CFError>?
if #available(iOS 10.0, *) {
**// Step 1: Generating the Public & Private Key**
guard let privateKey1 = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {return false}
let publicKey1 = SecKeyCopyPublicKey(privateKey1)
guard let privateKey2 = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {return false}
let publicKey2 = SecKeyCopyPublicKey(privateKey2)
let dict: [String: Any] = [:]
**// Step 2: Generating Shared Key**
guard let shared1 = SecKeyCopyKeyExchangeResult(privateKey1, SecKeyAlgorithm.ecdhKeyExchangeStandardX963SHA256, publicKey2!, dict as CFDictionary, &error) else {return false}
**// Step 3: Encrypt string using Sharedkey**
let options: [String: Any] = [kSecAttrKeyType as String: kSecAttrKeyTypeEC,
kSecAttrKeyClass as String: kSecAttrKeyClassPrivate,
kSecAttrKeySizeInBits as String : 256]
// Stuck from this line on
guard let key = SecKeyCreateWithData(shared1 ,
options as CFDictionary,
&error) else {return false}
print(key)
let str = "Hello"
let byteStr: [UInt8] = Array(str.utf8)
let cfData = CFDataCreate(nil, byteStr, byteStr.count)
guard let encrypted = SecKeyCreateEncryptedData(publicKey1!,
SecKeyAlgorithm.ecdsaSignatureDigestX962SHA256,
cfData!,
&error) else {return false}
print(encrypted)
} else {
print("unsupported")
}
Java代码:
public static Map<String, Object> ecEncrypt(String deviceData, String serverPubKey, String dsTranID)
throws DataEncryptionException {
provider = new BouncyCastleProvider();
HashMap<String, Object> result = null;
JWEObject jweObject = null;
JWK jwk = null;
SecretKey Z = null;
JWEHeader header = null;
ECPublicKey ecpubkey = null;
byte[] byte_pubkey = null;
try {
result = new HashMap<String, Object>();
/*
* Generate Ephemeral keypair for SDk which constitute Public and
* Private key of SDK
*/
STEP 1:
sdkKeyPair = Crypto.generateEphemeralKeyPair();
/*
* Compute Secrete Key Z from SDKs Private Key(pSDK),DS Public
* key(serverPubKey) and DS ID
*/
//converting string to Bytes
STEP 2:
byte_pubkey = Base64.decode(serverPubKey, android.util.Base64.DEFAULT);
//converting it back to EC public key
STEP 3:
KeyFactory factory = KeyFactory.getInstance("ECDSA", provider);
ecpubkey = (ECPublicKey) factory.generatePublic(new X509EncodedKeySpec(byte_pubkey));
System.out.println("FINAL OUTPUT" + ecpubkey);
STEP 4:
Z = Crypto.generateECDHSecret(ecpubkey,
(ECPrivateKey) sdkKeyPair.getPrivate(), dsTranID,
"A128CBC_HS256");
System.out.println("****Secrete key Z for SDK Computed succesfully *****");
/*
* Build JWK to construct header
*/
STEP 5:
jwk = new ECKey.Builder(Curve.P_256,
(ECPublicKey) sdkKeyPair.getPublic()).build();
STEP 6:
header = new JWEHeader.Builder(JWEAlgorithm.ECDH_ES,
EncryptionMethod.A128CBC_HS256).ephemeralPublicKey(
ECKey.parse(jwk.toJSONString())).build();
System.out.println("****Header for SDK Computed succesfully*****");
/*
* Add Header and payload before encrypting payload using secret key
* Z
*/
STEP 7:
jweObject = new JWEObject(header, new Payload(deviceData));
jweObject.encrypt(new DirectEncrypter(Z));
/*
* serialize JWEobject which contains
* [header-base64url].[encryptedKey
* -base64url].[iv-base64url].[cipherText
* -base64url].[authTag-base64url]
*/
System.out
.println("****Payload of SDK encrypted succesfully *****");
return result;
} catch (Exception e) {
e.printStackTrace();
throw new DataEncryptionException();
} finally {
sdkKeyPair = null;
jweObject = null;
jwk = null;
Z = null;
header = null;
}
}
我还包含了 Java 代码。我必须在 Swift 中做同样的事情。如何使用共享 key (Shared1)加密字符串进行EC加密?我需要执行第 3 步。有人可以帮忙吗?
最佳答案
首先,您要尝试实现 ECIES。如果您想查找有关该计划的信息,知道实际名称很重要。
因此,假设 key 对 1 来自密文的发送方,而 key 对 2 来自密文的接收方。在这种情况下, key 对 1 应该是临时的(当场创建,绑定(bind)到一个加密的消息)并且 key 对 2 是静态的(预先创建并保存)。此外,公钥 2 被信任来自接收方。从您的简化代码中,这一切都不清楚,并且在您的代码中,您仍然可以切换发送方和接收方。
因此,使用接收方的公钥 (2),发送方可以使用他们的私钥创建一个共享 key ,在您的代码中称为 shared1
。您现在可以使用 shared1
对数据执行对称加密。然后您只需将发送方的临时公钥和密文发送给接收方。接收方使用发送方的公钥 (1) 和他们的静态私钥 (2) 创建 shared2
。这与 shared1
相同,因此可以用作解密数据的 key 。
就是这样,除了要注意,由于发送方的私钥 (1) 与数据相关联,因此一旦计算出 shared1
就不再需要它,并且可能会被丢弃甚至在消息被加密之前。
如果您阅读了上面的内容,您就会发现将所有这些都放在一个方法中并不是一个好主意:
现在进行加密和发送:
对于接收:
就是这样。您可能希望在您的代码中显式这些步骤。
关于ios - 使用 Diffie Hellman key 交换和椭圆曲线加密快速加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58076132/
一些快速背景。我使用 rclone 将数据传输到 SFTP 服务器。 rclone 是用 Golang 编写的,并在底层使用 lib crypto。当我们尝试与 SFTP 服务器建立 ssh 连接时,
我将 FIPS 186-3 推荐的曲线用于 Diffie-Hellman 椭圆曲线。我正在尝试确定私钥的最大长度,根据 RFC 5915 应该是: 天花板 (log2(n)/8) ,其中 n 是曲线的
我正在做一个项目,Alice 和 Bob 使用 Diffie-Hellman key 交换相互发送消息。让我陷入困境的是如何将他们正在使用的证书合并到其中,以便我可以获得他们的 secret 消息。
也许在这里问这个问题不太合适......无论如何,我正在尝试使用 gmp 库来实现 DH,但我遇到的问题是: 有一次,当我进行测试以观察输出时,尽管选择了较大的素数和私钥值: p 的十进制长度大约超过
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
我在 JavaCard 上遇到 DiffieHellman 问题。我有这门课: https://pastebin.com/2F2sQ2Pe (https://github.com/ASKGLab/DH
所以我正在尝试进行客户端-服务器 diffie hellman 加密。我使用这个:serverPublicKey = (int)Math.pow(generator,serverPrivateKey)
上下文 我在看这个视频DHE explained 它讲的是两个人如何在没有吸管的情况下交换 key 以了解更多信息。 根据视频实现 // INITIALIZERS (video's values)--
我有一个像这样的 Diffie–Hellman 安全类: public class AESSecurityCap { private PublicKey publicKey; KeyA
嗨,我正在尝试实现 Diffie–Hellman key 交换 public static Integer secret = 100000; public static BigInteger g =
我遇到了一个挑战,它与测试 friend 的加密过程有关。 这是一个Diffie-Hellman交换过程,这里是已知的变量/常量: P, G 我生成的私钥(可变) 我生成的公钥(变量) 收件人公钥(常
我有下面这行代码来生成私钥: int Xa = randomNo.nextInt(10000); int Ya = (int) Math.pow(G, Xa) % P; G 和 P 是静态数字。而 X
我正在编写 Pohlig-Hellman 算法,但我在根据算法定义理解算法中的步骤时遇到问题。 通过 algorithm 的 Wiki : 我知道第一部分 1) 是计算 p-1 的素数 - 这很好。
我正在尝试设计一种安全方案,用于加密 silverlight 客户端和我创建的 php web 服务之间的应用程序级数据。由于我正在处理一个公共(public)网站,因此我从服务中提取的信息是公开的,
所以我在阅读 this并对莱姆斯的回答感到惊讶。因为我认为情况正好相反。 所以,至于我的问题。为什么使用 diffie-hellman key 交换来确定共享 key 来加密/解密消息的程序(通常)不
是否有实现 diffie helman 的 Python 标准库? 我已经查看了 python pyopenssl 中的 openssl 包装器,但它没有涵盖它。 我也无法在 NaCl 或 crypt
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我正在尝试实现 Diffie-Hellman key 交换。我对生成后如何使用 key 有点困惑。 如输出所示,2 个 key 是在 key 交换中使用相同的质数和基数生成的,并在生成 key 时交换
我需要进行单元测试来检查 Diffie-Hellman key 协议(protocol)协议(protocol)的实现是否正常,所以我需要测试向量来验证。问题是我在互联网上的任何地方都找不到它们。 R
Elliptic 曲线 diffie hellman 计算看起来与此处定义的标准曲线有什么不同: /* * The basic Diffie-Hell
我是一名优秀的程序员,十分优秀!