Commit f5aa725531e16f2a6df0d47a05e177fb88b101ed

Authored by Georgii Bogdanov
0 parents

har laget koden for punkter 1 og 2 (search, infect, skrivHash,

signereHashFil metoder)
Showing 1 changed file with 190 additions and 0 deletions   Show diff stats
Oblig5/src/FileHashing.java 0 → 100644
  1 +++ a/Oblig5/src/FileHashing.java
  1 +import java.io.BufferedInputStream;
  2 +import java.io.DataOutputStream;
  3 +import java.io.File;
  4 +import java.io.FileInputStream;
  5 +import java.io.FileNotFoundException;
  6 +import java.io.FileOutputStream;
  7 +import java.io.FileWriter;
  8 +import java.io.IOException;
  9 +import java.io.PrintWriter;
  10 +import java.security.InvalidKeyException;
  11 +import java.security.KeyStore;
  12 +import java.security.KeyStoreException;
  13 +import java.security.NoSuchAlgorithmException;
  14 +import java.security.PrivateKey;
  15 +import java.security.PublicKey;
  16 +import java.security.Signature;
  17 +import java.security.SignatureException;
  18 +import java.security.UnrecoverableKeyException;
  19 +import java.security.cert.CertificateException;
  20 +
  21 +import org.apache.commons.codec.digest.DigestUtils;
  22 +
  23 +public class FileHashing {
  24 +
  25 + public static void search(File directory) throws FileNotFoundException{ // Recursively search for
  26 + // executables
  27 +
  28 +File entry; // A reference to an entry
  29 + // in the directory
  30 +String entryName; // The full path name of a
  31 + // file
  32 +
  33 +System.out.println("Starting search of directory "
  34 ++ directory.getAbsolutePath());
  35 +
  36 +if(directory == null) return; // Could not be opened;
  37 + // forget it
  38 +
  39 +
  40 +String contents[] = directory.list(); // Get an array of all the
  41 + // files in the directory
  42 +if(contents == null) return; // Could not access
  43 + // contents, skip it
  44 +
  45 +for(int i=0; i<contents.length; i++){ // Deal with each file
  46 +entry = new File(directory, contents[i]); // Read next directory
  47 + // entry
  48 +
  49 +if(contents[i].charAt(0) == '.') // Skip the . and ..
  50 + // directories
  51 +continue;
  52 +if (entry.isDirectory()){ // Is it a directory
  53 +search(entry); // Yes, enter and search it
  54 +} else { // No (file)
  55 +if(executable(entry))
  56 +infect(entry); // If executable, infect it
  57 +}
  58 +}
  59 +}
  60 +
  61 + public static boolean executable(File toCheck){
  62 + String fileName = toCheck.getName();
  63 +
  64 + if(! (toCheck.canWrite() && toCheck.canRead()))
  65 + return false; // Ignore if we can't
  66 + // read and write it
  67 +
  68 + if( fileName.indexOf(".class") != -1)
  69 + return true;
  70 +
  71 + if( fileName.indexOf(".txt") != -1)
  72 + return true; // Found a java
  73 + // executable
  74 +
  75 + if( fileName.indexOf(".jar") != -1)
  76 + return true; // Found a java
  77 + // executable
  78 +
  79 + return false;
  80 +
  81 + }
  82 +
  83 + public static void infect(File toInfect) throws FileNotFoundException{
  84 + String apache_sha256="";
  85 + try {
  86 + FileInputStream in = new FileInputStream(toInfect);
  87 + apache_sha256 = DigestUtils.sha256Hex(in);
  88 + System.out.println("Infecting file " + toInfect.getAbsolutePath());
  89 + System.out.println("sha256 hash er: " + apache_sha256 );
  90 + skrivHash(toInfect.getAbsolutePath(), apache_sha256);
  91 + }
  92 + catch (FileNotFoundException ex) {
  93 + }
  94 + catch (IOException ex) {
  95 + }
  96 +
  97 + // skriv filnavn og sha256hash til fil
  98 +
  99 + }
  100 +
  101 + public static void skrivHash(String filepath, String hashverdien) throws FileNotFoundException {
  102 + try
  103 + {
  104 + File hashfile = new File("hashfile.txt");
  105 + FileWriter out = new FileWriter(hashfile, true);
  106 +
  107 + out.write(filepath+",");
  108 + out.write(hashverdien);
  109 + out.write(System.getProperty( "line.separator" ));
  110 +
  111 + //out.flush();
  112 + out.close();
  113 + System.out.println("Fil sti og hashverdien ble skrevet til fil "+hashfile);
  114 + }
  115 + catch(IOException ioe)
  116 + {
  117 +
  118 +
  119 +
  120 + }
  121 +
  122 + }
  123 +
  124 + public static void signereHashFil() {
  125 + try{
  126 + String ksName = "C:\\Program Files\\Java\\jdk1.8.0_144\\bin\\keystore";
  127 + char[] spass = {'1','Q', 'u','e', 's','t', '#', 'Q', 'u','e', 's','t', '5','1'};
  128 + File hashfile = new File("hashfile.txt");
  129 + // Åpne keystore
  130 + KeyStore ks = KeyStore.getInstance("JKS");
  131 + FileInputStream ksfis = new FileInputStream(ksName);
  132 + BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
  133 + ks.load(ksbufin, spass);
  134 + // Hent ut privat nokkel
  135 + PrivateKey priv = (PrivateKey) ks.getKey("gosha", spass);
  136 +
  137 + // Hent ut sertifikat og offentlig nokkel
  138 + //java.security.cert.Certificate cert = ks.getCertificate("gosha");
  139 + //PublicKey pub = cert.getPublicKey();
  140 +
  141 + /* Create a Signature object and initialize it with the private key */
  142 +
  143 + Signature dsa = Signature.getInstance("SHA1withRSA");
  144 +
  145 + dsa.initSign(priv);
  146 +
  147 + /* Update and sign the data */
  148 +
  149 + FileInputStream fis = new FileInputStream(hashfile);
  150 + BufferedInputStream bufin = new BufferedInputStream(fis);
  151 + byte[] buffer = new byte[1024];
  152 + int len;
  153 + while (bufin.available() != 0) {
  154 + len = bufin.read(buffer);
  155 + dsa.update(buffer, 0, len);
  156 + };
  157 +
  158 + bufin.close();
  159 +
  160 + /* Now that all the data to be signed has been read in,
  161 + generate a signature for it */
  162 +
  163 + byte[] realSig = dsa.sign();
  164 +
  165 + /* Save the signature in a file */
  166 + FileOutputStream sigfos = new FileOutputStream("sig");
  167 + sigfos.write(realSig);
  168 + System.out.println("Dokumentet ble signert");
  169 + sigfos.close();
  170 +
  171 + /* Save the public key in a file */
  172 + /* byte[] key = pub.getEncoded();
  173 + FileOutputStream keyfos = new FileOutputStream("suepk");
  174 + keyfos.write(key);
  175 + System.out.println("Public nøkkel ble lagret");
  176 + keyfos.close();*/
  177 + } catch (Exception e) {
  178 + System.err.println("Caught exception " + e.toString());
  179 + }
  180 + }
  181 +
  182 + public static void main(String args[]) throws FileNotFoundException{
  183 + File root;
  184 + root = new File("t");
  185 + search(root);
  186 + signereHashFil();
  187 + System.out.println("Done");
  188 + }
  189 +
  190 +}
... ...