Key format need to use in Crypt RSA

自作多情 提交于 2019-12-13 20:29:14

问题


I used this Java function to create Key pairs.

      KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
        keyGen.initialize(numBits);
        KeyPair keyPair = keyGen.genKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey  publicKey  = keyPair.getPublic();

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes  = publicKey.getEncoded();

I got this as public key(similar to this.Not possible to paste here):

0��0
    *�H��

I want to know which format key is this? pem/der ?

I want to use this key in my rsa php code. Whether I need to convert to any specific format?

$keyData =file_get_contents($configValues['metPubKey']); //'metPubKey 'variable contain file path

    $rsa = new Crypt_RSA();

        try{
            $rsa->loadKey($keyData); // public key
            // $rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_XML);
            //$rsa->setHash('md5');
            $encryptedToken = base64_encode($rsa->encrypt($token));
            print_r("after : \"".$encryptedToken."\"");
        }catch(Exception $e){
            // print_r($e);
            die;
        }

It is not throwing any exception. But for my curiosity while running same code with same data different tokens created each time. I just want to confirm.

Is there any other way in php to encrypt data using RSA public key?


回答1:


unfortunately Java is very clumsy when it comes to deal with binary raw data... however, you get different tokens each time due to the leading PKCS padding which concatenates some random stuff in order to prevent replay attacks and/or statistical analysis on your encrypted data

FYI you want to encrypt a symmetric key and use such key to encode/decode large binary data. Don't use RSA directly to encode/decode your final data!



来源:https://stackoverflow.com/questions/22861824/key-format-need-to-use-in-crypt-rsa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!