Encrypt/Decrypt string with rsa keys


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Encrypt/Decrypt string with rsa keys
Prev   Next
# 1  
Old 10-23-2009
Encrypt/Decrypt string with rsa keys

Hello,
I wanted to know if there was a way to encrypt a string, not a file using openssl and then decrypt it? I cant seem to get it to work.

This is what I have been trying but I'm not having much luck.
Code:
encTxt=`echo "$1" | openssl dgst -sha1 -binary | openssl rsautl -sign -inkey pri_keyfile.pem | openssl enc -base64`
echo `"$encTxt" | openssl enc -base64 -d | openssl rsautl -verify -inkey pub_keyfile.pem -pubin`

Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Encrypt and Decrypt a File with Password

Hello, I have few files on unix which are payroll related and I need them to encrypt with password so others wouldn't see the data. I use ETL tool and would like to know the unix command that does encryption/decryption to use in the ETL. Thank you, Sri (3 Replies)
Discussion started by: eskay
3 Replies

2. Shell Programming and Scripting

Encrypt and decrypt a password in shell script

Hi All, very good morning all. I am trying to connect to informatica repository by using shell script. I have written pmrep connect command in the script file. But i need to provide repository, domain ,username and password to connect. Username and password are hard coded in the script... (8 Replies)
Discussion started by: SekhaReddy
8 Replies

3. Shell Programming and Scripting

Encrypt and Decrypt

I have script for all oracle prod db. I have hard coded the username / password. I need a mechanism to encode and decode the username / password in a shell script. Another challenge is I use the username and password in a Select command for oracle DB. How can call the decrypted... (2 Replies)
Discussion started by: ilugopal
2 Replies

4. Shell Programming and Scripting

Encrypt and decrypt a string

