如何在IOS中使用RSA公钥加密数据

时间:2016-01-30 05:55:45

标签: php ios encryption cryptography rsa

我正在使用phpseclib lib进行rsa加密

        require_once = "./Crypt/RSA.php"
        $rsa = new Crypt_RSA();
        $rsa->setHash('sha1');
        $rsa->setMGFHash('sha1');
        $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_OAEP);
        $rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
        $rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);

        // Code to Generate the public key and private key

        $keys = $rsa->createKey(1024);
        extract($keys);

        // Base 64 encode Public and Private key
        $publicKeyBase = base64_encode( $publickey );
        $privateKeyBase = base64_encode( $privatekey );

        // Private key
        var_dump( $privateKeyBase );

        echo "---------------------------------------------------------</br>";

        // Public Key
        var_dump($publicKeyBase);

以bae64_encode格式生成如下的公钥:

LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0NCk1JSUNJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBZzhBTUlJQ0NnS0NBZ0VBbndac2s5QXpSUndEWXN5dksxLzYNCk11TTRVMlpkL3dIV1RaY01uVDR2ODdyaGVuMTZZM2szeDZ0OFJIWTBUY3hRZHJrQjIvVTZpQUdyRFMwaWppTFENClZLWmJtU1cvZ05xdk5UR0hUUlR3T0tlaVhadHNOQU01U1V2YWtVcytrMzBhd0t5U2NIV2xjL0tvMWJpZlBpYmUNCnZyM2xBTmhyMlFhT3kzY2dvbWszR040M1dpN01MTXFTMVY3M1Q4RUxYeGtDYkErRDA0WWgrZjFURVNRQkhwakoNCkIwYklBS0JKMkFYTE9Ieis1UUdFaXlGVGpMZDZsT3NpaW9LTThqbXk1R0JIdlladld0akt6RG9uVGNSQ3FpWisNCm1HYTNka1JyUkVtRDgvOXNRN0EzZldqeStBWWl1czdsRTRFZURCMWZzTFp2TVhKTzdFMGdrbk1kVGpsT3M0MU4NClhCNVVLWUZiZlJ4UWZLZlV0Y1N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