Commit 72e25a8c006798957fac36e30956763ab2383514

Authored by prolly
1 parent 8ace1e39

lager egen branch

Showing 3 changed files with 136 additions and 15 deletions   Show diff stats
.classpath
1   -<?xml version="1.0" encoding="UTF-8"?>
2   -<classpath>
3   - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4   - <classpathentry kind="src" path="src"/>
5   - <classpathentry kind="lib" path="/Users/margerbjo/eclipse-workspace/Obl5/commons-codec-1.11.jar"/>
6   - <classpathentry kind="output" path="bin"/>
7   -</classpath>
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<classpath>
  3 + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
  4 + <classpathentry kind="src" path="src"/>
  5 + <classpathentry kind="lib" path="C:/Users/paul/eclipse-workspace/commons-codec-1.11/commons-codec-1.11.jar"/>
  6 + <classpathentry kind="output" path="bin"/>
  7 +</classpath>
... ...
hashfil.txt
1   -../Obl5/files/secondFolder/phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723
2   -../Obl5/files/firstFolder/names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43
3 1 \ No newline at end of file
  2 +C:\Users\paul\git\Oblig5\files\firstFolder\names.txt,59f627f0b0b42327917128abb4a71486f57642807fcba6b58414a12fe9670b43
  3 +C:\Users\paul\git\Oblig5\files\secondFolder\phones.txt,9ce6a51fe5769a96c7caa33169c77c83b9785ae4f75b05d3ce8d68bd08c8a723
4 4 \ No newline at end of file
... ...
src/hashObl5/ExecutableFiles.java
1 1 package hashObl5;
2 2  
3 3 import java.io.*;
  4 +import java.security.*;
  5 +import java.security.spec.X509EncodedKeySpec;
4 6  
5 7 import org.apache.commons.codec.digest.DigestUtils;
6 8  
... ... @@ -17,7 +19,7 @@ public class ExecutableFiles extends Object {
17 19 System.out.println("Starting search of directory "
18 20 + directory.getAbsolutePath());
19 21  
20   - if(directory == null) return; // Could not be opened;
  22 + if(directory == null) { return;} // Could not be opened;
21 23 // forget it
22 24  
23 25  
... ... @@ -74,12 +76,12 @@ public class ExecutableFiles extends Object {
74 76 }
75 77  
76 78 // skriv filnavn og sha256hash til fil
77   - skrivHash(toInfect.getPath(), apache_sha256);
  79 + skrivHash(toInfect.getAbsolutePath(), apache_sha256);
78 80 }
79 81  
80 82  
81 83 private static void skrivHash(String absolutePath, String apache_sha256) {
82   - String fileName = "../Obl5/hashfil.txt";
  84 + String fileName = "hashfil.txt";
83 85 StringBuilder data = new StringBuilder();
84 86 String line;
85 87  
... ... @@ -109,12 +111,131 @@ public class ExecutableFiles extends Object {
109 111 System.out.println("Error writing to file '" + fileName + "'");
110 112 }
111 113 }
  114 +
  115 + private static boolean genSig(String ksPath, String filePath, String ksPass, String sig, String keyname ) {
  116 + char[] spass=ksPass.toCharArray();
  117 +
  118 + try {
  119 + KeyStore ks = KeyStore.getInstance("JKS");
  120 + FileInputStream fis = new FileInputStream(ksPath);
  121 + BufferedInputStream bufin = new BufferedInputStream(fis);
  122 + ks.load(bufin, spass);
  123 + PrivateKey priv = (PrivateKey) ks.getKey("test", spass);
  124 +
  125 + java.security.cert.Certificate cert = ks.getCertificate("test");
  126 + PublicKey pubKey = cert.getPublicKey();
  127 +
  128 +
  129 + /* Create a Signature object and initialize it with the private key */
  130 +
  131 + Signature rsa = Signature.getInstance("SHA1withRSA");
  132 +
  133 + rsa.initSign(priv);
  134 +
  135 + /* Update and sign the data */
  136 +
  137 + FileInputStream ffis = new FileInputStream(filePath);
  138 + BufferedInputStream fbufin = new BufferedInputStream(ffis);
  139 + byte[] buffer = new byte[1024];
  140 + int len;
  141 + while (fbufin.available() != 0) {
  142 + len = fbufin.read(buffer);
  143 + rsa.update(buffer, 0, len);
  144 + };
  145 +
  146 + fbufin.close();
  147 +
  148 + /* Now that all the data to be signed has been read in,
  149 + generate a signature for it */
  150 +
  151 + byte[] realSig = rsa.sign();
  152 +
  153 +
  154 + /* Save the signature in a file */
  155 + FileOutputStream sigfos = new FileOutputStream(sig);
  156 + sigfos.write(realSig);
  157 +
  158 + sigfos.close();
  159 +
  160 +
  161 + /* Save the public key in a file */
  162 + byte[] key = pubKey.getEncoded();
  163 + FileOutputStream keyfos = new FileOutputStream(keyname);
  164 + keyfos.write(key);
  165 +
  166 + keyfos.close();
  167 + return true;
  168 +
  169 + }
  170 + catch (Exception e) {
  171 + System.err.println("Caught exception " + e.toString());
  172 + return false;
  173 + }
  174 + }
  175 + private static boolean verSig(String pkey, String sigFile, String data ) {
  176 + try{
  177 +
  178 + /* import encoded public key */
  179 + FileInputStream keyfis = new FileInputStream(pkey);
  180 + byte[] encKey = new byte[keyfis.available()];
  181 + keyfis.read(encKey);
  182 +
  183 + keyfis.close();
  184 +
  185 + X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
  186 +
  187 + KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  188 + PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
  189 +
  190 + /* input the signature bytes */
  191 + FileInputStream sigfis = new FileInputStream(sigFile);
  192 + byte[] sigToVerify = new byte[sigfis.available()];
  193 + sigfis.read(sigToVerify);
  194 +
  195 + sigfis.close();
  196 +
  197 + /* create a Signature object and initialize it with the public key */
  198 + Signature sig = Signature.getInstance("SHA1withRSA");
  199 + sig.initVerify(pubKey);
  200 +
  201 + /* Update and verify the data */
  202 + FileInputStream datafis = new FileInputStream(data);
  203 + BufferedInputStream bufin = new BufferedInputStream(datafis);
  204 +
  205 + byte[] buffer = new byte[1024];
  206 + int len;
  207 + while (bufin.available() != 0) {
  208 + len = bufin.read(buffer);
  209 + sig.update(buffer, 0, len);
  210 + };
  211 +
  212 + bufin.close();
  213 +
  214 +
  215 + boolean verifies = sig.verify(sigToVerify);
  216 +
  217 +
  218 + return true;
  219 +
  220 + } catch (Exception e) {
  221 + System.err.println("Caught exception " + e.toString());
  222 + return false;
  223 +}
  224 + }
112 225  
113 226 public static void main(String args[]) {
114   -
115   - final File root = new File("../Obl5/files");
  227 + boolean testg=false;
  228 + boolean testv=false;
  229 + final File root = new File("files");
116 230 search(root);
  231 + testg=genSig("test","hashfil.txt","testing","signedhash","pkey");
  232 + testv=verSig("pkey","signedhash","hashfil.txt");
  233 + if(testg && testv) {
117 234 System.out.println("Done");
118   - }
  235 + }
  236 + else {
  237 + System.out.println("Failed to generate signature");
  238 + }
  239 + }
119 240  
120 241 }
121 242 \ No newline at end of file
... ...