Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<name>tcrypt</name>

<properties>
<app.main.class>ch.n1b.tcrypt.App</app.main.class>
<app.main.class>ch.n1b.tcrypt.AppDirectContent</app.main.class>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand All @@ -36,6 +36,12 @@

<dependencies>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
Expand Down Expand Up @@ -86,6 +92,23 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
59 changes: 59 additions & 0 deletions src/main/java/ch/n1b/tcrypt/AppDirectContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ch.n1b.tcrypt;

import ch.n1b.tcrypt.cryptor.AesGcmCryptor;
import org.apache.commons.codec.binary.Base64;

public final class AppDirectContent {

private static final String UNICODE_FORMAT = "UTF8";

private static final AesGcmCryptor CRYPTOR = new AesGcmCryptor();

public static void main(String[] args) {

if (args.length != 3) {
printUsage();
return;
}

final String mode = args[0];
final String password = args[1];
final String data = args[2];

if ("encode".equals(mode)) {
System.out.println(encrypt(password, data));
return;
}

if ("decode".equals(mode)) {
System.out.println(decrypt(password, data));
return;
}

printUsage();
}

private static String encrypt(final String password, final String unencryptedString) {
try {
byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
byte[] encryptedText = CRYPTOR.encrypt(password.toCharArray(), plainText);
return new String(Base64.encodeBase64(encryptedText));
} catch (final Exception e) {
throw new RuntimeException("Unable to encrypt!", e);
}
}

private static String decrypt(final String password, final String data) {
try {
byte[] encryptedText = CRYPTOR.decrypt(password.toCharArray(),Base64.decodeBase64(data));
return new String(encryptedText);
} catch (final Exception e) {
throw new RuntimeException("Unable to decrypt!", e);
}
}


private static void printUsage() {
System.out.printf("Usage: tcrypt ( encode | decode ) [master key / password] [data to encode/decode]%n");
}
}
2 changes: 1 addition & 1 deletion src/main/java/ch/n1b/tcrypt/cryptor/AesGcmCryptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public byte[] encrypt(char[] password, byte[] plaintext)
// initialise random and generate IV (initialisation vector)
SecretKey key = deriveAesKey(password, PBKDF2_SALT, AES_KEY_BITS_LENGTH);
final byte[] iv = new byte[GCM_IV_BYTES_LENGTH];
SecureRandom random = SecureRandom.getInstanceStrong();
SecureRandom random = new SecureRandom();
random.nextBytes(iv);

// encrypt
Expand Down