美文网首页iOS优秀开发文章
swift-RSA(二)-SecKeyEncrypt errSe

swift-RSA(二)-SecKeyEncrypt errSe

作者: ksnowlv | 来源:发表于2018-07-18 13:02 被阅读152次

swift中,使用SecKeyEncrypt进行RSA加密时,会出现errSecParam(-50)?

如何解决呢?

按照苹果官方文档中的errSecParam解释如下:

One or more parameters passed to the function are not valid
1.SecKeyEncrypt参数
  • key

    Public key with which to encrypt the data.

  • padding

    The type of padding to use. Possible values are listed in SecPadding. Typically, PKCS1 is used, which adds PKCS1 padding before encryption. If you specify none, the data is encrypted as-is.

  • plainText
    The data to encrypt.

  • plainTextLen

    Length in bytes of the data in the plainText buffer. This must be less than or equal to the value returned by the SecKeyGetBlockSize(:) function. When PKCS1 padding is performed, the maximum length of data that can be encrypted is 11 bytes less than the value returned by the SecKeyGetBlockSize(:) function (secKeyGetBlockSize() - 11).

  • cipherText
    On return, the encrypted text.

  • cipherTextLen

    On entry, the size of the buffer provided in the cipherText parameter. On return, the amount of data actually placed in the buffer.

2.SecKeyEncrypt参数解读及注意事项
  • key

    RSA的公钥,使用公钥加密,私钥解密。公钥加载不对或错误,直接报错errSecParam

  • padding

    选择PKCS1,不选PKCS1MD2/PKCS1MD5/PKCS1SHA1/PKCS1SHA224/PKCS1SHA256/PKCS1SHA384/PKCS1SHA512,这些是加签/验签时使用。该参数选错了,直接报错errSecParam

  • plainText/plainTextLen

    注意加密数据的长度,小于等于SecKeyGetBlockSize(:)-11 ,只要超过了该长度,直接报错errSecParam

  • cipherText/cipherTextLen
    cipherText加密后数据缓存区大于与cipherTextLen要一致。
    只要不一致,直接报错errSecParam

3.问题:
  • 1.为什么加密数据长度要求小于等于SecKeyGetBlockSize(:)-11?

      RSA加解密为块加密算法( block cipher algorithm),在固定长度的数据块上进行操作。
      block length是跟key length 以及所使用的填充模式相关
      PKCS1模式时:数据块长度最大为SecKeyGetBlockSize(:)-11
      OAEP模式时,数据块长度最大为SecKeyGetBlockSize(:)-42
    

相关文章

  • swift-RSA(二)-SecKeyEncrypt errSe

    swift中,使用SecKeyEncrypt进行RSA加密时,会出现errSecParam(-50)? 如何解决呢...

  • swift-RSA(一)

    如何使用swift进行RSA加解密呢? 一.这四个方面:加载公钥/加载私钥/RSA加密/RSA解密 1.加载公钥 ...

  • swift-RSA(四)-OAEP模式

    swift进行RSA加解密时,如果使用 OAEP时,有什么区别? 1.RSA加密 2.RSA解密 3.注意事项 1...

  • swift-RSA(五)-签名/验签

    RSA支持加解密,也支持签名/验签。使用rsa如何签名/验签呢? 1.rsa 签名 2.rsa 验签 3.调用

  • swift-RSA(三)-大数据块加解密

    如何使用swift进行RSA大数据块加解密呢? 1.使用RSA加密 2.使用RSA解密 3.调用及输出 调用 日志

  • 二(二)

    发什么神经 突然就很想花钱 一边心疼 一边毫不在乎的花 啧 莫名其妙

  • 二,二

    2017.9.11教师节后的周一,第一次走进教室,刚站到讲台两个小可爱送给我两束花,原谅我那时候人还没有认全没有记...

  • 二〇二〇

    本来这篇小结打算年初写的,但是想想后边还有复试就先放一放,结果复试结束后过了两个月才想起要写这篇小结... 时过境...

  • 二金二木二火二土

    今天看完了极简中国史,这本书看了半个多月,因为是八十年前写的书,不是白话文,所以看的特别累。不过从近代前辈的角度去...

  • 说二『似二非二的二』

    说实话原以为他最多似二,生活小节或许专门学着似二,中枢神经应该不至于非二,没想到最近越来越疯狂地绞尽脑汁地朝着二的...

网友评论

    本文标题:swift-RSA(二)-SecKeyEncrypt errSe

    本文链接:https://www.haomeiwen.com/subject/scsopftx.html