Encrypt data with public key provided as a X509 certificate

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