Sto crittografando una stringa in ogg-c e anche crittografando la stessa stringa in Java usando AES e sto vedendo alcuni strani problemi. La prima parte del risultato corrisponde a un certo punto ma poi è diversa, quindi quando vado a decodificare il risultato da Java sull'iPhone non è in grado di decrittografarlo.Come posso rendere la mia crittografia AES identica tra Java e Objective-C (iPhone)?
Sto usando una stringa sorgente di "Ora, allora, e cos'è questa assurdità?" Lo sai? " Utilizzando una chiave di "123456789"
Il codice Objective-C per crittografare è la seguente: NOTA: si tratta di una categoria NSData così scontato che il metodo viene chiamato su un oggetto NSData cosi 'sé' contiene i dati byte crittografare.
- (NSData *)AESEncryptWithKey:(NSString *)key {
char keyPtr[kCCKeySizeAES128+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 numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* 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];
}
free(buffer); //free the buffer;
return nil;
}
E il codice di crittografia Java è ...
public byte[] encryptData(byte[] data, String key) {
byte[] encrypted = null;
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] keyBytes = key.getBytes();
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
encrypted = new byte[cipher.getOutputSize(data.length)];
int ctLength = cipher.update(data, 0, data.length, encrypted, 0);
ctLength += cipher.doFinal(encrypted, ctLength);
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage());
} finally {
return encrypted;
}
}
L'uscita esadecimale del codice Objective-C è -
7a68ea36 8288c73d f7c45d8d 22432577 9693920a 4fae38b2 2e4bdcef 9aeb8afe 69394f3e 1eb62fa7 74da2b5c 8d7b3c89 a295d306 f1f90349 6899ac34 63a6efa0
e l'uscita è java -
7a68ea36 8288c73d f7c45d8d 22432577 e66b32f9 772b6679 d7c0cb69 037b8740 883f8211 748229f4 723984beb 50b5aea1 f17594c9 fad2d05e e0926805 572156d
Come potete vedere, tutto va bene fino a -
7a68ea36 8288c73d f7c45d8d 22432577
Sono indovinando ho alcune delle impostazioni diverse, ma non si può capire che cosa, ho provato a cambiare tra la BCE e CBC sul lato Java e non ha avuto alcun effetto.
Qualcuno può aiutare !? per favore ....
Ragazzi mi avete salvato da un incubo autosufficiente ... Grazie! – Shade