//
//  CryptoAES.m
//  AIIM
//
//  Created by gan on 2025/3/29.
//

#import <Foundation/Foundation.h>
#import "CryptoAES.h"
#import <Security/Security.h>
#import "RSA.h"
#import "GRSA.h"


static NSString *publicKey2 = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDR8l+B4jG6mEyMW8LYoNwMAUGnbZzDoKBSB1N4o19NscpyOA5m5Yyy2PDeja+iRNOwC7KTmKynd0Qzt8kwVnZHxA+oeKpNpepMS/+felan/QDgUD3u0Tix/sxuZ7GVB2h4X39wJTfgItNC7IK1qNQSZMkBfPhSQKQktkIlPU78BwIDAQAB";
static NSString *privateKey2 = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANHyX4HiMbqYTIxbwtig3AwBQadtnMOgoFIHU3ijX02xynI4DmbljLLY8N6Nr6JE07ALspOYrKd3RDO3yTBWdkfED6h4qk2l6kxL/596Vqf9AOBQPe7ROLH+zG5nsZUHaHhff3AlN+Ai00LsgrWo1BJkyQF8+FJApCS2QiU9TvwHAgMBAAECgYBhUmWLo/tBcFAr0G2mVgsrVXnFzz4YQWGQJO1AwxruvTgHcozajhaG4aHmyO9d3WHz/bguS1vd1Z+8CbotuibhjoiA464AL2sM9S490oYwLm39EFhSgG8JxovxrfpZ+fEpVjUjna40OSBvvtKtfhoBEjIRArd2Okkcx1f8f+MrgQJBAPL+5Rmtc9sLHDzhaqgP1Z0ntESLRN+JhqspFOaEibMdeBdlBbBfQ1HFdV8CsWLjzn/IM3JQnK4LnZ8hXwbWM/ECQQDdLrMFm5NESeYjlaLvBzHtQxTv6YOLz4I9++2YITa0+FvvRBhkLgrOYrO0yALIlrbdhxBnJDZzfYlpiJ/9pUd3AkEA4h4xoLBYpBd1om1864E4nXJJ/JLAd2cpavLioRPqc0PT8akvHw22vkwJyWzfFo+4JA5vWUwMUKU8esz5+YmwcQJADKftana6eH/aAVNAnXjF7JOYERpFgqx/AVj3AWt1G+nSKOC2nJTd3W1cZw3CywOEtyxRTfWOgBFA2Z8NuI/KhQJAIHAiOZH5P4s6WdNu3Gsg8HLcCtc+2Z8aeaBIDi+/L5pMHjiBVTtSEuPTvuhhan4ifGTV3uoJRRPO6re4F7mgZA==";

static NSString *RSApublicKey2 = @"-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDR8l+B4jG6mEyMW8LYoNwMAUGnbZzDoKBSB1N4o19NscpyOA5m5Yyy2PDeja+iRNOwC7KTmKynd0Qzt8kwVnZHxA+oeKpNpepMS/+felan/QDgUD3u0Tix/sxuZ7GVB2h4X39wJTfgItNC7IK1qNQSZMkBfPhSQKQktkIlPU78BwIDAQAB\n-----END PUBLIC KEY-----";
static NSString *RSAprivateKey2 = @"-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANHyX4HiMbqYTIxbwtig3AwBQadtnMOgoFIHU3ijX02xynI4DmbljLLY8N6Nr6JE07ALspOYrKd3RDO3yTBWdkfED6h4qk2l6kxL/596Vqf9AOBQPe7ROLH+zG5nsZUHaHhff3AlN+Ai00LsgrWo1BJkyQF8+FJApCS2QiU9TvwHAgMBAAECgYBhUmWLo/tBcFAr0G2mVgsrVXnFzz4YQWGQJO1AwxruvTgHcozajhaG4aHmyO9d3WHz/bguS1vd1Z+8CbotuibhjoiA464AL2sM9S490oYwLm39EFhSgG8JxovxrfpZ+fEpVjUjna40OSBvvtKtfhoBEjIRArd2Okkcx1f8f+MrgQJBAPL+5Rmtc9sLHDzhaqgP1Z0ntESLRN+JhqspFOaEibMdeBdlBbBfQ1HFdV8CsWLjzn/IM3JQnK4LnZ8hXwbWM/ECQQDdLrMFm5NESeYjlaLvBzHtQxTv6YOLz4I9++2YITa0+FvvRBhkLgrOYrO0yALIlrbdhxBnJDZzfYlpiJ/9pUd3AkEA4h4xoLBYpBd1om1864E4nXJJ/JLAd2cpavLioRPqc0PT8akvHw22vkwJyWzfFo+4JA5vWUwMUKU8esz5+YmwcQJADKftana6eH/aAVNAnXjF7JOYERpFgqx/AVj3AWt1G+nSKOC2nJTd3W1cZw3CywOEtyxRTfWOgBFA2Z8NuI/KhQJAIHAiOZH5P4s6WdNu3Gsg8HLcCtc+2Z8aeaBIDi+/L5pMHjiBVTtSEuPTvuhhan4ifGTV3uoJRRPO6re4F7mgZA==\n-----END PRIVATE KEY-----";

@implementation CryptoAES

+ (CryptoAES *_Nonnull)shareInstance{
    static id gShareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
      gShareInstance = [[self alloc] init];
    });
    return gShareInstance;
}

- (instancetype)init {
    if (self = [super init]) {
    }
    return self;
}

//RSA---------------------------------------------


#pragma  mark 加密

-(NSString *_Nullable)encryptStringL:(NSString *_Nullable)str{
//    NSString *encWithPubKey = [RSA encryptString:str publicKey:RSApublicKey2];
    NSString *encWithPubKey = [GRSA encryptString:str publicKey:RSApublicKey2];
    return encWithPubKey;
}

#pragma  mark 解密
-(NSString *_Nullable)decryptDataL:(NSString *_Nullable)str{
//    NSLog(@"decryptDataL str:%lu",(unsigned long)str.length);
    if([str isKindOfClass:[NSString class]]&&str.length>30){
        NSString *decWithPrivKey = [GRSA decryptString:str privateKey:RSAprivateKey2];
        if(decWithPrivKey&&decWithPrivKey.length>0){
            return decWithPrivKey;
        }
        else{
            return str;
        }
    }
    else{
        if([str isKindOfClass:[NSString class]]){
            return str;
        }
        return @"";
    }
}

@end
