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