AES加密

来源:互联网 发布:域名解析隐藏端口号 编辑:程序博客网 时间:2024/06/15 07:28

   AES算法(AES/ECB/PKCS5Padding)128位的,找了半天才找到匹配的 


RSA算法(RSA/ECB/PKCS1Padding)

    YF_Pay_Encrypt *tool = [[YF_Pay_Encryptalloc] init];

    

    tempData=[AESCipherencryptData:datakey:[key dataUsingEncoding:NSUTF8StringEncoding]];

    tempData=[tool.base64encode:tempData];

    

    tempstr = [[NSStringalloc] initWithData:tempDataencoding:NSUTF8StringEncoding];

    

    

    [sendDic setObject: tempstrforKey:@"data"];

    

#import <Foundation/Foundation.h>

#import "GTMBase64.h"

#define FBENCRYPT_ALGORITHM     kCCAlgorithmAES128

#define FBENCRYPT_BLOCK_SIZE    kCCBlockSizeAES128

#define FBENCRYPT_KEY_SIZE      kCCKeySizeAES128

@interface AESCipher : NSObject




+ (NSData*)encryptData:(NSData*)data key:(NSData*)key;

+ (NSData*)decryptData:(NSData*)data key:(NSData*)key;


@end






#import "AESCipher.h"

#import <CommonCrypto/CommonCryptor.h>


NSString *const kInitVector =@"16-Bytes--String";


size_t const kKeySize =kCCKeySizeAES128;


@implementation AESCipher





+ (NSData*)encryptData:(NSData*)data key:(NSData*)key;

{

    NSData* result =nil;

    

    // setup key

    unsignedchar cKey[FBENCRYPT_KEY_SIZE];

    bzero(cKey,sizeof(cKey));

    [key getBytes:cKeylength:FBENCRYPT_KEY_SIZE];

    

    // setup output buffer

    size_t bufferSize = [datalength] + FBENCRYPT_BLOCK_SIZE;

    void *buffer =malloc(bufferSize);

    

    // do encrypt

    size_t encryptedSize =0;

    CCCryptorStatus cryptStatus =CCCrypt(kCCEncrypt,

                                          FBENCRYPT_ALGORITHM,

                                          kCCOptionECBMode|kCCOptionPKCS7Padding,

                                          cKey,

                                          FBENCRYPT_KEY_SIZE,

                                          nil,

                                          [data bytes],

                                          [data length],

                                          buffer,

                                          bufferSize,

                                          &encryptedSize);

    if (cryptStatus ==kCCSuccess) {

        result = [NSDatadataWithBytesNoCopy:bufferlength:encryptedSize];

    } else {

        free(buffer);

        NSLog(@"[ERROR] failed to encrypt|CCCryptoStatus: %d", cryptStatus);

    }

    

    return result;

}


+ (NSData*)decryptData:(NSData*)data key:(NSData*)key;

{

    NSData* result =nil;

    

    // setup key

    unsignedchar cKey[FBENCRYPT_KEY_SIZE];

    bzero(cKey,sizeof(cKey));

    [key getBytes:cKeylength:FBENCRYPT_KEY_SIZE];

    

    // setup output buffer

    size_t bufferSize = [datalength] + FBENCRYPT_BLOCK_SIZE;

    void *buffer =malloc(bufferSize);

    

    // do decrypt

    size_t decryptedSize =0;

    CCCryptorStatus cryptStatus =CCCrypt(kCCDecrypt,

                                          FBENCRYPT_ALGORITHM,

                                          kCCOptionECBMode|kCCOptionPKCS7Padding,

                                          cKey,

                                          FBENCRYPT_KEY_SIZE,

                                          nil,

                                          [data bytes],

                                          [data length],

                                          buffer,

                                          bufferSize,

                                          &decryptedSize);

    

    if (cryptStatus ==kCCSuccess) {

        result = [NSDatadataWithBytesNoCopy:bufferlength:decryptedSize];

    } else {

        free(buffer);

        NSLog(@"[ERROR] failed to decrypt| CCCryptoStatus: %d", cryptStatus);

    }

    

    return result;  

}



@end



1 0
原创粉丝点击