PHP语言RSA使用总结

1.密钥格式有两种:

A.PKCS8(JAVA适用)

B.PKCS1(非java适用)

可以使用工具进行格式转换,支付宝官网有.

2.证书格式有

A.pfx里面有私钥和公钥,读取可能需要密码,如果没有密码传入"".

openssl_pkcs12_read ( string $pkcs12 , array &$certs , string $pass ) : bool

B.cer存储使用二进制格式,文本打开为乱码,可使用base64_encode转换为实体文本内容.CER=>pem格式方法如下:

/**
     * CER证书转换为pem格式
     * */
    private function cer_to_pem($cer){
        $cer =  '-----BEGIN CERTIFICATE-----'.PHP_EOL
        .chunk_split(base64_encode($cer), 64, PHP_EOL)
        .'-----END CERTIFICATE-----'.PHP_EOL;
        return $cer;
    }

C.pem存储为文本格式,可以直接读取使用.

D.其他格式暂未使用过,略

3.PHP内置函数别名的疑问,功能是一样的.

成功,返回真实的密钥资源标识符(非实体文本,打印会显示NULL),错误,则返回 FALSE

神奇的事情:如果不使用该函数,直接使用文本格式也可以正常签名和验签,PHP 5.6.x环境下,其他版本未测试.

转载于:https://my.oschina.net/singyen/blog/3035941

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Qt 语言实现 RSA 算法加密文件的示例代码: ```cpp #include <QCoreApplication> #include <QFile> #include <QDataStream> #include <QDebug> #include <openssl/rsa.h> #include <openssl/pem.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 加载公钥 RSA *rsa = RSA_new(); FILE *fp = fopen("public_key.pem", "r"); rsa = PEM_read_RSA_PUBKEY(fp, &rsa, NULL, NULL); fclose(fp); // 打开待加密文件 QFile file("plaintext.txt"); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Failed to open file."; return -1; } // 读取待加密数据 QByteArray plaintext = file.readAll(); file.close(); // 计算加密后数据长度并分配内存 int plaintextLen = plaintext.size(); int keyLen = RSA_size(rsa); int cipherLen = 0; if (plaintextLen % keyLen == 0) { cipherLen = plaintextLen / keyLen * RSA_size(rsa); } else { cipherLen = (plaintextLen / keyLen + 1) * RSA_size(rsa); } unsigned char *ciphertext = new unsigned char[cipherLen]; // 使用公钥加密数据 int cipherBlockLen = RSA_public_encrypt(keyLen - 11, (unsigned char*)plaintext.constData(), ciphertext, rsa, RSA_PKCS1_PADDING); int totalCipherLen = cipherBlockLen; for (int i = keyLen - 11; i < plaintextLen; i += keyLen - 11) { cipherBlockLen = RSA_public_encrypt(keyLen - 11, (unsigned char*)plaintext.constData() + i, ciphertext + totalCipherLen, rsa, RSA_PKCS1_PADDING); totalCipherLen += cipherBlockLen; } // 保存加密后数据到文件 QFile cipherFile("ciphertext.bin"); if (!cipherFile.open(QIODevice::WriteOnly)) { qDebug() << "Failed to create cipher file."; return -1; } QDataStream out(&cipherFile); out.writeRawData((const char*)ciphertext, totalCipherLen); cipherFile.close(); // 释放内存 delete[] ciphertext; RSA_free(rsa); return a.exec(); } ``` 在示例代码中,我们使用 OpenSSL 库中提供的 `RSA` 类型和 `PEM_read_RSA_PUBKEY` 函数加载公钥文件。然后打开待加密文件,读取文件中的数据。接着计算加密后数据的长度,并分配内存。最后,使用公钥对数据进行加密,保存加密后数据到文件中。 需要注意的是,在使用 RSA 算法加密数据时,每次加密的数据长度不能超过公钥长度减去 11,因此我们需要对待加密数据进行分块加密。在示例代码中,我们使用一个循环来实现分块加密。 需要使用的 OpenSSL 库头文件为 `openssl/rsa.h` 和 `openssl/pem.h`。在编译时需要链接 OpenSSL 库,可以在项目文件中添加以下配置: ```qmake LIBS += -lssl -lcrypto ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值