我有用於數據編碼的iOS來源,並嘗試在Android應用中實現相同的編碼。 iOS版來源:在iOS和Android上實現相同的RSA加密
- (NSString *)encryptRSA:(NSString *)plainTextString useKeyWithTag:(NSString *)tag withSecPadding:(SecPadding)padding {
SecKeyRef publicKey = [self _getPublicKeyRefByTag:tag];
size_t cipherBufferSize = SecKeyGetBlockSize(publicKey);
uint8_t *cipherBuffer = malloc(cipherBufferSize);
uint8_t *nonce = (uint8_t *)[plainTextString UTF8String];
SecKeyEncrypt(publicKey,
padding,
nonce,
strlen((char*)nonce),
&cipherBuffer[0],
&cipherBufferSize);
NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize];
free(cipherBuffer);
return [encryptedData base64EncodedStringWithOptions:0];
}
函數調用:
[self.rsaManager encryptRSA:inputText withSecPadding:kSecPaddingPKCS1];
在Android中我做下一個:
public static byte[] encrypt(byte[] text, PublicKey key) throws Exception {
final Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding");
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(text);
}
函數調用:
Base64.getEncoder().encodeToString(encrypt(inputText.getBytes(), publicKey))
在結果我得到不同的字符串在iOS和Android相同inputText
。我做錯了什麼?
互操作性應該通過加密+欺騙來檢查。 –
你可以嘗試[RNCryptor](https://github.com/RNCryptor),因爲它爲所有平臺提供了庫,或者你可以從那裏檢查你的代碼有什麼問題。 – Aanabidden