Implement same RSA encryption on iOS and Android -
i have ios sources data encoding , try implement same encoding in android app. ios sources:
- (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]; }
function call:
[self.rsamanager encryptrsa:inputtext withsecpadding:ksecpaddingpkcs1];
in android make next:
public static byte[] encrypt(byte[] text, publickey key) throws exception { final cipher cipher = cipher.getinstance("rsa/none/pkcs1padding"); // encrypt plain text using public key cipher.init(cipher.encrypt_mode, key); return cipher.dofinal(text); }
function call:
base64.getencoder().encodetostring(encrypt(inputtext.getbytes(), publickey))
in result different strings on ios , android same inputtext
. i'm doing wrong?
pkcs1 padding adds element of randomness encryption. if encrypt same thing twice, should different ciphertexts. both ciphertexts should decrypt same plaintext (modulo added randomness, should handled pkcs1 implementation).
https://en.wikipedia.org/wiki/optimal_asymmetric_encryption_padding
Comments
Post a Comment