Forum: Ruby Encrypt data with public key provided as a X509 certificate

B7640cf891a72d0a863fc5c8bcfdd351?d=identicon&s=25 Fa D. (fa_d)
on 2013-04-14 21:37
Hi,

I'm struggling with encryption using a x509 certificate.

Take a look

openssl x509 -in ChavePublicaAT.cer -text -noout
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            3b:fd:ef:b6:00:00:00:00:04:97
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: DC=local, DC=ritta, CN=DGITA Issuing CA2
        Validity
            Not Before: Nov 19 15:06:46 2012 GMT
            Not After : Nov 18 15:06:46 2017 GMT
        Subject: DC=local, DC=ritta, OU=DGITA, OU=Servicos
Centrais-Satelite, OU=DivSeguranca, CN=Chave Cifra Publica AT
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:a1:08:be:5c:33:3d:38:9f:8a:af:e0:65:6b:19:
                    f7:30:50:44:ed:43:18:2f:b6:df:3c:60:b1:4b:42:
....
        X509v3 extensions:
            X509v3 Key Usage:
                Key Encipherment, Data Encipherment
            S/MIME Capabilities:
...


So, how can I use this certificate to RSA encryption?


If I do:

  @public=OpenSSL::PKey::RSA.new File.read(File.join('./', 'rsa',
'ChavePublicaAT.cer'))
  Base64::encode64(@public.public_encrypt(data)).rstrip


I hit error :

Neither PUB key nor PRIV key: nested asn1 error
(OpenSSL::PKey::RSAError)

So, I would appreciate any help.

Regards
Fabio
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.