Commit 323b1bbab4a844c5a5feb41adc5a4f3a921a3fd5

Authored by Georgii Bogdanov
1 parent 3cefb5e5

laget verifikasjon av digital signatur metode (VerSig)

Showing 1 changed file with 57 additions and 4 deletions   Show diff stats
Oblig5/src/FileHashing.java
... ... @@ -189,14 +189,15 @@ infect(entry); // If executable, infect it
189 189  
190 190 }
191 191  
192   - }
  192 + }
  193 +
193 194  
194 195 public static void signereHashFil() {
195 196 try{
196 197 String ksName = "C:\\Program Files\\Java\\jdk1.8.0_144\\bin\\keystore";
197 198 char[] spass = {'1','Q', 'u','e', 's','t', '#', 'Q', 'u','e', 's','t', '5','1'};
198 199 File hashfile = new File("hashfile.txt");
199   - // Åpne keystore
  200 + // Г…pne keystore
200 201 KeyStore ks = KeyStore.getInstance("JKS");
201 202 FileInputStream ksfis = new FileInputStream(ksName);
202 203 BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
... ... @@ -242,13 +243,65 @@ infect(entry); // If executable, infect it
242 243 /* byte[] key = pub.getEncoded();
243 244 FileOutputStream keyfos = new FileOutputStream("suepk");
244 245 keyfos.write(key);
245   - System.out.println("Public nøkkel ble lagret");
  246 + System.out.println("Public nГёkkel ble lagret");
246 247 keyfos.close();*/
247 248 } catch (Exception e) {
248 249 System.err.println("Caught exception " + e.toString());
249 250 }
250 251 }
251   -
  252 + public static void VerSig() {
  253 +
  254 + /* Verify a DSA signature */
  255 +
  256 + try {
  257 +
  258 + String ksName = "C:\\Program Files\\Java\\jdk1.8.0_162\\bin\\keystore";
  259 + char[] spass = { '3', '1', '1', '0', '8', '0', 'B', 'e', 'l' };
  260 + // Åpne keystore
  261 + KeyStore ks = KeyStore.getInstance("JKS");
  262 + FileInputStream ksfis = new FileInputStream(ksName);
  263 + BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
  264 + ks.load(ksbufin, spass);
  265 +
  266 + // Hent ut sertifikat og offentlig nøkkel
  267 + java.security.cert.Certificate cert = ks.getCertificate("alex");
  268 + PublicKey pubKey = cert.getPublicKey();
  269 +
  270 + /* input the signature bytes */
  271 + FileInputStream sigfis = new FileInputStream("sig");
  272 + byte[] sigToVerify = new byte[sigfis.available()];
  273 + sigfis.read(sigToVerify);
  274 +
  275 + sigfis.close();
  276 +
  277 + /* create a Signature object and initialize it with the public key */
  278 + Signature sig = Signature.getInstance("SHA1withRSA");
  279 + sig.initVerify(pubKey);
  280 +
  281 + /* Update and verify the data */
  282 +
  283 + FileInputStream datafis = new FileInputStream(fileName);
  284 + BufferedInputStream bufin = new BufferedInputStream(datafis);
  285 +
  286 + byte[] buffer = new byte[1024];
  287 + int len;
  288 + while (bufin.available() != 0) {
  289 + len = bufin.read(buffer);
  290 + sig.update(buffer, 0, len);
  291 + }
  292 + ;
  293 +
  294 + bufin.close();
  295 +
  296 + boolean verifies = sig.verify(sigToVerify);
  297 +
  298 + System.out.println("signature verifies: " + verifies);
  299 +
  300 + } catch (Exception e) {
  301 + System.err.println("Caught exception " + e.toString());
  302 + }
  303 +
  304 + }
252 305  
253 306  
254 307 }
... ...