AES加密

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


RSA算法(RSA/ECB/PKCS1Padding)

    YF_Pay_Encrypt *tool = [[YF_Pay_Encrypt alloc] init];

    

    tempData=[AESCipher encryptData:data key:[key dataUsingEncoding:NSUTF8StringEncoding]];

    tempData=[tool.base64 encode:tempData];

    

    tempstr = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];

    

    

    [sendDic setObject: tempstr forKey:@"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

    unsigned char cKey[FBENCRYPT_KEY_SIZE];

    bzero(cKey, sizeof(cKey));

    [key getBytes:cKey length:FBENCRYPT_KEY_SIZE];

    

    // setup output buffer

    size_t bufferSize = [data length] + 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 = [NSData dataWithBytesNoCopy:buffer length: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

    unsigned char cKey[FBENCRYPT_KEY_SIZE];

    bzero(cKey, sizeof(cKey));

    [key getBytes:cKey length:FBENCRYPT_KEY_SIZE];

    

    // setup output buffer

    size_t bufferSize = [data length] + 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 = [NSData dataWithBytesNoCopy:buffer length:decryptedSize];

    } else {

        free(buffer);

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

    }

    

    return result;  

}



@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值