- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 AES 加密的文件。
我使用以下 NSData 类别:
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AES)
- (NSData *)AES256DecryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
keyPtr,
kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
NSLog(@"Bytes decrypted: %d",numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
NSLog(@"Decrypt failed with error code %d",cryptStatus);
free(buffer); //free the buffer;
return nil;
}
@end
[NSData dataWithContentsOfFile:dataPath];
- (NSData *)readDataOfLength:(NSUInteger)length
{
HTTPLogTrace2(@"%@[%p]: readDataOfLength:%lu", THIS_FILE, self, (unsigned long)length);
if (![self openFileIfNeeded])
{
// File opening failed,
// or response has been aborted due to another error.
return nil;
}
// Determine how much data we should read.
//
// It is OK if we ask to read more bytes than exist in the file.
// It is NOT OK to over-allocate the buffer.
UInt64 bytesLeftInFile = fileLength - fileOffset;
NSUInteger bytesToRead = (NSUInteger)MIN(length, bytesLeftInFile);
// Make sure buffer is big enough for read request.
// Do not over-allocate.
if (buffer == NULL || bufferSize < bytesToRead)
{
bufferSize = bytesToRead;
buffer = reallocf(buffer, (size_t)bufferSize);
if (buffer == NULL)
{
HTTPLogError(@"%@[%p]: Unable to allocate buffer", THIS_FILE, self);
[self abort];
return nil;
}
}
// Perform the read
HTTPLogVerbose(@"%@[%p]: Attempting to read %lu bytes from file", THIS_FILE, self, bytesToRead);
ssize_t result = read(fileFD, buffer, bytesToRead);
// Check the results
if (result < 0)
{
HTTPLogError(@"%@: Error(%i) reading file(%@)", THIS_FILE, errno, filePath);
[self abort];
return nil;
}
else if (result == 0)
{
HTTPLogError(@"%@: Read EOF on file(%@)", THIS_FILE, filePath);
[self abort];
return nil;
}
else // (result > 0)
{
HTTPLogVerbose(@"%@[%p]: Read %d bytes from file", THIS_FILE, self, result);
fileOffset += result;
NSData *data = [NSData dataWithBytes:buffer length:result];
return [data AES256DecryptWithKey:@"abcdefghijklmnopqrstuvwxyz123456"];
//return data;
}
}
[Switching to process 13059 thread 0x0]
2011-05-25 18:00:03.631 Drm[1843:6e0b] Bytes decrypted: 131072
2011-05-25 18:00:03.647 Drm[1843:6e0b] Bytes decrypted: 68096
[Switching to process 11779 thread 0x0]
2011-05-25 18:00:04.547 Drm[1843:6e0b] Bytes decrypted: 0
2011-05-25 18:00:04.555 Drm[1843:6e0b] Decrypt failed with error code -4300
最佳答案
AES 是一种分组密码。您必须一次解密一个 block 。 AES block 是 128 位的(这与 AES256DecryptWithKey
中的“256”无关)。所以你必须保证你传递的数据是16字节的倍数。
我没试过CCCrypt()
这样,这不是它的真正用途。 CCCrypt()
当您想要进行一次性解密时,这是一个便利功能。当你想做“随用随用”的解密时,你可以使用 CCCryptorCreate()
然后多次调用CCCryptorUpdate()
最后是 CCCryptorFinal()
(或者您可以调用 CCCryptorFinal()
然后调用 CCCryptorReset()
以使用相同的 key 解密更多内容)。最后您调用CCCryptorRelease()
释放你的密码器。
编辑 我想了更多,并意识到 CCCrypt()
即使您将输入分解为 16 字节的 block ,也不能以这种方式使用。每个 AES 加密 block 都会修改下一个 block 的 IV,因此您不能只在流中间启动某人。这就是为什么你需要一个持久的 CCCryptor
对象在整个 session 中。
关于objective-c - NSData 与 CCCrypt 在多线程环境中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6127318/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我有一个使用 Java 加密的字符串 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.EN
我正在尝试使用 CCCrypt 来加密我的数据,但它出了点问题,加密后数据长度发生了变化,但在解密时没有发生,这是以下代码。 - (NSData *)AES256EncryptWithKeyGener
我正在尝试解码一些数据,但是当数据大于 520,000(字节?它是 [数据长度])时,我的应用程序将在 CCCrypt 上的调试器上崩溃行,或者如果与调试器分离,它只会卡住并且实际上不会正确崩溃(Sp
当我加密文件(doc、pdf 等)时,我使用: size_t bufferSize = dataLength + kCCBlockSizeAES128; CCCrypt( kCCEncrypt,
代码如下: - (NSData *) doCipher: (NSData *) plainData key: (NSData *) symmetricKey context: (CCOperati
当我加密一个字符串然后立即解密时,CCCryptorStatus 为 -4304 并且生成的解码字符串长度不正确。谁能看出我可能做错了什么? //Encrypt NSString *plainText
我正在获取 CCCryptorStatus as kCCSuccess。 这意味着加密已成功完成。 但是,我想获取加密数据,如何获取? 任何帮助将不胜感激。 谢谢, 法会拉托 最佳答案 好好看看 CC
在我的 iOS 应用程序中,我必须解密来自服务器的数据。我使用 CommonCrypto 框架,经过几次试验,我成功解密了 CCCrypt(kCCDecrypt, // operation
下面是我加密字符串的代码, NSString *token = @"us=foo;pw=bar;pwAlg=false;"; NSString *key = @"testtest"; const vo
我有一个使用 AES 加密的文件。 我使用以下 NSData 类别: #import @implementation NSData (AES) - (NSData *)AES256DecryptWi
//Stateless, one-shot encrypt operation CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt,
我尝试编写一个基于 CCCrypt 的加密 (AESCBC256) 函数,而 CCCrypt 正在生成一个随机值。 例如,当我将 1234567 传递给函数时,对于相同的 iv 和 key ,它将返回
对于 iPad 应用程序(ios5.0,arc),我需要加密传输到我正在使用的 Web 服务的数据。在阅读有关通过 Apple 提供的加密时,我遇到了两个 API 来执行此操作:CCCrypt 和 S
我使用 2 个库来加密密码,但使用相同的密码,我得到不同的值:。 这是我在android中使用的代码: String dataEncrypted = new String(); try { C
我在使用 SWIFT 3 进行加密和解密时遇到了一个奇怪的行为.我正在使用以下方法加密和解密字符串。加密时我生成随机盐并将其附加到加密数据的末尾,解密时我从数据中读取 IV 数据以解密并执行解密操作。
我正在尝试使用 DES 加密来加密密码(不要问为什么 DES,我知道它不太安全)。我是第一次在 iOS 中这样做,因此不得不依赖另一篇关于如何做的帖子。 当我运行加密时它返回空值,与解密一个已经加密的
我有一个使用 CCCrypt() 的解密/加密方法,它在 iOS5 上运行得非常好。现在我正在使用 iOS6 SDK 并且从未更改过我的代码,但似乎有什么地方坏了。我仍然可以用一个 key 加密一个字
我正在将一个有效的 Objective-C 类别 (NSData+AESCrypt.m) 移植到 Swift,我发现了一个使用指针的问题。 Swift 中加密部分的代码编译正确,但生成运行时 EXEC
我正在针对 iOS 的 Kotlin 多平台项目中研究 AES256 加密算法。 我检查了一些在纯 Kotlin 中实现这一点的现有库(例如 krypto ),但没有一个符合我对其余代码的要求(已经在
我是一名优秀的程序员,十分优秀!