ios - RSA 用Security.framwork解密问题
PHP中文网
PHP中文网 2017-04-17 16:26:52
0
2
369

我用RSA的public key来解密,php那边用private key来加密,用的Security.framework来解密。但是解出来的数据前面多了一小串奇怪的乱码,后面数据是正确的。谁能帮我看看哪里有问题?

- (NSData *)decryptToDataWithPublicKey: (NSData *)dataToDecrypt { NSData *wrappedSymmetricKey = dataToDecrypt; SecKeyRef key = self.publicKey;//这是之前已经成功使用的SecKeyRef size_t cipherBufferSize = SecKeyGetBlockSize(key); size_t keyBufferSize = [wrappedSymmetricKey length]; NSMutableData *bits = [NSMutableData dataWithLength:keyBufferSize]; __unused OSStatus sanityCheck = SecKeyDecrypt(key, kSecPaddingNone, (const uint8_t *) [wrappedSymmetricKey bytes], cipherBufferSize, [bits mutableBytes], &keyBufferSize); [bits setLength:keyBufferSize]; return [bits copy]; }

最后的数据前面多了一堆fffff,不知道哪里出了问题..
f结束后的数据是正确的。

PHP中文网
PHP中文网

认证0级讲师

reply all (2)
大家讲道理

Ask and answer your own questions. In the end, this method still did not find a solution, and was finally abandoned. Instead, we used the feature of OC to be compatible with C, poured it into the openssl framework, and used openssl functions to decrypt.Security.framework

- (NSString *)decryptToDataWithPublicKey: (NSString *)dataToDecrypt { NSData *contentData = [dataToDecrypt base64DecodedData]; unsigned char *decrypted = (unsigned char *)malloc(rsa_public_len - 1); char *err = NULL; if (RSA_public_decrypt([contentData length], [contentData bytes], decrypted, rsa_publicKey, RSA_NO_PADDING) == -1) { ERR_load_CRYPTO_strings(); fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err)); fprintf(stderr, "Error %s\n", err); return nil; } RSA_free(rsa_publicKey); NSString *str = [NSString stringWithUTF8String:(char *) decrypted]; return str; }
    迷茫

    Have you noticed the Chinese garbled characters in openssl RSA encryption? Chinese base64 encoding doesn’t work either

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!