gpt4 book ai didi

java - OpenSSL 签名未在 Java 中验证

转载 作者:太空宇宙 更新时间:2023-11-04 04:32:48 25 4
gpt4 key购买 nike

我有一个用 Objective C (iOS) 编写的移动应用程序,我需要为服务器生成签名以使用之前提供的公钥进行验证。

我正在努力获取签名以验证它们是由 OpenSSL 在我的移动应用程序上创建的。我有 Java 到 Java 的通信工作并创建有效的签名,我只是无法让 OpenSSL 创建有效的签名。有可能我的 Java 算法不正确,但在这个阶段我对此还很陌生。

这是我生成签名的 OpenSSL 代码:

RCT_EXPORT_METHOD(signDataWithKey:(NSString *)dataToSign
privateKey:(NSString *)privateKey
callback:(RCTResponseSenderBlock)callback)
{
int retEr;
char* text = (char*) [dataToSign UTF8String];
unsigned char *data;
unsigned long dataLen;

// converting nsstring base64 private key to openssl RSA key

BIO *mem = NULL;
RSA *rsa_private = NULL;
char *private_key = (char*)[privateKey UTF8String];

NSLog(@"Processing private key %s", private_key);

mem = BIO_new_mem_buf(private_key, strlen(private_key));
if (mem == NULL)
{
char buffer[120];
ERR_error_string(ERR_get_error(), buffer);
NSLog(@"Error loading private key %s", buffer);
}

rsa_private = PEM_read_bio_RSAPrivateKey(mem, NULL, NULL, NULL);
BIO_free (mem);
if (rsa_private == NULL)
{
char buffer[120];
ERR_error_string(ERR_get_error(), buffer);
NSLog(@"OpenSSL error: %s", buffer);
} else {
NSLog(@"Successfully loaded private key");
}
// end of convertion

data = (unsigned char *) text;
dataLen = strlen(text);


//// creating signature
// sha256
unsigned char hash[SHA256_DIGEST_LENGTH];
unsigned char sign[256];
unsigned int signLen;



SHA256(data, dataLen, hash);

unsigned char *shaString = sha256_hash_string(hash);

NSData* plainData = [NSData dataWithBytes:(const void *)shaString length:strlen(shaString)];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];

NSLog(@"Base64 checksum %@", base64String);

NSLog(@"SHA256 of %s is %send", text, shaString);


// signing
retEr = RSA_sign(NID_sha256WithRSAEncryption, shaString, strlen(shaString), sign, &signLen, rsa_private);
NSData* signatureData = [NSData dataWithBytes:(const void *)sign length:signLen];
NSString *base64Signature = [signatureData base64EncodedStringWithOptions:0];
NSLog(@"Got signed data %@", base64Signature);


// printf("Signature len gth = %d\n", signLen);
NSLog(@"RSA_sign: %@ signature length = %u", (retEr == 1) ? @"RSA_sign success": @"RSA_sign error", signLen);

NSLog(@"Got signed data %@", base64Signature);


RSA_free(rsa_private);
callback(@[base64Signature]);
}

unsigned char *sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH])
{
unsigned char *outputBuffer = calloc(65, sizeof(char));

int i = 0;

for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
}

outputBuffer[64] = 0;

return outputBuffer;

}

您会看到我实际上使用的是 SHA256 哈希的字符串表示形式——而不是二进制——这是故意的。然后我拿走那个字符串并签名。

下面是验证签名的Java代码:

public static boolean verifySignature(PublicKey publicKey, byte[] signedData, String signature) {
java.security.Security.addProvider(
new org.bouncycastle.jce.provider.BouncyCastleProvider()
);
Signature signatureCheck = Signature.getInstance("SHA256withRSA", "BC");
signatureCheck.initVerify(publicKey);
signatureCheck.update(signedData);

return signatureCheck.verify(CryptoUtil.Base64Decode(signature))
}

