<file_sep>import org.junit.Test;
import sec.AESHelper;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static sec.CipherHelper.getPasswordKey;

public class TestAES {
    public static final String AES_TRANS = "AES/CBC/PKCS5Padding";
    public static final String AES_ALGO = "AES";
    public static final byte[] AES_SALT = new byte[]{-70, 93, -16, 40, -72, -27, -109, -78};
    public static final int AES_ITERATIONS = 704272;
    public static final int AES_KEY_LENGTH = 128;

    @Test
    public void testGetKeyvalidinput() throws Exception {
        char[] pass = {'n', 'i', 'k', 'h', 'i', 't', 'a'};
        final SecretKey key = getPasswordKey( pass, AES_SALT, AES_ITERATIONS, AES_KEY_LENGTH );
        SecretKey expectedKey = new SecretKeySpec( key.getEncoded(), AES_ALGO );
        AESHelper aeshelper = new AESHelper();
        assertEquals( expectedKey, aeshelper.getKey( pass ) );
        assertNotNull( aeshelper.getKey( pass ) );
    }

    @Test
    public void testgetEncryptcipher()throws Exception {
        Cipher cipher = Cipher.getInstance(AES_TRANS);
        // creating a fake output stream
        String output_File = "C:/Users/nivesh/TestFile.txt";
        FileOutputStream fos = new FileOutputStream(output_File);
        //creating a new secret key
        String str = "nikhita";
        char[] charArray = str.toCharArray();
        AESHelper mockAeshelper = new AESHelper();
        SecretKey key = mockAeshelper.getKey(charArray);
        System.out.println(mockAeshelper.getEncryptCipher(fos,key));
        assertNotNull(mockAeshelper.getEncryptCipher(fos,key));
    }
   @Test
    public void testgetDecryptcipher()throws Exception{
       //creating a fake input stream
       String Input_File = "C:/Users/nivesh/TestFile.txt";
       FileInputStream fis = new FileInputStream(Input_File);
       //creating a secret key
       String str = "nikhita";
       char[] charArray = str.toCharArray();
       AESHelper mockAeshelper = new AESHelper();
       SecretKey key = mockAeshelper.getKey(charArray);
       System.out.println( mockAeshelper.getDecryptCipher(fis,key));
       assertNotNull( mockAeshelper.getDecryptCipher(fis,key) );
    }
}<file_sep>package sec;

import java.io.*;
import java.security.SecureRandom;

import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;

public abstract class CipherHelper {
	
	public static final SecureRandom RANDOM = new SecureRandom();
	public static final byte AES_MAGIC = 0;
	public static final byte DES_MAGIC = 1;
	public static final byte BF_MAGIC = 2;
	public static final DESHelper DES = new DESHelper();
	public static final AESHelper AES = new AESHelper();
	public static final BlowfishHelper BF = new BlowfishHelper();
	public static final int PAD = 23;
	
	public static CipherHelper getHelper (byte magic) {
		switch (magic) {
			case AES_MAGIC:
				return AES;
			case DES_MAGIC:
				return DES;
			case BF_MAGIC:
				return BF;
			default:
				throw new RuntimeException("unknown magic " + magic);
		}
	}
	
	public static SecretKey getPasswordKey (char[] pass, byte[] salt, int iterations, int length) throws Exception {
		SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
		PBEKeySpec keySpec = new PBEKeySpec(pass, salt, iterations, length);
		long t = System.nanoTime();
		SecretKey key = factory.generateSecret(keySpec);
		t = System.nanoTime() - t;
		System.out.println("password key iteration time " + (t / 1000000000.0));
		return key;
	}

	public static void pad (final OutputStream os) throws IOException {
		byte[] a = new byte[PAD];
		CipherHelper.RANDOM.nextBytes(a);
		for (int n = 0; n < a.length; n++) {
			os.write(a[n]);
		}
	}
	
	public static void unpad (final InputStream is) throws IOException {
		for (int n = 0; n < PAD; n++) {
			is.read();
		}
	}
	
