# RSA cross platform

**URL:** <https://www.ruby-forum.com/t/rsa-cross-platform/160871>\
**Category:** Ruby\
**Created:** [February 20, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/rsa-cross-platform/160871 "2009-02-20T14:38:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [February 20, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/rsa-cross-platform/160871/1 "2009-02-20T14:38:39Z")

</div>

I am using Debian OS and ruby 1.8 version .

I have attached .pfx certificate file . Which was created using openssl  
command as follows ,

# create a file containing key and self-signed certificate

openssl req   
-x509 -nodes -days 365   
-newkey rsa:1024 -keyout mycert.pem -out mycert.pem

# export mycert.pem as PKCS#12 file, mycert.pfx

openssl pkcs12 -export   
-out mycert.pfx -in mycert.pem   
-name “My Certificate”

Using the openssl library in ruby I am able to do encryption and  
decryption . Apart from this . I encrypted data in C#( windows ) using  
this .pfx file . The c# coding is ,  
public string GetEncryptedText(string PlainStringToEncrypt)

```
{

    X509Certificate2 x509_2 = new X509Certificate2("C:/mycert.pfx",

```

“paymate”,X509KeyStorageFlags.MachineKeySet);

```
    X509Store store = new X509Store(StoreLocation.CurrentUser);

    store.Open(OpenFlags.ReadWrite);

        foreach (X509Certificate2 cert in store.Certificates)

        {

            if

```

(cert.SubjectName.Name.Contains(DigitalCertificateName))

```
            {

                x509_2 = cert;

                break;

            }

        }

        if (x509_2 == null)

            throw new Exception("No Certificate could be found in

```

name " + DigitalCertificateName);

```
    try

    {

        string PlainString = PlainStringToEncrypt.Trim();

        byte[] cipherbytes =

```

ASCIIEncoding.ASCII.GetBytes(PlainString);

```
        RSACryptoServiceProvider rsa =

```

(RSACryptoServiceProvider)x509\_2.PublicKey.Key;

```
        byte[] cipher = rsa.Encrypt(cipherbytes, false);

        return Convert.ToBase64String(cipher);

    }

    catch (Exception e)

    {

            throw e;

    }

```

}  
Encrypted Data is :  
“qLH9NZcxGL2vwuee4uryO8xphinBdE7XWttQmWfLhmOXVv5kFHW3JHTR0MiDfvONcHtPtvzKaCvgKlThd2XoNQm3K0EfRMKyokvDSWITQjTjHMOKyNGRCknsfm1dTrlZKU5eYNV+Qzn+MDdJ2gAb4vldbjFchgMFs2Qb2RJz3f4=”

This encrypted data was created in C#(windows) using above code but  
mycert.pfx was created in linux machine . Now I want to do  
decryption in ruby linux .  
I followed following decryption methods in ruby ,  
create the privatekey.pem using openssl command is ,  
openssl pkcs12 -in mycert.pfx -out private.pem -nodes  
#give the password as “paymate”

ruby coding to decrypting is ,  
private\_key = OpenSSL::PKey::RSA.new(File.read("./private.pem"))  
private\_key.private\_decrypt(Base64.decode64(“qLH9NZcxGL2vwuee4uryO8xphinBdE7XWttQmWfLhmOXVv5kFHW3JHTR0MiDfvONcHtPtvzKaCvgKlThd2XoNQm3K0EfRMKyokvDSWITQjTjHMOKyNGRCknsfm1dTrlZKU5eYNV+Qzn+MDdJ2gAb4vldbjFchgMFs2Qb2RJz3f4=”  
))

#But it through me error

kindly provide me any other solution in ruby

---

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [July 9, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/rsa-cross-platform/160871/2 "2009-07-09T15:24:51Z")

</div>

I have wrongly used the key .

use instead of private.pem , what you have created key file using  
openssl .

But it has been working fine . No problem at all .

Above steps are useful for doing encryption in windows and decryption in  
Linux especially on UNIX based opearating systems .

cheers,  
ashikali .

---

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [July 9, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/rsa-cross-platform/160871/3 "2009-07-09T15:26:17Z")

</div>

Ashikali A. wrote:

> I have wrongly used the key .
> 
> use instead of private.pem , what you have created key file using  
> openssl .
> 
> But it has been working fine . No problem at all .
> 
> Above steps are useful for doing encryption in windows and decryption in  
> Linux especially on UNIX based opearating systems .
> 
> cheers,  
> ashikali .

Yeah, It is working fine

With regards,  
Veeramani. N

---

<div class="post-metadata">

**Author:** ![ashikalim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@ashikalim](https://www.ruby-forum.com/u/ashikalim)\
**Post date:** [July 9, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/rsa-cross-platform/160871/4 "2009-07-09T15:28:11Z")

</div>

Now RSA is possible in cross platform .

cheers,  
vetrivel
