public class RSA
extends java.lang.Object
Below is an example of generated key pair, the public-key is in section __RSAPublicKey__ and the private-key is in section __RSAPrivateKey__ __RSAPublicKey__ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU+Myd2Bax9lDac5fjvl8k4QUTXsBt37BF1h4M 2eGs9HXapi6izJbrE+1/Y1iMtR+jvd/tMxEltbU9ZdfsAk1vC859O9pga6/+AeoYkS2Gh7tmmAUc PpafagY62A1ehrM9j0THvdNbyuQhnVh3ipKaMV4Cv4fMsZBPQhq3TZKLCwIDAQAB __RSAPrivateKey__ MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJT4zJ3YFrH2UNpzl+O+XyThBRNe wG3fsEXWHgzZ4az0ddqmLqLMlusT7X9jWIy1H6O93+0zESW1tT1l1+wCTW8Lzn072mBrr/4B6hiR LYaHu2aYBRw+lp9qBjrYDV6Gsz2PRMe901vK5CGdWHeKkpoxXgK/h8yxkE9CGrdNkosLAgMBAAEC gYB6KbkqK50Y3SQasDY7hc7iIEEYTFhDAe+8pUTJRxdgzKuk3ShbSqjsJ9BX6bEg2hsTHM735ivG n8b7jXvOx2iVSgGHdwKECW94Vr3yACMbshFwwRssz4clAApAsgg+gruwVbEhXQH5oJcz0TxRLmkr tmvS9xuvW24kIOKBVQCQsQJBAM2v7REtTQdQmsM8cn9ehKvITpJ8J1zw+Z7otIjzGOa9qfqBUDiE Dmsr+0e9OQS1PeMGJ2scrtOE6DtqATH94ukCQQC5aWACxLAQz3jIsmFH33EbmP6omx6uiS8JM08Z hikgHi8qpZXy4YMaPHwhgOttLww3Sl1scSUaOR0+237K5z3TAkEArJN+Gdpjdc64ezTdcGaSdDs1 SYynzkBVgHII/5rP+tGJq6gR8clC+8LEK3XlQED9Mtz399PyD4DFBQy1Agfd+QJBAK1RMWylC6hL KK8DACi9V+/iTnqxtjaVCiDhk5labesWDCUjcE4APBs0Wxy+dFQ13Id6vmX2RiaDKTFu4NEu4/0C QQCfmhqfQEaY1SrqLFf2aNmBvK312ggDBh9k/a+zzSbJa34U6TfuRmYcdo4/SAxY9WN5yTNW4lW7 imrUc9d3yCll//encrypt the secret-data with public-key and print the encrypted-data to console
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CLASS_NAME |
static int |
ENGRYPT_KEY_LENGHT
The default length of generated key, it is 1024
|
static java.lang.String |
KEY_ALGORITHM
The algorithm of encryption, it is 'RSA'
|
static java.lang.String |
MAPKEY_FOR_PRIVATEKEY
The map key for storing a private key in the map, it is '__RSAPrivateKey__'
|
static java.lang.String |
MAPKEY_FOR_PUBLICKEY
The map key for storing a public key in the map, it is '__RSAPublicKey__'
|
static java.lang.String |
PARAM_DATA |
static java.lang.String |
PARAM_DECRYPTE |
static java.lang.String |
PARAM_ENCRYPT |
static java.lang.String |
PARAM_GENERATE_KEY |
static java.lang.String |
PARAM_KEY |
static java.lang.String |
PARAM_OUT_FILE |
static java.lang.String |
PARAM_TEST |
static java.lang.String |
SAFS_ENCRYPTED_STRING_PREFIX |
static java.lang.String |
SIGNATURE_ALGORITHM
The algorithm of signature, it is 'MD5withRSA'
|
static java.lang.String |
UTF8_CHARSET
The default encoding used to interpret plain-text, it is 'UTF-8'
|
Constructor and Description |
---|
RSA() |
Modifier and Type | Method and Description |
---|---|
static java.security.Key |
decodeEncodedKey(java.lang.String encodedKey,
boolean isPrivate) |
static java.security.PrivateKey |
decodePrivateKey(java.lang.String encodedPrivateKey) |
static java.security.PublicKey |
decodePublicKey(java.lang.String encodedPublicKey) |
static byte[] |
decryptByPrivateKey(byte[] data,
java.lang.String encodedPrivateKey)
Decrypt the bytes data by private key.
|
static java.lang.String |
decryptByPrivateKey(java.lang.String data,
java.lang.String encodedPrivateKey)
Decrypt the base64-encoded enctyped string data by private key.
|
static java.lang.String |
decryptByPrivateKey(java.lang.String data,
java.lang.String encodedPrivateKey,
java.lang.String encoding)
Decrypt the base64-encoded enctyped string data by private key.
|
static byte[] |
decryptByPublicKey(byte[] data,
java.lang.String encodedPublicKey)
Decrypt the bytes data by public key.
|
static java.lang.String |
decryptByPublicKey(java.lang.String data,
java.lang.String encodedPublicKey)
Decrypt the base64-encoded enctyped string data by public key.
|
static java.lang.String |
decryptByPublicKey(java.lang.String data,
java.lang.String encodedPublicKey,
java.lang.String encoding)
Decrypt the base64-encoded enctyped string data by public key.
|
static byte[] |
encryptByPrivateKey(byte[] data,
java.lang.String encodedPrivateKey) |
static java.lang.String |
encryptByPrivateKey(java.lang.String data,
java.lang.String encodedPrivateKey)
Encrypt the string data by private key, data is interpreted as UTF-8 encoding.
|
static java.lang.String |
encryptByPrivateKey(java.lang.String data,
java.lang.String encodedPrivateKey,
java.lang.String encoding)
Encrypt the string data by private key.
|
static byte[] |
encryptByPublicKey(byte[] data,
java.lang.String encodedPublicKey)
Encrypt the bytes data by public key.
|
static java.lang.String |
encryptByPublicKey(java.lang.String data,
java.lang.String encodedPublicKey)
Encrypt the string data by public key, data is interpreted as UTF-8 encoding.
|
static java.lang.String |
encryptByPublicKey(java.lang.String data,
java.lang.String encodedPublicKey,
java.lang.String encoding)
Encrypt the string data by public key.
|
static java.lang.String |
getPrivateKey(java.util.Map<java.lang.String,java.lang.String> keyMap) |
static java.lang.String |
getPublicKey(java.util.Map<java.lang.String,java.lang.String> keyMap) |
static java.lang.String |
getUsage() |
static java.util.Map<java.lang.String,java.lang.String> |
initKeys()
Generate key-pair, the public-key and private-key; then base64-encode them and put them into a map.
|
static void |
main(java.lang.String[] args) |
static void |
readRemote(java.lang.String filename) |
static java.lang.String |
sign(byte[] data,
java.lang.String encodedPrivateKey)
Sign the data with private key, and base64-encode the signature.
|
static void |
testEncyptByPrivateKeyAndDecryptByPublicKey(byte[] data,
java.lang.String pubkey,
java.lang.String prikey) |
static void |
testEncyptByPublicKeyAndDecryptByPrivateKey(byte[] data,
java.lang.String pubkey,
java.lang.String prikey) |
static boolean |
verifySignature(byte[] data,
java.lang.String encodedPublicKey,
java.lang.String encodedSignature)
Use 'public key' to verify the data is signed (signed by 'private key') correclty.
|
public static final java.lang.String CLASS_NAME
public static final java.lang.String UTF8_CHARSET
public static final int ENGRYPT_KEY_LENGHT
public static final java.lang.String KEY_ALGORITHM
public static final java.lang.String SIGNATURE_ALGORITHM
public static final java.lang.String MAPKEY_FOR_PUBLICKEY
public static final java.lang.String MAPKEY_FOR_PRIVATEKEY
public static final java.lang.String SAFS_ENCRYPTED_STRING_PREFIX
public static final java.lang.String PARAM_TEST
public static final java.lang.String PARAM_GENERATE_KEY
public static final java.lang.String PARAM_OUT_FILE
public static final java.lang.String PARAM_ENCRYPT
public static final java.lang.String PARAM_DECRYPTE
public static final java.lang.String PARAM_DATA
public static final java.lang.String PARAM_KEY
public static java.security.PrivateKey decodePrivateKey(java.lang.String encodedPrivateKey) throws java.lang.Exception
encodedPrivateKey
- String the base64-encoded private key.java.lang.Exception
public static java.security.PublicKey decodePublicKey(java.lang.String encodedPublicKey) throws java.lang.Exception
encodedPublicKey
- String the base64-encoded public key.java.lang.Exception
public static java.security.Key decodeEncodedKey(java.lang.String encodedKey, boolean isPrivate) throws java.lang.Exception
encodedKey
- the base64-encoded keyisPrivate
- if true, then encodedKey is private; otherwise, the encodedKey is publicjava.lang.Exception
public static java.lang.String sign(byte[] data, java.lang.String encodedPrivateKey) throws java.lang.Exception
data
- byte[], the data to signencodedPrivateKey
- String, the base64 encoded private key stringjava.lang.Exception
public static boolean verifySignature(byte[] data, java.lang.String encodedPublicKey, java.lang.String encodedSignature) throws java.lang.Exception
data
- byte[], the signed data (by 'private key')encodedPublicKey
- String, the base64-encoded public key.encodedSignature
- String, the base64-encoded signature.java.lang.Exception
public static java.lang.String encryptByPrivateKey(java.lang.String data, java.lang.String encodedPrivateKey) throws java.lang.Exception
data
- String, the string data to encrypt, it will be interpreted as UTF-8 encodingencodedPrivateKey
- String, the base64-encoded private keyjava.lang.Exception
public static java.lang.String encryptByPrivateKey(java.lang.String data, java.lang.String encodedPrivateKey, java.lang.String encoding) throws java.lang.Exception
data
- String, the string data to encryptencodedPrivateKey
- String, the base64-encoded private keyencoding
- String, the encoding used to interpret the string datajava.lang.Exception
public static byte[] encryptByPrivateKey(byte[] data, java.lang.String encodedPrivateKey) throws java.lang.Exception
data
- byte[], the bytes data to encryptencodedPrivateKey
- String, the base64-encoded private keyjava.lang.Exception
public static java.lang.String decryptByPublicKey(java.lang.String data, java.lang.String encodedPublicKey) throws java.lang.Exception
data
- String, the base64-encoded string data to decrypt, the decrypted bytes will be interpreted by UTF-8 encodingencodedPublicKey
- String, the base64-encoded public keyjava.lang.Exception
public static java.lang.String decryptByPublicKey(java.lang.String data, java.lang.String encodedPublicKey, java.lang.String encoding) throws java.lang.Exception
data
- String, the base64-encoded string data to decrypt.encodedPublicKey
- String, the base64-encoded public keyencoding
- String, the encoding used to interpret the decryped bytes.java.lang.Exception
public static byte[] decryptByPublicKey(byte[] data, java.lang.String encodedPublicKey) throws java.lang.Exception
data
- byte[], the bytes data to decryptencodedPublicKey
- String, the base64-encoded public keyjava.lang.Exception
public static java.lang.String encryptByPublicKey(java.lang.String data, java.lang.String encodedPublicKey) throws java.lang.Exception
data
- String, the string data to encrypt, it will be interpreted as UTF-8 encodingencodedPublicKey
- String, the base64-encoded public keyjava.lang.Exception
public static java.lang.String encryptByPublicKey(java.lang.String data, java.lang.String encodedPublicKey, java.lang.String encoding) throws java.lang.Exception
data
- String, the string data to encryptencodedPublicKey
- String, the base64-encoded public keyencoding
- String, the encoding used to interpret the string datajava.lang.Exception
public static byte[] encryptByPublicKey(byte[] data, java.lang.String encodedPublicKey) throws java.lang.Exception
data
- byte[], the bytes data to encryptencodedPublicKey
- String, the base64-encoded public keyjava.lang.Exception
public static java.lang.String decryptByPrivateKey(java.lang.String data, java.lang.String encodedPrivateKey) throws java.lang.Exception
data
- String, the base64-encoded string data to decrypt, the decrypted bytes will be interpreted by UTF-8 encodingencodedPrivateKey
- String, the base64-encoded private keyjava.lang.Exception
public static java.lang.String decryptByPrivateKey(java.lang.String data, java.lang.String encodedPrivateKey, java.lang.String encoding) throws java.lang.Exception
data
- String, the base64-encoded string data to decrypt.encodedPrivateKey
- String, the base64-encoded private keyencoding
- String, the encoding used to interpret the decrypted bytesjava.lang.Exception
public static byte[] decryptByPrivateKey(byte[] data, java.lang.String encodedPrivateKey) throws java.lang.Exception
data
- byte[], the bytes data to decryptencodedPrivateKey
- String, the base64-encoded private keyjava.lang.Exception
public static java.lang.String getPrivateKey(java.util.Map<java.lang.String,java.lang.String> keyMap) throws java.lang.Exception
keyMap
- the map containing the base64-encoded private key.java.lang.Exception
#initKeys()}
public static java.lang.String getPublicKey(java.util.Map<java.lang.String,java.lang.String> keyMap) throws java.lang.Exception
keyMap
- the map containing the base64-encoded public key.java.lang.Exception
#initKeys()}
public static java.util.Map<java.lang.String,java.lang.String> initKeys() throws java.lang.Exception
MAPKEY_FOR_PRIVATEKEY
, encoded-private-key-string>MAPKEY_FOR_PUBLICKEY
, encoded-public-key-string>java.lang.Exception
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
public static java.lang.String getUsage()
public static void testEncyptByPublicKeyAndDecryptByPrivateKey(byte[] data, java.lang.String pubkey, java.lang.String prikey) throws java.lang.Exception
java.lang.Exception
public static void testEncyptByPrivateKeyAndDecryptByPublicKey(byte[] data, java.lang.String pubkey, java.lang.String prikey) throws java.lang.Exception
java.lang.Exception
public static void readRemote(java.lang.String filename) throws java.io.FileNotFoundException, java.io.IOException
java.io.FileNotFoundException
java.io.IOException
Copyright © SAS Institute. All Rights Reserved.