	public abstract byte getMagic ();
	
	public abstract SecretKey getKey (char[] pass) throws Exception;
	
	public abstract Cipher getEncryptCipher (OutputStream os, SecretKey key) throws Exception;
	
	public abstract Cipher getDecryptCipher (InputStream is, SecretKey key) throws Exception;
}
<file_sep>package sec;

import java.io.File;

import javax.swing.filechooser.FileFilter;

class SecretFileFilter extends FileFilter {
	public final String description;
	public final CipherHelper helper;
	
	public SecretFileFilter (CipherHelper helper, String description) {
		this.helper = helper;
		this.description = description;
	}
	
	@Override
	public String getDescription () {
		return description;
	}
	
	@Override
	public boolean accept (File f) {
		return f.isDirectory() || f.getName().toLowerCase().endsWith(".sec");
	}
}<file_sep><?xml version="1.0" encoding="UTF-8"?>
<project default="jar">

	<property name="jarfile" value="secrets.jar" />

	<target name="jar">
		<jar destfile="dist/${jarfile}">
			<manifest>
				<attribute name="Main-Class" value="sec.SecretsJFrame" />
			</manifest>
			<fileset dir="bin" />
			<!-- run mvn dependency:copy-dependencies first... -->
			<zipgroupfileset dir="target/dependency" includes="**/*.jar" />
		</jar>
	</target>

	<target name="deploy" depends="jar">
		<copy file="dist/${jarfile}" tofile="${user.home}/Dropbox/Public/${jarfile}" />
	</target>

</project>
<file_sep>package sec;

import java.io.*;
import java.util.Properties;

public class Secret {
	public static final byte TEXT_TYPE = 0;
	public static final byte RTF_TYPE = 1;
	public static final byte FILE_TYPE = 2;
	
	public static final byte KEY_INDEX = 0;
	public static final byte VALUE_INDEX = 1;
	public static final byte TYPE_INDEX = 2;
	public static final byte PROPERTIES_INDEX = 3;
	public static final byte INDEXES = 4;
	
	public String name;
	public byte[] type;
	public byte[] value;
	public byte[] properties;
	public boolean modified;
	
	public Secret (String name) {
		this(name, null, null);
	}
	
	public Secret (String name, byte[] type, byte[] value) {
		this.name = name;
		this.type = type;
		this.value = value;
	}
	