一切似乎都是正确的(我已经手动验证了私钥/公钥对),如果我使用 Java 签署相同的数据,它将验证(如下所示):

  public static String getSignatureForString(PrivateKey privateKey, String data) {
Signature signature = Signature.getInstance("SHA256withRSA", "BC");
signature.initSign(privateKey);

signature.update(data.getBytes());

byte[] signed = signature.sign();

return CryptoUtil.Base64Encode(signed);
}

我真的走到了死胡同,我已经尝试了所有各种更改 RSA_sign 方法以单独使用 NID_sha256 和其他排列,但在这个阶段我只是猜测。如果有人以前成功做过这种事情,我将不胜感激。

最佳答案

好的,所以我不完全知道为什么这与 RSA_sign 方法有任何不同,但在 PHP 中测试 openssl_verify() 和 openssl_sign() 并且这些函数为在 Java 中创建的签名生成正确的结果后,我决定阅读通过PHP源代码弄清楚他们是怎么做到的。无论如何,PHP 使用带有 EVP_ 前缀的函数(根据 OpenSSL 文档的高级加密函数)。不管怎样,我调整到 Objective C 代码来代替使用这些函数,现在我的签名在所有语言中都是匹配的。我希望这对某人有所帮助。

RCT_EXPORT_METHOD(signDataWithKey:(NSString *)dataToSign
privateKey:(NSString *)privateKey
callback:(RCTResponseSenderBlock)callback)
{
int retEr;
char* text = (char*) [dataToSign UTF8String];
unsigned char *data;
unsigned long dataLen;

// converting nsstring base64 private key to openssl RSA key

BIO *mem = NULL;
EVP_PKEY *pkey;
char *private_key = (char*)[privateKey UTF8String];

EVP_MD_CTX md_ctx;

NSLog(@"Processing private key %s", private_key);


mem = BIO_new_mem_buf(private_key, strlen(private_key));
if (mem == NULL)
{
char buffer[120];
ERR_error_string(ERR_get_error(), buffer);
NSLog(@"Error loading private key %s", buffer);
}

// CHANGED
pkey = PEM_read_bio_PrivateKey(mem, NULL, NULL, NULL);

BIO_free (mem);
if (pkey == NULL)
{
char buffer[120];
ERR_error_string(ERR_get_error(), buffer);
NSLog(@"OpenSSL error: %s", buffer);
} else {
NSLog(@"Successfully loaded private key");
}
// end of convertion

data = (unsigned char *) text;
dataLen = strlen(text);


//// creating signature
// sha256
unsigned char hash[SHA256_DIGEST_LENGTH];
unsigned char sign[4096];
int signLen;



SHA256(data, dataLen, hash);

unsigned char *shaString = sha256_hash_string(hash);

NSData* plainData = [NSData dataWithBytes:(const void *)shaString length:strlen(shaString)];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];

NSLog(@"Base64 checksum %@", base64String);

NSLog(@"SHA256 of %s is %s end", text, shaString);

// CHANGED - Using EVP to sign now.
EVP_SignInit(&md_ctx, EVP_sha256());
EVP_SignUpdate(&md_ctx, shaString, strlen(shaString));
retEr = EVP_SignFinal(&md_ctx, sign, &signLen, pkey);


// OLD METHOD
//retEr = RSA_sign(NID_sha256WithRSAEncryption, shaString, strlen(shaString), sign, &signLen, rsa_private);


NSData* signatureData = [NSData dataWithBytes:(const void *)sign length:signLen];
NSString *base64Signature = [signatureData base64EncodedStringWithOptions:0];
NSLog(@"Got signed data %@", base64Signature);


// printf("Signature len gth = %d\n", signLen);
NSLog(@"RSA_sign: %@ signature length = %u", (retEr == 1) ? @"RSA_sign success": @"RSA_sign error", signLen);

NSLog(@"Got signed data %@", base64Signature);


EVP_PKEY_free(pkey);
callback(@[base64Signature]);
}

unsigned char *sha256_hash_string (unsigned char hash[SHA256_DIGEST_LENGTH])
{
unsigned char *outputBuffer = calloc(65, sizeof(char));

int i = 0;

for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
}

outputBuffer[64] = 0;

return outputBuffer;

}

关于java - OpenSSL 签名未在 Java 中验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33998359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com