|
@@ -584,116 +584,21 @@
|
|
#pragma mark - Encrypt/Decrypt asymmetric
|
|
#pragma mark - Encrypt/Decrypt asymmetric
|
|
#
|
|
#
|
|
|
|
|
|
-- (NSData *)encryptAsymmetricString:(NSString *)plain publicKey:(NSString *)publicKey privateKey:(NSString *)privateKey
|
|
|
|
|
|
+- (NSData *)encryptAsymmetricData:(NSData *)plainData privateKey:(NSString *)privateKey
|
|
{
|
|
{
|
|
EVP_PKEY *key = NULL;
|
|
EVP_PKEY *key = NULL;
|
|
int status = 0;
|
|
int status = 0;
|
|
-
|
|
|
|
- if (publicKey != nil) {
|
|
|
|
-
|
|
|
|
- unsigned char *pKey = (unsigned char *)[publicKey UTF8String];
|
|
|
|
|
|
|
|
- // Extract real publicKey
|
|
|
|
- BIO *bio = BIO_new_mem_buf(pKey, -1);
|
|
|
|
- if (!bio)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- X509 *x509 = PEM_read_bio_X509(bio, NULL, 0, NULL);
|
|
|
|
- if (!x509)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- key = X509_get_pubkey(x509);
|
|
|
|
- if (!key)
|
|
|
|
- return nil;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (privateKey != nil) {
|
|
|
|
-
|
|
|
|
- unsigned char *pKey = (unsigned char *)[privateKey UTF8String];
|
|
|
|
|
|
+ unsigned char *pKey = (unsigned char *)[privateKey UTF8String];
|
|
|
|
|
|
- BIO *bio = BIO_new_mem_buf(pKey, -1);
|
|
|
|
- if (!bio)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
|
|
|
|
- if (!key)
|
|
|
|
- return nil;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
|
|
|
|
- if (!ctx)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- status = EVP_PKEY_encrypt_init(ctx);
|
|
|
|
- if (status <= 0)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- status = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING);
|
|
|
|
- if (status <= 0)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- status = EVP_PKEY_CTX_set_rsa_oaep_md(ctx, EVP_sha256());
|
|
|
|
- if (status <= 0)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- status = EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, EVP_sha256());
|
|
|
|
- if (status <= 0)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- unsigned long outLen = 0;
|
|
|
|
- NSData *plainData = [plain dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
- status = EVP_PKEY_encrypt(ctx, NULL, &outLen, [plainData bytes], (int)[plainData length]);
|
|
|
|
- if (status <= 0 || outLen == 0)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- unsigned char *out = (unsigned char *) malloc(outLen);
|
|
|
|
- status = EVP_PKEY_encrypt(ctx, out, &outLen, [plainData bytes], (int)[plainData length]);
|
|
|
|
- if (status <= 0)
|
|
|
|
|
|
+ BIO *bio = BIO_new_mem_buf(pKey, -1);
|
|
|
|
+ if (!bio)
|
|
return nil;
|
|
return nil;
|
|
-
|
|
|
|
- NSData *outData = [[NSData alloc] initWithBytes:out length:outLen];
|
|
|
|
-
|
|
|
|
- if (out)
|
|
|
|
- free(out);
|
|
|
|
-
|
|
|
|
- return outData;
|
|
|
|
-}
|
|
|
|
|
|
|
|
-- (NSData *)encryptAsymmetricData:(NSData *)plainData publicKey:(NSString *)publicKey privateKey:(NSString *)privateKey
|
|
|
|
-{
|
|
|
|
- EVP_PKEY *key = NULL;
|
|
|
|
- int status = 0;
|
|
|
|
-
|
|
|
|
- if (publicKey != nil) {
|
|
|
|
-
|
|
|
|
- unsigned char *pKey = (unsigned char *)[publicKey UTF8String];
|
|
|
|
-
|
|
|
|
- // Extract real publicKey
|
|
|
|
- BIO *bio = BIO_new_mem_buf(pKey, -1);
|
|
|
|
- if (!bio)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- X509 *x509 = PEM_read_bio_X509(bio, NULL, 0, NULL);
|
|
|
|
- if (!x509)
|
|
|
|
- return nil;
|
|
|
|
-
|
|
|
|
- key = X509_get_pubkey(x509);
|
|
|
|
- if (!key)
|
|
|
|
- return nil;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (privateKey != nil) {
|
|
|
|
-
|
|
|
|
- unsigned char *pKey = (unsigned char *)[privateKey UTF8String];
|
|
|
|
-
|
|
|
|
- BIO *bio = BIO_new_mem_buf(pKey, -1);
|
|
|
|
- if (!bio)
|
|
|
|
- return nil;
|
|
|
|
|
|
+ key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
|
|
|
|
+ if (!key)
|
|
|
|
+ return nil;
|
|
|
|
|
|
- key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
|
|
|
|
- if (!key)
|
|
|
|
- return nil;
|
|
|
|
- }
|
|
|
|
|
|
|
|
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
|
|
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
|
|
if (!ctx)
|
|
if (!ctx)
|