Openssl解密显示额外的字符 [英] Openssl decryption displays extra characters

查看:199
本文介绍了Openssl解密显示额外的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从PHP应用程序发送和接收加密数据到iOS,反之亦然。

I want to send and receive encrypted data from a PHP application to iOS and vice versa.

一切运行正常,直到我解密从iOS应用程序接收的字符串在PHP中添加额外的字符,例如当在iOS中加密字符串(foo)时,这里是PHP中的输出。

Everything runs fine until i decrypt the string received from iOS application, it adds extra characters on display in PHP, for instance when a string (foo) is encrypted in iOS, here is the output i get in PHP.

foo¤º   ¤º  äØKŒCarrier+†7†ÐHÀ€ôa$aLa°aTa(aX¿€<a@aèaaTwaØwaäwa0®50‡4€B4ÀB4°†4ð†4þ;Ð}2}20¸>@¸> ¸>LGa¿

字符foo存在于解密的字符串中,但是大量额外的字符。

The character foo exist in the decrypted string, but with lot of extra character.

我想知道这是iOS的问题还是PHP?如果是IOS可能是可能的问题,它是编码,填充还是其他?

I am wondering if this is iOS issue or PHP & if it is IOS what could be the possible issue? is it encoding, padding or any other?

PS:iOS应用程序正在使用RSA_PKCS1_PADDING,当我使用RSA_NO_PADDING与其他字符在这里是我得到的输出。

P.S: iOS application is using RSA_PKCS1_PADDING, when i use RSA_NO_PADDING with other character here is the output i get.

foodrrrrrrøžäØKŒJustify (Undo action name)äØKŒ!Raise Baseline (Undo action name)äØKŒClear Recent Searches?0xÔ\Õ8Ò|Ó`œšÕØf"œš€œšÔ

注意字符串对齐(撤消动作名称)提高基线(撤消动作名称)可能是什么可能的问题这里?

Notice the string Justify (Undo action name) , Raise Baseline (Undo action name) what could be the possible issue here?

更新:

这是我的PHP代码:

Update :
Here is my PHP Code:

header('Content-Type: text/html; charset=utf-8');
$encrypt = Model_SSL::getInstance()->encrypt("PankeshRaj@iOS");
echo Model_SSL::getInstance()->decrypt("nhXQ7Yg1o5GzhXLe6x1LoKxK/Sg5qPDdA4SCtuH5LIp2klEPKQLAsOf+2ODg+A30k3EzyAjH+xKun9x4HPNIb67ETe4+84oMTPCk5TTuxOrSMNOjwm6L4mp01frOfT/OOcKvtCs+IuW3fWbbvbw/sgecDMG0IuuhsWvvL9FaOpLXkT8vGBOYh7pQH8Gkworf/dnonf/Rfk1vUYWkWlK7n/ycbl7uVVyIO+v3tcKfDdJuEvr4qOd5z3h323i7zvuHbt04WyPrV9Pjk0Zd9C2xte/K5bGHHTlRNZCEdLp5jhB9r7ljL4ToCpAiKSaBeG0HktXjDdW4+X1piPHox6e9Gw==");

这里是我的Model_SSL

And here is my Model_SSL

<?php
class Model_SSL
{
    protected $publicKey;

    protected $privateKey;

    protected static $ssl;

    private function __construct()
    {
        $this->publicKey = file_get_contents(APPLICATION_PATH.'/../resources/ssl_keys/key.pub');
        $this->privateKey = file_get_contents(APPLICATION_PATH.'/../resources/ssl_keys/private.pem');
    }

    public static function getInstance()
    {
        if (!isset(self::$ssl)) {
            self::$ssl = new self;
        }
        return self::$ssl;
    }

    public function encrypt($string)
    {
        openssl_public_encrypt($string, $cryptedText, $this->publicKey);
        return base64_encode($cryptedText);
    }

    public function decrypt($string)
    {
        $string = base64_decode($string);
        openssl_private_decrypt($string, $decryptedText, $this->privateKey);
        return $decryptedText;
    }
}

对于Objective-c,我正在尝试一个示例库,这是非常冗长的发布在这里,这里是我使用的图书馆的链接: https:// github.com/jslim89/RSA-Example

For Objective-c i am trying with a sample library, which is quite very lengthy to post here, here is the link to library which i am using : https://github.com/jslim89/RSA-Example

推荐答案

解决方案是实现一个函数来删除填充加密字符串中的字符串长度。

The solution was to implement a function to remove padding by including the string length in encrypted string.

我已经在这里解释了 OpenSSL加密填充问题

这篇关于Openssl解密显示额外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