sombriks

Untitled

May 26th, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.74 KB | None | 0 0
  1. package testesignature;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.security.KeyFactory;
  10. import java.security.KeyPair;
  11. import java.security.KeyPairGenerator;
  12. import java.security.PrivateKey;
  13. import java.security.PublicKey;
  14. import java.security.SecureRandom;
  15. import java.security.Signature;
  16. import java.security.spec.PKCS8EncodedKeySpec;
  17. import java.security.spec.X509EncodedKeySpec;
  18.  
  19. import javax.crypto.Cipher;
  20. import javax.crypto.CipherInputStream;
  21. import javax.crypto.CipherOutputStream;
  22. import javax.crypto.KeyGenerator;
  23. import javax.crypto.SecretKey;
  24. import javax.crypto.spec.SecretKeySpec;
  25.  
  26. import org.apache.commons.io.IOUtils;
  27. import org.junit.Assert;
  28. import org.junit.Test;
  29.  
  30. public class TheTest {
  31.  
  32.     @Test
  33.     public void shouldCreateKeys() throws Exception {
  34.         KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
  35.         SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
  36.         keyGen.initialize(1024, random);
  37.         KeyPair pairSig = keyGen.generateKeyPair();
  38.         PrivateKey privSig = pairSig.getPrivate();
  39.         PublicKey pubSig = pairSig.getPublic();
  40.         Files.write(Paths.get("priv.key"), privSig.getEncoded());
  41.         Files.write(Paths.get("pub.key"), pubSig.getEncoded());
  42.  
  43.         KeyGenerator kgen = KeyGenerator.getInstance("AES");
  44.         kgen.init(128);
  45.         SecretKey key = kgen.generateKey();
  46.         Files.write(Paths.get("secret.key"), key.getEncoded());
  47.     }
  48.  
  49.     @Test
  50.     public void shouldSignImage() throws Exception {
  51.  
  52.         Path pPrivKey = Paths.get("priv.key");
  53.         byte[] bPriv = Files.readAllBytes(pPrivKey);
  54.         PKCS8EncodedKeySpec xPriv = new PKCS8EncodedKeySpec(bPriv);
  55.  
  56.         KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  57.         PrivateKey priv = keyFactory.generatePrivate(xPriv);
  58.  
  59.         Path pImg = Paths.get("19244.jpg");
  60.         byte[] bImg = Files.readAllBytes(pImg);
  61.  
  62.         Signature dsa = Signature.getInstance("SHA1withDSA");
  63.         dsa.initSign(priv);
  64.         dsa.update(bImg);
  65.         Path pSigImg = Paths.get("19244.jpg.sig");
  66.         Files.write(pSigImg, dsa.sign());
  67.  
  68.     }
  69.  
  70.     @Test
  71.     public void shouldVerifyImageSignature() throws Exception {
  72.  
  73.         Path pPubKey = Paths.get("pub.key");
  74.         byte[] bPub = Files.readAllBytes(pPubKey);
  75.         X509EncodedKeySpec xPub = new X509EncodedKeySpec(bPub);
  76.  
  77.         KeyFactory keyFactory = KeyFactory.getInstance("DSA");
  78.         PublicKey pub = keyFactory.generatePublic(xPub);
  79.  
  80.         Path pImg = Paths.get("19244.jpg");
  81.         byte[] bImg = Files.readAllBytes(pImg);
  82.  
  83.         Path pSigImg = Paths.get("19244.jpg.sig");
  84.  
  85.         Signature dsb = Signature.getInstance("SHA1withDSA");
  86.         dsb.initVerify(pub);
  87.         dsb.update(bImg);
  88.         Assert.assertTrue(dsb.verify(Files.readAllBytes(pSigImg)));
  89.     }
  90.  
  91.     @Test
  92.     public void shouldEncryptImage() throws Exception {
  93.  
  94.         Path pImg = Paths.get("19244.jpg");
  95.         byte[] bImg = Files.readAllBytes(pImg);
  96.  
  97.         Path pCrypImg = Paths.get("19244.jpg.cryp");
  98.         SecretKey key = new SecretKeySpec(Files.readAllBytes(Paths
  99.                 .get("secret.key")), "AES");
  100.  
  101.         Cipher cipher = Cipher.getInstance("AES");
  102.         cipher.init(Cipher.ENCRYPT_MODE, key);
  103.         try (OutputStream out = new CipherOutputStream(
  104.                 Files.newOutputStream(pCrypImg), cipher)) {
  105.             out.write(bImg);
  106.             out.flush();
  107.         }
  108.     }
  109.  
  110.     @Test
  111.     public void shouldDecryptImage() throws Exception {
  112.  
  113.         Path pCrypImg = Paths.get("19244.jpg.cryp");
  114.  
  115.         SecretKey key = new SecretKeySpec(Files.readAllBytes(Paths
  116.                 .get("secret.key")), "AES");
  117.  
  118.         Cipher cipher = Cipher.getInstance("AES");
  119.         cipher.init(Cipher.DECRYPT_MODE, key);
  120.  
  121.         try (InputStream in = new CipherInputStream(
  122.                 Files.newInputStream(pCrypImg), cipher)) {
  123.             try (OutputStream out = new FileOutputStream("19244-decryp.jpg")) {
  124.                 IOUtils.copy(in, out);
  125.             }
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment