gpt4 book ai didi

iphone - 如何在 iPhone 中使用 DES 加密 NSString 值?

转载 作者:可可西里 更新时间:2023-11-01 03:27:51 24 4
gpt4 key购买 nike

我使用以下代码用 key 加密 NSString 值,

NSString *token = @"encryptValue";
NSString *key = @"encryptKey";

NSData * keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
NSData * plainData = [token dataUsingEncoding: NSUTF8StringEncoding];
NSMutableData * encData = [NSMutableData dataWithLength: plainData.length + 256];
size_t bytesWritten = 0;
CCCryptorStatus ccStatus = CCCrypt (kCCEncrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding,
[keyData bytes],
kCCKeySizeDES,
NULL,
[plainData bytes],
[plainData length],
encData.mutableBytes,
encData.length,
&bytesWritten);
encData.length = bytesWritten;
NSString *encrypted = [[NSString alloc] initWithBytes:[encData bytes] length:[encData length] encoding: NSUTF8StringEncoding];
NSLog(@"ENCRYPT : %@",encrypted);

但我得到的 (null) 是结果。有什么问题吗?如何加密 NSString 值?

最佳答案

这是一种用于 AES 的方法,对于 DES 可能是类似的,看看这对我有用...在 NSData
上创建一个新类别 AES对于NSData+AES.h

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>

@interface NSData (AES)

- (NSData*) EncryptAES: (NSString *) key;
- (NSData *) DecryptAES: (NSString *) key;

@end

对于NSData+AES.m

    #import "NSData+AES.h"
@implementation NSData(AES)

- (NSData*) EncryptAES: (NSString *) key
{
char keyPtr[kCCKeySizeAES256+1];
bzero( keyPtr, sizeof(keyPtr) );

[key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];
size_t numBytesEncrypted = 0;

NSUInteger dataLength = [self length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

CCCryptorStatus result = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL,
[self bytes], [self length],
buffer, bufferSize,
&numBytesEncrypted );

if( result == kCCSuccess )
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

return nil;
}

- (NSData *) DecryptAES: (NSString *) key
{
char keyPtr[kCCKeySizeAES256+1];
bzero( keyPtr, sizeof(keyPtr) );

[key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];

size_t numBytesEncrypted = 0;

NSUInteger dataLength = [self length];

size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer_decrypt = malloc(bufferSize);

CCCryptorStatus result = CCCrypt( kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL,
[self bytes], [self length],
buffer_decrypt, bufferSize,
&numBytesEncrypted );

if( result == kCCSuccess )
return [NSData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];

return nil;
}

@end

用法:导入NSData+AES.h

//Encryption
NSString *myString = @"This is the content";
NSString *key = @"my secret key";
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [data EncryptAES:key];

//Decryption
NSString *decryptedString = [[[NSString alloc] initWithData:[encryptedData DecryptAES:key] encoding:NSUTF8StringEncoding] autorelease]; // remove autorelease if using ARC

关于iphone - 如何在 iPhone 中使用 DES 加密 NSString 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10631076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com