Objective-C 中具有流支持的 AES/CCM 实现
特性
初始化
NSData *key = ...
NSData *iv = ...
NSData *adata = ...
NSInteger tagLength = ...
VPCCMCrypt *ccm = [[VPCCMCrypt alloc] initWithKey:key
iv:iv
adata:adata
tagLength:tagLength];
如何使用
数据到数据加密
NSData *plainData = ...
[ccm encryptDataWithData:plainData finishedBlock:^(NSData *data) {
//Do something with data
} errorBlock:^(NSError *error) {
NSLog(@"Encryption Error: %@", error);
}];
数据到数据解密
NSData *encryptedData = ...
[ccm decryptDataWithData:encryptedData finishedBlock:^(NSData *data) {
//Do something with data
} errorBlock:^(NSError *error) {
NSLog(@"Decryption Error: %@", error);
}];
文件到文件加密
NSURL *sourceURL = ...
NSURL *destinationURL = ...
[ccm encryptFileToFileWithSourceURL:sourceURL
destUrl:destinationURL
finishedBlock:^{
//Encryption finished
} errorBlock:^(NSError *error) {
NSLog(@"Encryption Error: %@", error);
}];
文件到文件解密
NSURL *sourceURL = ...
NSURL *destinationURL = ...
[ccm decryptFileToFileWithSourceURL:sourceURL
destUrl:destinationURL
finishedBlock:^{
//Decryption finished
} errorBlock:^(NSError *error) {
NSLog(@"Encryption Error: %@", error);
}];
流加密
NSURL *fileUrl = ...
[ccm encryptStreamWithUrl:fileUrl
dataBlock:^(NSData *data, BOOL isLastBlock) {
if (isLastBlock) {
//data = TAG
}
//Upload data to server
} errorBlock:^(NSError *error) {
NSLog(@"Encryption Error: %@", error);
}];
流解密
NSURL *fileUrl = ...
[ccm decryptStreamWithUrl:fileUrl
dataBlock:^(NSData *data, BOOL isLastBlock) {
//Do something with the decrypted data
} errorBlock:^(NSError *error) {
NSLog(@"Decryption Error: %@", error);
}];
特别感谢 Thanos Chatziathanasiou ([email protected]) 的帮助