security - SecKeyEncrypt SecPaddingNone iOS -
i trying raw rsa encryption on ios. please not try , talk me out of it.
i doing encryption 2 keys. first step, using key a, uses secpaddingpkcs1 , gives me output of 256 bytes. after that, using key b, use secpaddingnone. assume output remain 256 bytes, becomes 512 bytes. not sure doing wrong, know? thinking wrong?
i use swiftyrsa library perform encryption.
thanks
edit code:
the encryptdata function has been copied on swiftyrsa. assume data
256 bytes, , result of first encryption key a.
let datastring = text.datausingencoding(nsutf8stringencoding) let certificatelabel = "certificate" let certificateref = self.getcertificatefromkeychain(certificatelabel) let certificatedata = self.getdatafromcertificate(certificateref) let cryptoimportexportmanager = cryptoexportimportmanager() let publickeyref = cryptoimportexportmanager.importpublickeyreferencefromdercertificate(certificatedata) let encrypteddata = self.encryptdata(data, publickey: publickeyref!, padding: secpadding.none)
let me know if need add more code.
thanks
this is bug in swiftyrsa; rsa returns blocksize
(in case 256) bytes after encryption. if encrypt more blocksize
bytes data split multiple chunks, each of encrypted, multiple of blocksize
bytes back.
when pkcs1 padding used effective blocksize reduced 11 bytes, means encrypting 256 bytes return 2 blocks or 512 bytes (since 256 > 256-11 or 245).
when no padding used, blocksize doesn't need reduced 11, swiftyrsa still this. have tested without 11 byte reduction using no padding , second encryption, without padding, returns 256 bytes expected. regression tests still pass , have confirmed openssl can correctly decrypt double encrypted data (first padding , encrypted second time no padding).
this has been fixed in git repo, if want patch local source work-around, fix change encryptdata
follows:
// encrypts data rsa key public func encryptdata(data: nsdata, publickey: seckeyref, padding: secp public func encryptdata(data: nsdata, publickey: seckeyref, padding: secpadding) throws -> nsdata { let blocksize = seckeygetblocksize(publickey) let maxchunksize = (padding == .none) ? blocksize : blocksize - 11 ...
Comments
Post a Comment