首页 > 其他分享 >Example for generate RSA key

Example for generate RSA key

时间:2023-11-03 20:44:57浏览次数:34  
标签:rsa private RSA pem ----- key Example

1. Use OpenSSL

Generate private key:
> openssl genrsa -out private.pem 2048
By default the format of output is PKCS#1-PEM
Generate public key:
> openssl rsa -in rsa_private.pem -pubout -out rsa_public.pem

From PEM to DER
> openssl rsa -in rsa_private.pem -outform der-out rsa_private.der

From PKCS#1 to PKCS#8
> openssl pkcs8 -topk8 -in rsa_private.pem -out pkcs8_private.pem

To signs the xxx.txt with hash algorithm SHA1 and save to xxx.sign
> openssl dgst -sha1 -out xxx.sign -sign rsa_private.pem xxx.txt

To verifies the xxx.sign file 
> openssl dgst -sha1 -verify public_key.pem -signature xxx.sign xxx.txt

2. Use Python

Generate key pair
import rsa

(pubkey, privkey) = rsa.newkeys(2048)
with open('public.pem', 'w') as f:
    f.write(pubkey.save_pkcs1().decode())
with open('private.pem', 'w') as f:
    f.write(privkey.save_pkcs1().decode())
Encrypt and Decrypt
with open('public.pem', 'r') as f:
    pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('private.pem', 'r') as f:
    privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())

plaintext = '0123456789'
ciphertext = rsa.encrypt(plaintext.encode(), pubkey)
print(rsa.decrypt(ciphertext, privkey).decode())
Signs and Verify
import base64

signature = rsa.sign(plaintext.encode(), privkey, 'SHA-256')
b64fmt = base64.b64encode(signature)
print(b64fmt)
try:
    signature = base64.b64decode(b64fmt)
    rsa.verify(plaintext.encode(), signature, pubkey)
except rsa.VerificationError:
    print('Verifiy error')
else:
    print('Verify succeed')

3. Use C with OpenSSL 1.1.1
    BIGNUM* bn = BN_new();
    BN_set_word(bn, RSA_3);  // exponent

    RSA* rsa = RSA_new();
    int ret = RSA_generate_key_ex(
        rsa,  /* pointer to the RSA structure */
        2048, /* number of bits for the key - 2048 is a good value */
        bn,   /* exponent allocated earlier */
        NULL  /* callback - can be NULL if progress isn't needed */
    ); 

    if (ret == 1)
        RSA_print_fp(stdout, rsa, 0);
    
    RSA_free(rsa);
    BN_free(bn);

 


4. What is a PEM,DER and PKCS8?

Privacy-Enhanced Mail (PEM) is a file formats for cryptographic material.
The PEM format is the DER format encoded in base64 with additional header and footer lines to be transported
The header and footer lines in the PEM format defines what type of PEM file it is.
RSA Private Key:
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
Public Key Certificate:
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
DER Distinguished Encoding Rules is a key file format for cryptographic data.
It's the binary ASN.1 encoding of the data (not in plain text otherwise it's a PEM.

PKCS8 is the eighth of the Public-Key Cryptography Standards (PKCS) and is a syntax for storing private key material.
The private keys may be encrypted with a symmetric key algorithm.

Difference between PKCS8 and PKCS1
PKCS #8 is a private key syntax for all algorithms and not just RSA. PKCS1 is primarily for using the RSA algorithm.
PKCS #8 also uses ASN.1 which identifies the algorithm in its structure.
Over time, while PKCS1 is still valid, PKCS #8 has become the standard syntax for storing private key information.
 

标签:rsa,private,RSA,pem,-----,key,Example
From: https://www.cnblogs.com/inthelight/p/17790680.html

相关文章

  • 【论文阅读】Generative Adversarial Nets
    GoodfellowI,Pouget-AbadieJ,MirzaM,etal.Generativeadversarialnets[J].Advancesinneuralinformationprocessingsystems,2014,27.引用:61648原作者代码:GitHub-goodfeli/adversarial:Codeandhyperparametersforthepaper"GenerativeAdversarialN......
  • 20.5 OpenSSL 套接字RSA加密传输
    RSA算法同样可以用于加密传输,但此类加密算法虽然非常安全,但通常不会用于大量的数据传输,这是因为RSA算法加解密过程涉及大量的数学运算,尤其是模幂运算(即计算大数的幂模运算),这些运算对于计算机而言是十分耗时。其次在RSA算法中,加密数据的长度不能超过密钥长度减去一定的填充长度。......
  • [PG] Another example of FCSA
    functionactualargumentsandcadidatesT=(193341,23,23)C=[(193341,1700,1700),(1700,1700,1700),(1043,1700,1700)]querytypeinformation,selectoid,typname,typcategorytypcat,typispreferredpreferredfrompg_typewhereoidin(193341,2......
  • jackson序列化key排序
    对象在序列化的时候对key进行排序使用 JsonPropertyOrder```java@Target({ElementType.ANNOTATION_TYPE,ElementType.TYPE,ElementType.METHOD,ElementType.CONSTRUCTOR,ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)@JacksonAnnotationpublic@interface......
  • Keras TypeError: ('Keyword argument not understood:', 'input')
    TypeError:('Keywordargumentnotunderstood:','input') model=Model(input=[inputs],output=output)报错信息TypeError:('Keywordargumentnotunderstood:','input')解决方法换成model=Model(inputs=...,outputs=...) ......
  • TypeError: fit() got an unexpected keyword argument 'nb_epoch'
    model.fit(trainX,trainY,nb_epoch=200,batch_size=2,verbose=2) It'sjust epochs now. nb_epoch wasdeprecatedyearsago. 把nb_epoch......
  • keystone服务的详解
    一:keystone服务作用:1:提供其余组件的所有认证服务,令牌的管理2:用户管理:验证用户的身份合法流程就是openstack上的用户,进行一个初始化,将信息存储在数据库里面去,然后第一次登录的时候,会去数据库里面去找数据进行校验,然后返回用户一个令牌,以后用户有这个令牌的话,使用某些服务的时候......
  • doris FE启动异常:org.yaml.snakeyaml.representer.Representer: method <init>()V not
    dorisFF启动异常,异常信息如下:  2023-11-0109:53:22,691INFO(main|1)[PaloFe.start():124]PaloFEstarting...2023-11-0109:53:22,699INFO(main|1)[FrontendOptions.analyzePriorityCidrs():107]configuredprior_cidrsvalue:10.252.226.5/242023-11-0109:5......
  • AliOS-monkey测试汇总
    一、什么是monkey测试   Monkey测试是一种软件测试类型,测试人员在没有预定义测试用例的情况下,应用随机测试用例测试程序,并检查系统行为。Monkey测试的目的是使用探索性技术来发现错误1。Monkey测试是一种黑盒测试。当测试人员为了编写和执行函数而出现时间不足时,就可......
  • Redis Bigkey排查
    在处理bigkey问题可以先从一下几点入手什么是bigkey?bigkey危害?bigkey是如何产生的?如何发现bigkey?如何处理bigkey?什么是BigkeyRedisbigkey是指在Redis数据库中占用空间较大的键值对。这些键通常包含了大量的数据,可能会影响Redis的性能和内存使用。例如,在一个集合......