我尝试加密请求,我创建了这个功能,但我在.NET服务器上获得了不同的加密值。这是我在iOS中使用的功能:
- (NSData *)Encrypt3DESWithInput:(NSString *)input
key:(NSString *)key
{
NSData *inputData = [input dataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger dataLength = [inputData length];
size_t bufferSize = dataLength + kCCBlockSize3DES;
void *buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding,
[keyData bytes],
kCCKeySize3DES,
initializationVector,
[inputData bytes],
dataLength,
buffer,
bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
NSLog(@"Success");
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
这些是我在私钥和初始化向量中使用的值:
static NSString *const privateKey = @"";
static const char initializationVector[8] = {31, 10, 35, 17, 0, 42, 151, 64};
答案 0 :(得分:0)
使用密钥,该密钥是加密/解密例程的自然长度。不要依赖于键填充,这是未定义的。如果您有密码,请使用PBKDF2
创建一个全长安全密钥..
密钥字符串为@""
,密钥数据由[key dataUsingEncoding:NSUTF8StringEncoding]
NSString *keyString = @"";
NSData *keyData = [keyString dataUsingEncoding:NSUTF8StringEncoding];
NSLog输出:
keyString:''
keyData:<>
注意密钥长度为0,因此DES方法将从内存中访问随机数据。