我正在尝试在 iOS 应用程序和 java servlet 之间实现 AES 加密。Java servlet 使用 BouncyCastle 库,而 iOS 应用程序使用 OpenSSL。虽然我在双方都使用了相同的公钥/私钥对和域参数,但 OpenSSL 生成的共享密钥有时与 BouncyCastle 在服务器端生成的不同。
程序如下;
- 在服务器中生成的具有指定域参数的公钥/私钥对(例如
server_public_key
,server_private_key
) server_public_key
EC_POINT
以X 和 Y的形式嵌入 iOS 应用程序中- 在运行时,iOS 应用程序会生成自己的公钥/私钥对(比如
client_key_curve
哪个是EC_KEY
),并且 - 然后根据 server_public_key 和 client_key_curve加载
server_public_key
并计算共享密钥 ( ),并且key_agreement
- 然后
client_public_key
(从 中提取client_key_curve
)以及使用派生的共享秘密(key_agreement
)对称加密的加密消息发送到服务器 client_public_key
在服务器端,再次使用与客户端相同的服务器 ECDH 参数计算共享密钥 ,并且- 然后使用计算的加密消息解密
key_agreement
但是解密的消息并不总是与客户端发送的消息相同。
由于我还开发了一个使用相同程序但使用 BouncyCastle 进行加密的 Android 应用程序,因此我怀疑使用 OpenSSL 实现代码的正确性,因此此处显示代码以供其他人帮助解决问题。我为计算共享秘密而实施的内容如下
- (void)calculateSharedSecret
{
BN_CTX* bn_ctx;
EC_KEY* client_key_curve = NULL;
EC_KEY* server_key_curve = NULL;
EC_GROUP* client_key_group = NULL;
EC_GROUP* server_key_group = NULL;
EC_POINT* client_publicKey = NULL;
EC_POINT* server_publicKey = NULL;
BIGNUM* client_privatKey = NULL;
BIGNUM* client_publicK_x = NULL;
BIGNUM* client_publicK_y = NULL;
BIGNUM* server_publicK_x = NULL;
BIGNUM* server_publicK_y = NULL;
NSException *p = [NSException exceptionWithName:@"" reason:@"" userInfo:nil];
bn_ctx = BN_CTX_new();
BN_CTX_start(bn_ctx);
client_publicK_x = BN_CTX_get(bn_ctx);
client_publicK_y = BN_CTX_get(bn_ctx);
client_privatKey = BN_CTX_get(bn_ctx);
server_publicK_x = BN_CTX_get(bn_ctx);
server_publicK_y = BN_CTX_get(bn_ctx);
// client
if ((client_key_curve = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)) == NULL)
@throw p;
if ((client_key_group = (EC_GROUP *)EC_KEY_get0_group(client_key_curve)) == NULL)
@throw p;
if (EC_KEY_generate_key(client_key_curve) != 1)
@throw p;
if ((client_publicKey = (EC_POINT *)EC_KEY_get0_public_key(client_key_curve)) == NULL)
@throw p;
if (EC_KEY_check_key(client_key_curve) != 1)
@throw p;
client_privatKey = (BIGNUM *)EC_KEY_get0_private_key(client_key_curve);
char *client_public_key = EC_POINT_point2hex(client_key_group, client_publicKey, POINT_CONVERSION_COMPRESSED, bn_ctx);
char *client_privat_key = BN_bn2hex(client_privatKey);
_clientPublicKey = [NSString stringWithCString:client_public_key encoding:NSUTF8StringEncoding];
// server
NSArray* lines = [self loadServerPublicKeyXY];
NSString *public_str_x = [lines objectAtIndex:0];
NSString *public_str_y = [lines objectAtIndex:1];
BN_dec2bn(&server_publicK_x, [public_str_x UTF8String]);
BN_dec2bn(&server_publicK_y, [public_str_y UTF8String]);
if ((server_key_curve = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1)) == NULL)
@throw p;
if ((server_key_group = (EC_GROUP *)EC_KEY_get0_group(server_key_curve)) == NULL)
@throw p;
if (EC_KEY_generate_key(server_key_curve) != 1)
@throw p;
if ((server_publicKey = EC_POINT_new(server_key_group)) == NULL)
@throw p;
if (EC_POINT_set_affine_coordinates_GFp(server_key_group, server_publicKey, server_publicK_x, server_publicK_y, bn_ctx) != 1)
@throw p;
if (EC_KEY_check_key(server_key_curve) != 1)
@throw p;
unsigned char *key_agreement = NULL;
key_agreement = (unsigned char *)OPENSSL_malloc(SHA_DIGEST_LENGTH);
if (ECDH_compute_key(key_agreement, SHA_DIGEST_LENGTH, server_publicKey, client_key_curve, KDF1_SHA1) == 0)
@throw p;
_symmetricKey = [NSData dataWithBytes:key_agreement length:16];
}
和
void *KDF1_SHA1(const void *input, size_t inlen, void *output, size_t *outlen)
{
if (*outlen < SHA_DIGEST_LENGTH)
return NULL;
else
*outlen = SHA_DIGEST_LENGTH;
return SHA1(input, inlen, output);
}
_clientPublicKey
并_symmetricKey
在类级别声明
两侧使用相同的曲线(名为 prime256v1 或 secp256r1),但结果并不总是相同。
编辑 1:作为对@PeterDettman 的回应,我发布了服务器端代码以进行更多说明
public byte[] generateAESSymmetricKey(byte[] client_public_key_hex) throws InvalidRequest{
try {
// ECDH Private Key as well as other prime256v1 params was generated by Java "keytool" and stored in a JKS file
KeyStore keyStore = ...;
PrivateKey privateKey = (PrivateKey) keyStore.getKey("keyAlias", "keyStorePassword".toCharArray());
ECPrivateKeyParameters ecdhPrivateKeyParameters = (ECPrivateKeyParameters) (PrivateKeyFactory.createKey(privateKey.getEncoded()));
ECCurve ecCurve = ecdhPrivateKeyParameters.getParameters().getCurve();
ECDomainParameters ecDomainParameters = ecdhPrivateKeyParameters.getParameters();
ECPublicKeyParameters client_public_key = new ECPublicKeyParameters(ecCurve.decodePoint(client_public_key_hex), ecDomainParameters);
BasicAgreement agree = new ECDHBasicAgreement();
agree.init(ecdhPrivateKeyParameters);
byte[] keyAgreement = agree.calculateAgreement(client_public_key).toByteArray();
SHA1Digest sha1Digest = new SHA1Digest();
sha1Digest.update(keyAgreement, 0, keyAgreement.length);
byte hashKeyAgreement[] = new byte[sha1Digest.getDigestSize()];
sha1Digest.doFinal(hashKeyAgreement, 0);
byte[] server_calculatd_symmetric_key = new byte[16];
System.arraycopy(hashKeyAgreement, 0, server_calculatd_symmetric_key, 0, server_calculatd_symmetric_key.length);
return server_calculatd_symmetric_key;
} catch (Throwable ignored) {
return null;
}
}
在哪里client_public_key_hex
将client_public_key
其转换为字节数组。预期的结果是始终server_calculatd_symmetric_key
等于symmetricKey
。但它们并不总是相同的。
编辑 2:作为对@PeterDettman 回答的反馈,我做了一些更改以反映他的建议,尽管不平等率降低了,但在所有情况下,双方生成的密钥协议(共享秘密)并不相同。
可以使用以下数据重现其中一种不等式情况
- 公钥: 02E05C058C3DF6E8D63791660D9C5EA98B5A0822AB93339B0B8815322131119C4C
- 私钥: 062E8AC930BD6009CF929E51B37432498075D21C335BD00086BF68CE09933ACA
- OpenSSL 生成的共享密钥:51d027264f8540e5d0fde70000000000
- BouncyCastle 生成的共享密钥: 51d027264f8540e5d0fde700e5db0fab
那么在实现的代码或过程中是否有任何错误?
谢谢