Archived Forum Post

Index of archived forum posts

Question:

How to Generate RSA Private Key and Store Encrypted Value in DB Table?

Mar 08 '13 at 15:22

We are trying to generate an RSA key pair, then encrypt privatekey w/password, then store the encrypted value in a table.


Answer

See this example:

ASP: Generate RSA Key and Export to Encrypted PEM

SQL Server: Generate RSA Key and Export to Encrypted PEM

C#: Generate RSA Key and Export to Encrypted PEM

C++: Generate RSA Key and Export to Encrypted PEM

Unicode C++: Generate RSA Key and Export to Encrypted PEM

Objective-C: Generate RSA Key and Export to Encrypted PEM

IOS: Generate RSA Key and Export to Encrypted PEM

PowerShell: Generate RSA Key and Export to Encrypted PEM

MFC: Generate RSA Key and Export to Encrypted PEM

C: Generate RSA Key and Export to Encrypted PEM

Unicode C: Generate RSA Key and Export to Encrypted PEM

Delphi: Generate RSA Key and Export to Encrypted PEM

DelphiDll: Generate RSA Key and Export to Encrypted PEM

Visual FoxPro: Generate RSA Key and Export to Encrypted PEM

Java: Generate RSA Key and Export to Encrypted PEM

Androidâ„¢: Generate RSA Key and Export to Encrypted PEM

Perl: Generate RSA Key and Export to Encrypted PEM

PHP: Generate RSA Key and Export to Encrypted PEM

PHP: Generate RSA Key and Export to Encrypted PEM

Python: Generate RSA Key and Export to Encrypted PEM

Ruby: Generate RSA Key and Export to Encrypted PEM

VB.NET: Generate RSA Key and Export to Encrypted PEM

Visual Basic: Generate RSA Key and Export to Encrypted PEM

VBScript: Generate RSA Key and Export to Encrypted PEM


Answer

encrypt with public key to decrypt, select row, and load the private key use decrypt, setting the usePrivatekey to 1.

Start with a new RSA object to demonstrate that all we

need are the keys previously exported:

my $rsaEncryptor = new chilkat::CkRsa(); $success = $rsaEncryptor->UnlockComponent("ComponentKey"); if ($success != 1) { print "RSA component unlock failed" . "n"; exit; }

Encrypted output is always binary. In this case, we want

to encode the encrypted bytes in a printable string.

Our choices are "hex", "base64", "url", "quoted-printable".

$rsaEncryptor->put_EncodingMode("hex");

We'll encrypt with the public key and decrypt with the private

key. It's also possible to do the reverse.

$public contains the public key

$rsaEncryptor->ImportPublicKey($public);

$usePrivateKey = 0; my $encryptedStr; connectDB();

open the password file

if (open(MYFILE, "c:\public\passwd")) { $line = <myfile>; while ($line ne "") { chomp($line); $id = trim(substr($line,0,48)); $pass = trim(substr($line,48,32)); $encryptedStr = $rsaEncryptor->encryptStringENC($pass,$usePrivateKey); insertDB($id, $encryptedStr); $line = <myfile>; } } else { die("Cannot open the file INPUT"); } close MYFILE; disConnectDB();