Skip to content

dsyer/spring-security-rsa

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
May 11, 2017 11:16
src
June 29, 2023 07:57
July 10, 2014 07:27
November 27, 2019 14:28
June 28, 2023 12:39
May 11, 2017 11:16
June 28, 2023 14:16

This little project provides some RSA extensions to the base spring-security-crypto library. Currently supported: encryption and decryption with 2 algorithms wrapped up in the Spring Security Crypto interfaces TextEncryptor and BytesEncryptor. Example round trip:

TextEncryptor encryptor = new RsaSecretEncryptor();
String cipher = encryptor.encrypt("my message");
String message = encryptor.decrypt(cipher);

Above we create an encryptor with a random RSA key (the default constructor), and use it to encrypt and then decrypt a message. the default constructor is useful for testing, but for more durable use cases you can inject a private key or a KeyPair using the other constructors.

The encryption algorithm in the RsaSecretEncryptor is to generate a random 16-byte password, and use that to encrypt the message. The password is then itself RSA encrypted and prepended to the cipher text. The cipher test is base64 encoded (if using the TextEncryptor interface).

The other algorithm is in the RsaRawEncryptor which does raw RSA encryption on the whole message. We recommend the RsaSecretEncryptor.

N.B. if you need RSA signing and verification there are utilities already available in spring-security-jwt.

Important Prerequisites: to use Spring Security RSA you need the full-strength JCE installed in your JVM (it's not there by default). You can download the "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" from Oracle, and follow instructions for installation (essentially replace the 2 policy files in the JRE lib/security directory with the ones that you downloaded).