com.eu.miscedautils.CommonUtils
Class RSAEncryptUtil

java.lang.Object
  extended by com.eu.miscedautils.CommonUtils.RSAEncryptUtil

public class RSAEncryptUtil
extends java.lang.Object

Title: RSAEncryptUtil

Description: Utility class that helps encrypt and decrypt strings using RSA algorithm

Version:
1.0
Author:
Aviran Mordo http://aviran.mordos.com

Constructor Summary
RSAEncryptUtil()
           
 
Method Summary
static byte[] copyBytes(byte[] arr, int length)
           
static byte[] decodeBASE64(java.lang.String text)
          Decode BASE64 encoded string to bytes array
static byte[] decrypt(byte[] text, java.security.PrivateKey key)
          Decrypt text using private key
static java.lang.String decrypt(java.lang.String text, java.security.PrivateKey key)
          Decrypt BASE64 encoded text using private key
static void decryptFile(java.lang.String srcFileName, java.lang.String destFileName, java.security.PrivateKey key)
          Decrypt file using 1024 RSA encryption
static java.lang.String encodeBASE64(byte[] bytes)
          Encode bytes array to BASE64 string
static byte[] encrypt(byte[] text, java.security.PublicKey key)
          Encrypt a text using public key.
static java.lang.String encrypt(java.lang.String text, java.security.PublicKey key)
          Encrypt a text using public key.
static void encryptDecryptFile(java.lang.String srcFileName, java.lang.String destFileName, java.security.Key key, int cipherMode)
          Encrypt and Decrypt files using 1024 RSA encryption
static void encryptFile(java.lang.String srcFileName, java.lang.String destFileName, java.security.PublicKey key)
          Encrypt file using 1024 RSA encryption
static java.security.KeyPair generateKey()
          Generate key which contains a pair of private and public key using 1024 bytes
static java.lang.String getKeyAsString(java.security.Key key)
          Convert a Key to string encoded as BASE64
static java.security.PrivateKey getPrivateKeyFromString(java.lang.String key)
          Generates Private Key from BASE64 encoded string
static java.security.PublicKey getPublicKeyFromString(java.lang.String key)
          Generates Public Key from BASE64 encoded string
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RSAEncryptUtil

public RSAEncryptUtil()
Method Detail

generateKey

public static java.security.KeyPair generateKey()
                                         throws java.security.NoSuchAlgorithmException
Generate key which contains a pair of private and public key using 1024 bytes

Returns:
key pair
Throws:
java.security.NoSuchAlgorithmException

encrypt

public static byte[] encrypt(byte[] text,
                             java.security.PublicKey key)
                      throws java.lang.Exception
Encrypt a text using public key.

Parameters:
text - The original unencrypted text
key - The public key
Returns:
Encrypted text
Throws:
java.lang.Exception

encrypt

public static java.lang.String encrypt(java.lang.String text,
                                       java.security.PublicKey key)
                                throws java.lang.Exception
Encrypt a text using public key. The result is enctypted BASE64 encoded text

Parameters:
text - The original unencrypted text
key - The public key
Returns:
Encrypted text encoded as BASE64
Throws:
java.lang.Exception

decrypt

public static byte[] decrypt(byte[] text,
                             java.security.PrivateKey key)
                      throws java.lang.Exception
Decrypt text using private key

Parameters:
text - The encrypted text
key - The private key
Returns:
The unencrypted text
Throws:
java.lang.Exception

decrypt

public static java.lang.String decrypt(java.lang.String text,
                                       java.security.PrivateKey key)
                                throws java.lang.Exception
Decrypt BASE64 encoded text using private key

Parameters:
text - The encrypted text, encoded as BASE64
key - The private key
Returns:
The unencrypted text encoded as UTF8
Throws:
java.lang.Exception

getKeyAsString

public static java.lang.String getKeyAsString(java.security.Key key)
Convert a Key to string encoded as BASE64

Parameters:
key - The key (private or public)
Returns:
A string representation of the key

getPrivateKeyFromString

public static java.security.PrivateKey getPrivateKeyFromString(java.lang.String key)
                                                        throws java.lang.Exception
Generates Private Key from BASE64 encoded string

Parameters:
key - BASE64 encoded string which represents the key
Returns:
The PrivateKey
Throws:
java.lang.Exception

getPublicKeyFromString

public static java.security.PublicKey getPublicKeyFromString(java.lang.String key)
                                                      throws java.lang.Exception
Generates Public Key from BASE64 encoded string

Parameters:
key - BASE64 encoded string which represents the key
Returns:
The PublicKey
Throws:
java.lang.Exception

encodeBASE64

public static java.lang.String encodeBASE64(byte[] bytes)
Encode bytes array to BASE64 string

Parameters:
bytes -
Returns:
Encoded string

decodeBASE64

public static byte[] decodeBASE64(java.lang.String text)
                           throws java.io.IOException
Decode BASE64 encoded string to bytes array

Parameters:
text - The string
Returns:
Bytes array
Throws:
java.io.IOException

encryptFile

public static void encryptFile(java.lang.String srcFileName,
                               java.lang.String destFileName,
                               java.security.PublicKey key)
                        throws java.lang.Exception
Encrypt file using 1024 RSA encryption

Parameters:
srcFileName - Source file name
destFileName - Destination file name
key - The key. For encryption this is the Private Key and for decryption this is the public key
cipherMode - Cipher Mode
Throws:
java.lang.Exception

decryptFile

public static void decryptFile(java.lang.String srcFileName,
                               java.lang.String destFileName,
                               java.security.PrivateKey key)
                        throws java.lang.Exception
Decrypt file using 1024 RSA encryption

Parameters:
srcFileName - Source file name
destFileName - Destination file name
key - The key. For encryption this is the Private Key and for decryption this is the public key
cipherMode - Cipher Mode
Throws:
java.lang.Exception

encryptDecryptFile

public static void encryptDecryptFile(java.lang.String srcFileName,
                                      java.lang.String destFileName,
                                      java.security.Key key,
                                      int cipherMode)
                               throws java.lang.Exception
Encrypt and Decrypt files using 1024 RSA encryption

Parameters:
srcFileName - Source file name
destFileName - Destination file name
key - The key. For encryption this is the Private Key and for decryption this is the public key
cipherMode - Cipher Mode
Throws:
java.lang.Exception

copyBytes

public static byte[] copyBytes(byte[] arr,
                               int length)