Hi, I want to encrypt and decrypt a string(database password) which will be used in my scripts. encrypt the string while storing in a file and while using it in other scripts it should decrypt. i tried below method. As it can decrypt easily, it is not recommended. encrypt=`perl -e 'print unpack... (5 Replies)
Discussion started by: rohan10k
5 Replies

5. Linux

RSA decrypt with public key ?

Dear All, I need to decrypt with private key most of the time and this works for RSA. At times I need to decrypt with public key (data is encrypted with private key). This does not seem to work via VB.Net. Is there support for such an activity in Java on Linux or Windows ? Please advise. ... (3 Replies)
Discussion started by: Sushma Y
3 Replies

6. Shell Programming and Scripting

How to encrypt and decrypt a file

How to encrypt and decrypt a file using unix Command? Can any one help me? (2 Replies)
Discussion started by: laknar
2 Replies

7. Shell Programming and Scripting

Encrypt and Decrypt script

Dear Experts, I am using one script name :volume.sh and its written in bash shell script. I just want to encrypt the script so that any one else cannot see it. please tell me the commands how to encrypt the script as well as to decrypt it. Regards, SHARY (9 Replies)
Discussion started by: shary
9 Replies

8. Solaris

Decrypt Des file - then encrypt

Help.. I need to decrypt a file that was encrypted using DES 56 Bit. I have the encryption key and the block size used but no idea what utility to use.. I then need to encrypt the file using pgp and another key I have.. againt I dont know what utility to use. I am running solaris 9 .... ... (0 Replies)
Discussion started by: frustrated1
0 Replies

9. Shell Programming and Scripting

encrypt and decrypt password

how do i encrypt and decrypt a password (2 Replies)
Discussion started by: sanwish
2 Replies

10. Shell Programming and Scripting

Encrypt & Decrypt a String

Hi Everybody, I have a script that telnet another system. For some reasons, this is should be done by "root", so the root password has been written explicitly in this script, which mean any body read this script will know the root password of the other system. I think the solution is to write... (6 Replies)
Discussion started by: aldowsary
6 Replies
Login or Register to Ask a Question
RSA(3)							User Contributed Perl Documentation						    RSA(3)

NAME
Crypt::OpenSSL::RSA - RSA encoding and decoding, using the openSSL libraries SYNOPSIS
use Crypt::OpenSSL::Random; use Crypt::OpenSSL::RSA; # not necessary if we have /dev/random: Crypt::OpenSSL::Random::random_seed($good_entropy); Crypt::OpenSSL::RSA->import_random_seed(); $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($key_string); $rsa_pub->use_sslv23_padding(); # use_pkcs1_oaep_padding is the default $ciphertext = $rsa->encrypt($plaintext); $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($key_string); $plaintext = $rsa->encrypt($ciphertext); $rsa = Crypt::OpenSSL::RSA->generate_key(1024); # or $rsa = Crypt::OpenSSL::RSA->generate_key(1024, $prime); print "private key is: ", $rsa->get_private_key_string(); print "public key (in PKCS1 format) is: ", $rsa->get_public_key_string(); print "public key (in X509 format) is: ", $rsa->get_public_key_x509_string(); $rsa_priv->use_md5_hash(); # use_sha1_hash is the default $signature = $rsa_priv->sign($plaintext); print "Signed correctly " if ($rsa->verify($plaintext, $signature)); DESCRIPTION
Crypt::OpenSSL::RSA provides the ability to RSA encrypt strings which are somewhat shorter than the block size of a key. It also allows for decryption, signatures and signature verification. NOTE: Many of the methods in this package can croak, so use eval, or Error.pm's try/catch mechanism to capture errors. Also, while some methods from earlier versions of this package return true on success, this (never documented) behavior is no longer the case. Class Methods new_public_key Create a new Crypt::OpenSSL::RSA object by loading a public key in from a string containing Base64/DER-encoding of either the PKCS1 or X.509 representation of the key. The string should include the -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with the use_xxx_padding methods new_private_key Create a new Crypt::OpenSSL::RSA object by loading a private key in from an string containing the Base64/DER encoding of the PKCS1 representation of the key. The string should include the -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with use_xxx_padding. generate_key Create a new Crypt::OpenSSL::RSA object by constructing a private/public key pair. The first (mandetory) argument is the key size, while the second optional argument specifies the public exponent (the default public exponent is 65537). The padding is set to PKCS1_OAEP, but can be changed with use_xxx_padding methods. new_key_from_parameters Given Crypt::OpenSSL::Bignum objects for n, e, and optionally d, p, and q, where p and q are the prime factors of n, e is the public exponent and d is the private exponent, create a new Crypt::OpenSSL::RSA object using these values. If p and q are provided and d is undef, d is computed. Note that while p and q are not necessary for a private key, their presence will speed up computation. import_random_seed Import a random seed from Crypt::OpenSSL::Random, since the OpenSSL libraries won't allow sharing of random structures across perl XS modules. Instance Methods DESTROY Clean up after ourselves. In particular, erase and free the memory occupied by the RSA key structure. get_public_key_string Return the Base64/DER-encoded PKCS1 representation of the public key. This string has header and footer lines: -----BEGIN RSA PUBLIC KEY------ -----END RSA PUBLIC KEY------ get_public_key_x509_string Return the Base64/DER-encoded representation of the "subject public key", suitable for use in X509 certificates. This string has header and footer lines: -----BEGIN PUBLIC KEY------ -----END PUBLIC KEY------ and is the format that is produced by running "openssl rsa -pubout". get_private_key_string Return the DER-encoded PKCS1 representation of the private key. encrypt Encrypt a binary "string" using the public (portion of the) key. decrypt Decrypt a binary "string". Croaks if the key is public only. private_encrypt Encrypt a binary "string" using the private key. Croaks if the key is public only. public_decrypt Decrypt a binary "string" using the public (portion of the) key. sign Sign a string using the secret (portion of the) key. verify Check the signature on a text. use_no_padding Use raw RSA encryption. This mode should only be used to implement cryptographically sound padding modes in the application code. Encrypting user data directly with RSA is insecure. use_pkcs1_padding Use PKCS #1 v1.5 padding. This currently is the most widely used mode of padding. use_pkcs1_oaep_padding Use EME-OAEP padding as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding parameter. This mode of padding is recommended for all new applications. It is the default mode used by Crypt::OpenSSL::RSA. use_sslv23_padding Use PKCS #1 v1.5 padding with an SSL-specific modification that denotes that the server is SSL3 capable. use_md5_hash Use the RFC 1321 MD5 hashing algorithm by Ron Rivest when signing and verifying messages. use_sha1_hash Use the RFC 3174 Secure Hashing Algorithm (FIPS 180-1) when signing and verifying messages. This is the default. use_sha224_hash, use_sha256_hash, use_sha384_hash, use_sha512_hash These FIPS 180-2 hash algorithms, for use when signing and verifying messages, are only available with newer openssl versions (>= 0.9.8). use_ripemd160_hash Dobbertin, Bosselaers and Preneel's RIPEMD hashing algorithm when signing and verifying messages. size Returns the size, in bytes, of the key. All encrypted text will be of this size, and depending on the padding mode used, the length of the text to be encrypted should be: pkcs1_oaep_padding at most 42 bytes less than this size. pkcs1_padding or sslv23_padding at most 11 bytes less than this size. no_padding exactly this size. check_key This function validates the RSA key, returning a true value if the key is valid, and a false value otherwise. Croaks if the key is public only. get_key_parameters Return Crypt::OpenSSL::Bignum objects representing the values of n, e, d, p, q, d mod (p-1), d mod (q-1), and 1/q mod p, where p and q are the prime factors of n, e is the public exponent and d is the private exponent. Some of these values may return as undef; only n and e will be defined for a public key. The Crypt::OpenSSL::Bignum module must be installed for this to work. is_private Return true if this is a private key, and false if it is private only. BUGS
There is a small memory leak when generating new keys of more than 512 bits. AUTHOR
Ian Robertson, iroberts@cpan.org. For support, please email perl-openssl-users@lists.sourceforge.net. SEE ALSO
perl(1), Crypt::OpenSSL::Random(3), Crypt::OpenSSL::Bignum(3), rsa(3), RSA_new(3), RSA_public_encrypt(3), RSA_size(3), RSA_generate_key(3), RSA_check_key(3) perl v5.18.2 2011-08-24 RSA(3)