kipedia.org/wiki/Cipher">Cipher to convert an original input source using a {@code key} toan uninterpretable format. The resulting encrypted output is only able to be converted back to original form with a {@code key} as well. {@code CipherService}s can perform both encryption and decryption.
Cipher Basics
For what is known as
Symmetric {@code Cipher}s, the {@code Key} used to encrypt the source is the sameas (or trivially similar to) the {@code Key} used to decrypt it.
For
Asymmetric {@code Cipher}s, the encryption {@code Key} is not the same as the decryption {@code Key}. The most common type of Asymmetric Ciphers are based on what is called public/private key pairs:
A
private key is known only to a single party, and as its name implies, is supposed be kept very private and secure. A
public key that is associated with the private key can be disseminated freely to anyone. Then data encrypted by the public key can only be decrypted by the private key and vice versa, but neither party need share their private key with anyone else. By not sharing a private key, you can guarantee no 3rd party can intercept the key and therefore use it to decrypt a message.
This asymmetric key technology was created as a more secure alternative to symmetric ciphers that sometimes suffer from man-in-the-middle attacks since, for data shared between two parties, the same Key must also be shared and may be compromised.
Note that a symmetric cipher is perfectly fine to use if you just want to encode data in a format no one else can understand and you never give away the key. Shiro uses a symmetric cipher when creating certain HTTP Cookies for example - because it is often undesirable to have user's identity stored in a plain-text cookie, that identity can be converted via a symmetric cipher. Since the the same exact Shiro application will receive the cookie, it can decrypt it via the same {@code Key} and there is no potential for discovery since that Keyis never shared with anyone.
{@code CipherService}s vs JDK {@link javax.crypto.Cipher Cipher}s
Shiro {@code CipherService}s essentially do the same things as JDK {@link javax.crypto.Cipher Cipher}s, but in simpler and easier-to-use ways for most application developers. When thinking about encrypting and decrypting data in an application, most app developers want what a {@code CipherService} provides, rather than having to manage thelower-level intricacies of the JDK's {@code Cipher} API. Here are a few reasons why most people prefer{@code CipherService}s:
- Stateless Methods - {@code CipherService} method calls do not retain state between method invocations.JDK {@code Cipher} instances do retain state across invocations, requiring its end-users to manage the instanceand its state themselves.
- Thread Safety - {@code CipherService} instances are thread-safe inherently because no state isretained across method invocations. JDK {@code Cipher} instances retain state and cannot be used by multiplethreads concurrently.
- Single Operation - {@code CipherService} method calls are single operation methods: encryption ordecryption in their entirety are done as a single method call. This is ideal for the large majority of developer needs where you have something unencrypted and just want it decrypted (or vice versa) in a single method call. In contrast, JDK {@code Cipher} instances can support encrypting/decrypting data in chunks over time (because itretains state), but this often introduces API clutter and confusion for most application developers.
- Type Safe - There are {@code CipherService} implementations for different Cipher algorithms( {@code AesCipherService}, {@code BlowfishCipherService}, etc). There is only one JDK {@code Cipher} class torepresent all cipher algorithms/instances.
- Simple Construction - Because {@code CipherService} instances are type-safe, instantiating and usingone is often as simple as calling the default constructor, for example,
new AesCipherService();
. The JDK {@code Cipher} class however requires using a procedural factory method with String arguments to indicate howthe instance should be created. The String arguments themselves are somewhat cryptic and hard to understand unless you're a security expert. Shiro hides these details from you, but allows you to configure them if you want.
@author Les Hazlewood
@see BlowfishCipherService
@see AesCipherService
@since 1.0