Skip to content

Commit

Permalink
Prepare release 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
robinkanters committed Sep 14, 2016
2 parents c29a3ed + b1f7908 commit 1755be8
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 28 deletions.
18 changes: 3 additions & 15 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

340 changes: 340 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

85 changes: 84 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
# easencrypt
# EasEncrypt

The simple encryption library for Java.

## Introduction

The Java encryption and cipher APIs can be a bit challenging, especially for people who are
inexperienced in encryption. This library is meant to be an abstraction/simplification so that
everyone can easily integrate RSA/DSA and/or AES/DES into their projects.

## Example

First, initialize the encryptors with just 3 lines of code:

```
// Initialize the RSA cipher. This can be swapped out for a custom implementation.
AsymmetricEncrypter rsaEncrypter = new RsaEncrypter();
// Generate a keypair. Since this is the default Java keypair, it's compatible with external code.
KeyPair keyPair = rsaEncrypter.generateKeyPair();
// Initialze an encrypter with a cipher (which can be swapped out for other implementations).
LongTextEncrypter encrypter = new LongTextEncrypterImpl(new DesCipher(), rsaEncrypter);
```

...and you're off to the races! Now just encrypt and decrypt whenever you like:

### Encrypt

```
String encrypted = encrypter.encrypt("Hello world!", keyPair.getPublic());
String decrypted = encrypter.decrypt(encrypted, keyPair.getPrivate()); // "Hello world!"
```

## Installation

Since EasEncrypt is only hosted on Github, you need to install [JitPack](https://jitpack.io) into
your gradle project:

```grails
repositories {
...
maven { url "https://jitpack.io" }
}
```

Then, you can add EasEncrypt to your dependencies:

```grails
dependencies {
compile 'com.github.robinkanters:easencrypt:0.1'
}
```

Your final `build.gradle` file will look something like this:

```grails
group 'com.yourname'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
// your other dependencies here
compile 'com.github.robinkanters:easencrypt:0.1'
// your other dependencies here
}
```

## License

EasEncrypt is distributed under the GNU General Public License v2.0. Please see [the official
documentation](https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html) or the LICENSE file in
this repository for more information.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

public interface AsymmetricEncrypter {
@SuppressWarnings("WeakerAccess")
Expand All @@ -10,7 +12,7 @@ public interface AsymmetricEncrypter {
@SuppressWarnings("WeakerAccess")
KeyPair generateKeyPair(int keySize) throws NoSuchAlgorithmException;

byte[] encrypt(byte[] text, KeyPair keyPair);
byte[] encrypt(byte[] text, PublicKey keyPair);

byte[] decrypt(byte[] text, KeyPair keyPair);
byte[] decrypt(byte[] text, PrivateKey keyPair);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

public class LongTextEncrypterImpl implements LongTextEncrypter {
public static final String DIVIDER = "!!";

private final SymmetricalCipher symmetricalCipher;
private final RsaEncrypter asymmetricEncrypter;
private final AsymmetricEncrypter asymmetricEncrypter;

public LongTextEncrypterImpl(SymmetricalCipher symmetricalCipher) {
public LongTextEncrypterImpl(SymmetricalCipher symmetricalCipher,
AsymmetricEncrypter asymmetricEncrypter) {
this.symmetricalCipher = symmetricalCipher;
this.asymmetricEncrypter = new RsaEncrypter();
this.asymmetricEncrypter = asymmetricEncrypter;
}

public String encrypt(String plain, PublicKey publicKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public KeyPair generateKeyPair(int keySize) throws NoSuchAlgorithmException {
* :The public key
* @return Encrypted text
*/
@Override
public byte[] encrypt(byte[] text, KeyPair keyPair) {
return encrypt(text, keyPair.getPublic());
}
Expand All @@ -59,6 +58,7 @@ public byte[] encrypt(byte[] text, KeyPair keyPair) {
* :The public key
* @return Encrypted text
*/
@Override
public byte[] encrypt(byte[] text, PublicKey key) {
try {
return tryEncrypt(text, key);
Expand Down Expand Up @@ -86,7 +86,6 @@ private byte[] tryEncrypt(byte[] text, PublicKey key) throws NoSuchAlgorithmExce
* :The private key
* @return plain text
*/
@Override
public byte[] decrypt(byte[] text, KeyPair keyPair) {
return decrypt(text, keyPair.getPrivate());
}
Expand All @@ -100,6 +99,7 @@ public byte[] decrypt(byte[] text, KeyPair keyPair) {
* :The private key
* @return plain text
*/
@Override
public byte[] decrypt(byte[] text, PrivateKey key) {
try {
return tryDecrypt(text, key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void longText_ShouldThrowException() throws Exception {

private void assertEncryptDecryptReturnsInputString(String plainText) {
final AsymmetricEncrypter rsaEncrypter = new RsaEncrypter();
byte[] encrypted = rsaEncrypter.encrypt(plainText.getBytes(), keyPair);
byte[] decrypted = rsaEncrypter.decrypt(encrypted, keyPair);
byte[] encrypted = rsaEncrypter.encrypt(plainText.getBytes(), keyPair.getPublic());
byte[] decrypted = rsaEncrypter.decrypt(encrypted, keyPair.getPrivate());

assertTrue(encrypted.length > 0);
assertEquals(plainText, new String(decrypted));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@

public class LongTextEncrypterTest {
private KeyPair keyPair;
private AsymmetricEncrypter rsaEncrypter;

@Before
public void setUp() throws Exception {
final AsymmetricEncrypter rsaEncrypter = new RsaEncrypter();
rsaEncrypter = new RsaEncrypter();
keyPair = rsaEncrypter.generateKeyPair(512);
}

@Test
public void longText() throws Exception {
String plain = "Hello, world!";

LongTextEncrypter longTextEncrypter = new LongTextEncrypterImpl(new DesCipher());
LongTextEncrypter longTextEncrypter = new LongTextEncrypterImpl(new DesCipher(), rsaEncrypter);

String encrypted = longTextEncrypter.encrypt(plain, keyPair.getPublic());
assertNotEquals(plain, encrypted);
Expand All @@ -43,7 +44,7 @@ private void writeln(String... x) {
public void aes() throws Exception {
String plain = "Hello, world!";

LongTextEncrypter longTextEncrypter = new LongTextEncrypterImpl(new AesCipher());
LongTextEncrypter longTextEncrypter = new LongTextEncrypterImpl(new AesCipher(), rsaEncrypter);

String encrypted = longTextEncrypter.encrypt(plain, keyPair.getPublic());
assertNotEquals(plain, encrypted);
Expand Down

0 comments on commit 1755be8

Please sign in to comment.