gpt4 book ai didi

iphone - iphone 中的 3des 加密

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:39 25 4
gpt4 key购买 nike

我是 iOS 开发和 Objective C 的新手。我正在开发一个将加密数据发送到服务器的应用程序。服务器使用 3des 和 cbc,没有填充。我已经阅读了 stackoverflow 中的大部分相关问题,但仍然无法正常工作。已经为此工作了几天,但仍然无法使其与服务器加密相匹配。

这是我的工作成果:

NSString* plaintexthex = @"536176696E67204163636F756E747C313233343536000000";
NSData *dTextIn = [self dataFromHexString:plaintexthex]; //my own way of convert hex to data

NSString* keyhex = @"6E7B336FD2051BA165A9362BD9735531";
NSData *_keyData = [self dataFromHexString:keyhex]; //my own way of convert hex to data

CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;

bufferPtrSize = ([dTextIn length] + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x00, bufferPtrSize);

uint8_t iv[kCCBlockSize3DES];
memset((void *) iv, 0x00, (size_t) sizeof(iv));

unsigned char *bytePtr = (unsigned char *)[_keyData bytes];

ccStatus = CCCrypt(kCCEncrypt, // CCoperation op
kCCAlgorithm3DES, // CCAlgorithm alg
kCCModeCBC, // CCOptions
[_keyData bytes], // const void *key
kCCKeySize3DES, // 3DES key size length 24 bit
iv, // const void *iv,
[dTextIn bytes], // const void *dataIn
[dTextIn length], // size_t dataInLength
bufferPtr, // void *dataOut
bufferPtrSize, // size_t dataOutAvailable
&movedBytes); // size_t *dataOutMoved

NSString *result;
NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length: (NSUInteger)movedBytes];
result = [self hexStringFromData:myData];

NSLog(@"Data to encrypt %@",dTextIn);
NSLog(@"Encryption key %@",_keyData);
NSLog(@"Bytes of key are %s ", bytePtr);
NSLog(@"Key length %d ",[_keyData length]);
NSLog(@"Encrypted bytes %@", myData);
NSLog(@"Encrypted string %@", result);
NSLog(@"Encrypted string length %d", [result length]);

- (NSData *)dataFromHexString:(NSString *)string
{
NSMutableData *stringData = [[[NSMutableData alloc] init] autorelease];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [string length] / 2; i++) {
byte_chars[0] = [string characterAtIndex:i*2];
byte_chars[1] = [string characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[stringData appendBytes:&whole_byte length:1];
}
return stringData;
}

我在Android平台上开发了一个类似的应用程序,它与服务器配合得很好。这是我在Android平台上使用的函数的加密。

public byte[] encrypt(byte[] key, byte[] message) throws Exception {

byte [] plainTextBytes = message;
byte[] encryptKey = key;

SecretKey theKey = new SecretKeySpec(encryptKey, "DESede");
Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
IvParameterSpec IvParameters = new IvParameterSpec(new byte[] {(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00});
cipher.init(Cipher.ENCRYPT_MODE, theKey, IvParameters);
byte[] encrypted = cipher.doFinal(plainTextBytes);
return encrypted;
}

基本上我想复制这种类似的加密以在 iOS 平台上使用。欢迎任何帮助,并在此先感谢您。

最佳答案

kCCModeCBC 是一种模式,而不是一个选项。您需要的选项是 0。 CBC 是 CCCrypt() 的默认模式。默认也是无填充。

关于iphone - iphone 中的 3des 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11928196/

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