Class RSACipher
- java.lang.Object
-
- jetbrains.buildServer.serverSide.crypt.RSACipher
-
public class RSACipher extends Object
Utility class intended to simplify RSA encryption / decryption.
-
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_ENCRYPTED_DATA_SIZE
static int
MAX_NON_ASCII_ENCRYPTED_DATA_SIZE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
decryptData(String hexEncoded)
Decrypts hex encoded byte array with help of RSA private key.static String
decryptWebRequestData(String hexEncoded)
Decrypts hex encoded JavaScript encrypted data.static String
encryptData(String plain)
Encrypts specified string, and returns hex representation of the encrypted datastatic String
encryptData(String plain, PublicKey pubKey)
Encrypts data using the specified public key using default system charsetstatic String
encryptData(String plain, PublicKey pubKey, Charset charset)
Encrypts data using the specified public key using the given charsetstatic String
encryptDataForWeb(String plain)
Encrypts specified string, and returns hex representation of the encrypted data.static void
generateKeys()
Generates new public and private keys and stores them in static fields.static String
getHexEncodedPublicKey()
Returns hex representation of the public keystatic String
getHexEncodedPublicKeyExponent()
Returns hex representation of the public key exponentstatic PublicKey
recreateKey(String hexModulus, String hexExponent)
Recreates public key from hex encoded modulus & exponent.
-
-
-
Field Detail
-
MAX_ENCRYPTED_DATA_SIZE
public static final int MAX_ENCRYPTED_DATA_SIZE
- See Also:
- Constant Field Values
-
MAX_NON_ASCII_ENCRYPTED_DATA_SIZE
public static final int MAX_NON_ASCII_ENCRYPTED_DATA_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
generateKeys
public static void generateKeys()
Generates new public and private keys and stores them in static fields.
-
decryptData
public static String decryptData(String hexEncoded) throws DecryptionFailedException
Decrypts hex encoded byte array with help of RSA private key.- Parameters:
hexEncoded
- hex encoded encrypted data- Returns:
- decrypted string
- Throws:
DecryptionFailedException
- if decryption failed
-
decryptWebRequestData
@Nullable public static String decryptWebRequestData(String hexEncoded) throws DecryptionFailedException
Decrypts hex encoded JavaScript encrypted data.- Parameters:
hexEncoded
- hex encoded encrypted byte array- Returns:
- decrypted string
- Throws:
DecryptionFailedException
- if decryption failed
-
encryptData
@Nullable public static String encryptData(String plain) throws EncryptionFailedException
Encrypts specified string, and returns hex representation of the encrypted data- Parameters:
plain
- text to encrypt- Returns:
- hex representation of the encrypted data
- Throws:
EncryptionFailedException
- if encryption failed
-
encryptDataForWeb
public static String encryptDataForWeb(String plain) throws EncryptionFailedException
Encrypts specified string, and returns hex representation of the encrypted data. It differs fromencryptData(String)
in that it stores length of the encrypted string as a last byte into byte array before encryption- Parameters:
plain
- text to encrypt- Returns:
- hex representation of the encrypted data
- Throws:
EncryptionFailedException
- if encryption failed
-
encryptData
@Nullable public static String encryptData(String plain, PublicKey pubKey) throws EncryptionFailedException
Encrypts data using the specified public key using default system charset- Parameters:
plain
- text to encryptpubKey
- public key- Returns:
- data encrypted with specified public key in hex form
- Throws:
EncryptionFailedException
- if encryption failed
-
encryptData
@Nullable public static String encryptData(String plain, PublicKey pubKey, Charset charset) throws EncryptionFailedException, UnsupportedEncodingException
Encrypts data using the specified public key using the given charset- Parameters:
plain
- text to encryptpubKey
- public keycharset
- the Charset to be used to encode- Returns:
- data encrypted with specified public key in hex form
- Throws:
EncryptionFailedException
- if encryption failedUnsupportedEncodingException
-
getHexEncodedPublicKey
public static String getHexEncodedPublicKey()
Returns hex representation of the public key- Returns:
- hex representation of the public key
-
getHexEncodedPublicKeyExponent
public static String getHexEncodedPublicKeyExponent()
Returns hex representation of the public key exponent- Returns:
- hex representation of the public key exponent
-
recreateKey
public static PublicKey recreateKey(String hexModulus, String hexExponent) throws InvalidKeyException
Recreates public key from hex encoded modulus & exponent.- Parameters:
hexModulus
- hex encoded modulushexExponent
- hex encoded exponent- Returns:
- recreated public key
- Throws:
InvalidKeyException
- if key recreation failed
-
-