需要在 Crypt RSA 中使用的密钥格式 [英] Key format need to use in Crypt RSA

查看:67
本文介绍了需要在 Crypt RSA 中使用的密钥格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个 Java 函数来创建密钥对.

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 ?

我想在我的 rsa php 代码中使用这个密钥.我是否需要转换为任何特定格式?

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.

php 中还有其他方法可以使用 RSA 公钥加密数据吗?

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

推荐答案

不幸的是,Java 在处理二进制原始数据时非常笨拙……但是,由于领先的 PKCS 填充会连接一些随机内容,以防止重放攻击和/或对加密数据进行统计分析,因此您每次都会获得不同的令牌

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

仅供参考,您想加密对称密钥并使用此类密钥对大型二进制数据进行编码/解码.不要直接使用 RSA 编码/解码您的最终数据!

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!

这篇关于需要在 Crypt RSA 中使用的密钥格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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