Ну, я пытаюсь зашифровать строку в цели c, расширяющую NSData, с помощью этого метода:
<code>
@implementation NSData (AES128)</p>
<ul>
<li><p>(NSData *)AES128Encrypt {
char keyPtr[kCCKeySizeAES128] = {'\xe1','\xaa','\x9c','\x61','\x46','\x74','\x44','\x56','\xf0','\xe5','\x47','\x46','\x86','\xdc','\x95','\x77'};</p>
<p>NSUInteger dataLength = [self length];</p>
<p>size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);</p>
<p>size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr,kCCKeySizeAES128,NULL /* initialization vector (optional) <em>/,[self bytes], dataLength, /</em> input <em>/buffer, bufferSize, /</em> output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}</p>
<p>free(buffer); //free the buffer;
return nil;
}</p></li>
<li><p>(NSData *)AES128Decrypt {
char keyPtr[kCCKeySizeAES128] = {'\xe1','\xaa','\x9c','\x61','\x46','\x74','\x44','\x56','\xf0','\xe5','\x47','\x46','\x86','\xdc','\x95','\x77'};</p>
<p>NSUInteger dataLength = [self length];</p>
<p>//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);</p>
<p>size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus=CCCrypt(kCCDecrypt,kCCAlgorithmAES128,kCCOptionPKCS7Padding,keyPtr, kCCKeySizeAES128,NULL /* initialization vector (optional) <em>/,[self bytes], dataLength, /</em> input <em>/buffer, bufferSize, /</em> output */&numBytesDecrypted);</p>
<p>if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}</p>
<p>free(buffer); //free the buffer;
return nil;
}</p></li>
</ul>
<p>@end
, тогда я вызываю его здесь:
<code>
NSString *strData = @"My string";</p>
<p>NSData *objNSData = [NSData dataWithData:[strData dataUsingEncoding: NSUTF8StringEncoding]];</p>
<p>NSLog(@"encrypted: %@",[objNSData description]);
ЕслиЯ просто использую его в цели c, он отлично работает.Но когда я пытаюсь отправить его на сервер java, он не работает.
Мои данные шифра выглядят так:
86fcf0fa9e3dff93dc8918ffd02ee203 12de0bf8c8ba300456293c4240296c0d
, если я пытаюсьэто в Java, используя также AES с тем же ключом, я получаю это:
86fcf0fa9e3dff93dc8918ffd02ee203 8388f173da143c6aeeb90e554259c83c
это странно, потому что первая половина его так же может *
может бытьпроисходит?спасибо.