Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testesignature;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.security.KeyFactory;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.security.SecureRandom;
- import java.security.Signature;
- import java.security.spec.PKCS8EncodedKeySpec;
- import java.security.spec.X509EncodedKeySpec;
- import javax.crypto.Cipher;
- import javax.crypto.CipherInputStream;
- import javax.crypto.CipherOutputStream;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import org.apache.commons.io.IOUtils;
- import org.junit.Assert;
- import org.junit.Test;
- public class TheTest {
- @Test
- public void shouldCreateKeys() throws Exception {
- KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
- SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
- keyGen.initialize(1024, random);
- KeyPair pairSig = keyGen.generateKeyPair();
- PrivateKey privSig = pairSig.getPrivate();
- PublicKey pubSig = pairSig.getPublic();
- Files.write(Paths.get("priv.key"), privSig.getEncoded());
- Files.write(Paths.get("pub.key"), pubSig.getEncoded());
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
- kgen.init(128);
- SecretKey key = kgen.generateKey();
- Files.write(Paths.get("secret.key"), key.getEncoded());
- }
- @Test
- public void shouldSignImage() throws Exception {
- Path pPrivKey = Paths.get("priv.key");
- byte[] bPriv = Files.readAllBytes(pPrivKey);
- PKCS8EncodedKeySpec xPriv = new PKCS8EncodedKeySpec(bPriv);
- KeyFactory keyFactory = KeyFactory.getInstance("DSA");
- PrivateKey priv = keyFactory.generatePrivate(xPriv);
- Path pImg = Paths.get("19244.jpg");
- byte[] bImg = Files.readAllBytes(pImg);
- Signature dsa = Signature.getInstance("SHA1withDSA");
- dsa.initSign(priv);
- dsa.update(bImg);
- Path pSigImg = Paths.get("19244.jpg.sig");
- Files.write(pSigImg, dsa.sign());
- }
- @Test
- public void shouldVerifyImageSignature() throws Exception {
- Path pPubKey = Paths.get("pub.key");
- byte[] bPub = Files.readAllBytes(pPubKey);
- X509EncodedKeySpec xPub = new X509EncodedKeySpec(bPub);
- KeyFactory keyFactory = KeyFactory.getInstance("DSA");
- PublicKey pub = keyFactory.generatePublic(xPub);
- Path pImg = Paths.get("19244.jpg");
- byte[] bImg = Files.readAllBytes(pImg);
- Path pSigImg = Paths.get("19244.jpg.sig");
- Signature dsb = Signature.getInstance("SHA1withDSA");
- dsb.initVerify(pub);
- dsb.update(bImg);
- Assert.assertTrue(dsb.verify(Files.readAllBytes(pSigImg)));
- }
- @Test
- public void shouldEncryptImage() throws Exception {
- Path pImg = Paths.get("19244.jpg");
- byte[] bImg = Files.readAllBytes(pImg);
- Path pCrypImg = Paths.get("19244.jpg.cryp");
- SecretKey key = new SecretKeySpec(Files.readAllBytes(Paths
- .get("secret.key")), "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- try (OutputStream out = new CipherOutputStream(
- Files.newOutputStream(pCrypImg), cipher)) {
- out.write(bImg);
- out.flush();
- }
- }
- @Test
- public void shouldDecryptImage() throws Exception {
- Path pCrypImg = Paths.get("19244.jpg.cryp");
- SecretKey key = new SecretKeySpec(Files.readAllBytes(Paths
- .get("secret.key")), "AES");
- Cipher cipher = Cipher.getInstance("AES");
- cipher.init(Cipher.DECRYPT_MODE, key);
- try (InputStream in = new CipherInputStream(
- Files.newInputStream(pCrypImg), cipher)) {
- try (OutputStream out = new FileOutputStream("19244-decryp.jpg")) {
- IOUtils.copy(in, out);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment