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
- AES加密
- AES加密
- AES加密
- AES加密
- AES 加密
- AES加密
- AES 加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES加密
- AES 加密
- 一个简单的本地存储操作,直接操作模型的方式操作缓存
- PHP 对象的存储与传输(序列化 serialize 对象)
- HTTP 1.1与HTTP 1.0的比较
- 使用pm2部署nodejs
- 杨辉三角/二项式定理
- AES加密
- git--全局删除项目中的.DS_Store文件
- Map遍历的四种方法及比较
- 在UIAlterView的代理方法中 点击确定时 添加一个UIView到[[UIApplication sharedApplication].keyWindow上只出现了0.5秒左右就消失的问题
- 用java执行网页信息爬取代码
- Java并发包使用注意事项
- centOS7定时删除文件
- 最强蓝牙库--RxAndroidBLE
- Web架构