	public Properties getProperties() {
		Properties p = new Properties();
		if (properties != null && properties.length > 0) {
			try (ByteArrayInputStream bis = new ByteArrayInputStream(properties)) {
				p.load(bis);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return p;
	}
	
	public void setProperties(Properties p) {
		if (p != null && p.size() > 0) {
			try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
				p.store(bos, null);
				properties = bos.toByteArray();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			properties = null;
		}
	}
	
	@Override
	public String toString () {
		return name + (modified ? "*": "");
	}
	
}
<file_sep>apply plugin: 'java'
apply plugin: 'maven'

group = 'com.github.alexyz'
version = '0.0.1-SNAPSHOT'

description = """secret"""

sourceCompatibility = 1.7
targetCompatibility = 1.7

task runjar(dependsOn:jar) << {
    javaexec {
        main="-jar"; args "./lib/secrets.jar"
    }
}
task execute(type:JavaExec) {
    main = "sec.SecretsJFrame"
    classpath = sourceSets.main.runtimeClasspath
}
sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
    test {
        java {
            srcDir 'tests'
        }
    }
}
repositories {
        
     maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
    compile group: 'org.apache.commons', name: 'commons-lang3', version:'LATEST'
    compile group: 'commons-io', name: 'commons-io', version:'2.2'
    compile group: 'junit', name: 'junit', version:'4.11'
    compile group: 'org.apache.maven.surefire', name: 'maven-surefire-common', version:'2.18.1'
}
<file_sep>                         
                         SECRETS PROJECT
                                                                                                                                              
                                                                                                                  
#INTRODUCTION:
The ‘secret’ is a maven configuration open source project cloned form the Git repository. This secret application can be used to create new text files of type AES-128 and save them with a password which keeps your information as a secret.
#PRE-REQUISITES:
IntelliJ IDEA, java SE, GIT, Gradle , SBT, Scala.
About My Project:
#INTELLIJ IDEA SETUP AND PROJECT BUILD: 
I downloaded java SE from oracle site and created JAVA_HOME, JDK HOME, and I setup the path in environmental variables.
Then i downloaded IntelliJ idea 2017.2.3 community edition and installed it in my PC.
 I cloned an open source project and created a new project in my intelliJ IDEA.I added some jar files like org.apache.commons: commons-lang3:3.6, commons-io: commons-io: 2.5 to resolve the issues with dependencies. 
I imported plug-in required to run the project and verified that all the lifecycle options in my maven project are built successfully. 
Then I built the project using build artifact option which creates a jar.
#JUNIT TESTS:
To create Junit tests for my project, I downloaded Junit jars and added that path as Junit_Home to the environmental variables.
I tried to understand the whole project structure of my project and wrote junit tests using assert statements to test if my project is correct and created a folder named tests which contains all my tests. I ran all the tests that I have created and they ran successfully.
#GRADLE SCRIPT:
I installed Gradle 2.8 in my PC and set its GRADLE_HOME path in environmental variables. 
To build the project Gradle imported plug-in and libraries required. 
Then I started writing the gradle script in build.gradle file after understanding the pom.xml file and build.xml file in my maven project.
 I created a lib folder in the project and added my secrets.jar file to it.
 I wrote a task called task runjar in my build.gradle script which runs the jar in my lib folder.
 I wrote a task called task execute which will execute my project by accessing my main class. 
#Commands used to access gradle: 
‘gradle build’ to build the project.
‘gradle runjar’ to run the jar file that I defined in the build.gradle script.
‘gradle execute’ to run the project using main class that I defined in build.gradle script.
‘gradle test’ to test the Junit tests in the project.
#SBT SCRIPT:
I installed sbt and scala in my pc, set their paths in environmental variables.
Then i imported my project as a sbt project.
Then it generated a build.sbt file in my project. 
I wrote the sbt script adding all the dependencies and wrote a task to access my main class. 
Then i built and ran it.
#Commands used to access Sbt:
‘sbt compile’ to compile the project.
‘sbt run’ to run the project.
‘sbt test’ to build the tests.


#JAVA MONITORING TOOLS:
I used jconsole, visualvm, and java mission control tools to monitor the project.

#JCONSOLE: 
The jconsole executable can be found in java - > jdk -> bin folder.
Then to check if it is in my system i used ‘jconsole’ command. 
To access a particular application that is running, we need to know the PID (process id). To know it, i opened my task manager and searched for IJ idea PID and gave a command ‘jconsole pid’. 
Then it opened and I checked all the options that it provides.
Below are the snapshots of the various tasks that it performs.

#VISUAL  VM:
The visualvm executable can be found in java - > jdk -> bin folder.
You can use ‘jvisualvm’ command and access it.
In the menu panel i selected the application that I want to monitor that is Idea and observed these results which I have attached in the below snapshots

#Java Mission Control: 
I started the jmc executable from the JDK bin directory. 
Under JVM Browser on the left pane, I have selected the MBean Server. 
Once loaded, it will show a cool dashboard of the JVM CPU/memory usage. I have attached a snapshot of the same.


<file_sep>rootProject.name = 'secret'
<file_sep>package sec;

import java.security.SecureRandom;
import java.util.Arrays;

public class salt {
	
	public static void main (String[] args) {
		SecureRandom r = new SecureRandom();
		byte[] s = new byte[8];
		r.nextBytes(s);
		System.out.println(Arrays.toString(s));
		System.out.println(500000 + r.nextInt(500000));
	}
	
}
