Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///l5
- package lab5_r;
- import java.math.BigInteger;
- public class MainApp {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //recursively searches for the sqr root of a in interval [left, right]
- BigInteger n= new BigInteger("837210799");
- BigInteger e= new BigInteger("7");
- BigInteger d= new BigInteger("478341751");
- BigInteger one= new BigInteger("1");
- BigInteger two= new BigInteger("2");
- BigInteger pplusq;
- BigInteger s,k;
- k=((d.multiply(e)).subtract(one)).divide(n);
- k=k.add(one);
- System.out.print(" k=" +k.toString());;
- pplusq=(((k.multiply(n.add(one))).add(one)).subtract(e.multiply(d))).divide(k);
- System.out.print(" p+q=" +pplusq.toString());;
- BigInteger delta;
- delta=(pplusq.pow(2)).subtract((k.multiply(one)).multiply(n));
- System.out.print(" delta=" +delta.toString());;
- BigInteger x1,x2;
- x1=(pplusq.add(SquareRoot(delta))).divide(two);
- x2=(pplusq.subtract(SquareRoot(delta))).divide(two);
- System.out.print(" x1=" +x1.toString());;
- System.out.print(" x2=" +x2.toString());;
- //d=(x1.subtract(one)).multiply(x2.subtract(one));
- e= new BigInteger("17");
- BigInteger aux;
- aux=(x1.subtract(one)).multiply(x2.subtract(one));
- d=e.modInverse(aux);
- System.out.println("pt e=17, d=" +d.toString());;
- }
- private static BigInteger NaiveSquareRootSearch(BigInteger a, BigInteger left,BigInteger right)
- {
- // fix root as the arithmetic mean of left and right
- BigInteger root = left.add(right).shiftRight(1);
- // if the root is not between [root, root+1],
- //is not an integer and root is our best integer approximation
- if(!((root.pow(2).compareTo(a) == -1)&&(root.add(BigInteger.ONE).pow(2).compareTo(a) == 1))){
- if (root.pow(2).compareTo(a) == -1) root = NaiveSquareRootSearch(a, root, right);
- if (root.pow(2).compareTo(a) == 1) root = NaiveSquareRootSearch(a, left, root);
- }
- return root;
- }
- public static BigInteger SquareRoot(BigInteger a)
- {
- return NaiveSquareRootSearch(a, BigInteger.ZERO, a);
- }
- }
- ///////////////////////////////////////////l4
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Security.Cryptography;
- namespace LAB4a
- {
- class Program
- {
- static RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
- static void Main(string[] args)
- {
- double[] results = new double[5];
- byte[] plain = Encoding.ASCII.GetBytes("ftk");
- int optiune;
- do
- {
- Console.WriteLine("1. 1024");
- Console.WriteLine("2. 2048");
- Console.WriteLine("3. 3072");
- Console.WriteLine("4. 4096");
- Console.WriteLine("5. exit");
- Console.Write("Introduceti optiunea :");
- optiune = Convert.ToInt16(Console.ReadLine());
- switch (optiune)
- {
- case 1:
- results = time_generation(1024, plain);
- Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
- Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
- Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
- Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
- Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
- Console.ReadKey();
- break;
- case 2:
- results = time_generation(2048, plain);
- Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
- Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
- Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
- Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
- Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
- Console.ReadKey();
- break;
- case 3:
- results = time_generation(3072, plain);
- Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
- Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
- Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
- Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
- Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
- Console.ReadKey();
- break;
- case 4:
- results = time_generation(4096, plain);
- Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
- Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
- Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
- Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
- Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
- Console.ReadKey();
- break;
- }
- } while (optiune != 5);
- }
- public static double[] time_generation(int size, byte[] plain)
- {
- double[] results = new double[5];
- long ms;
- System.Diagnostics.Stopwatch swatch = new System.Diagnostics.Stopwatch();
- int size1;
- int count = 100;
- byte[] ciphertext = myrsa.Encrypt(plain, true);
- SHA256Managed myHash = new SHA256Managed();
- byte[] signature;
- bool verified;
- // generation time
- swatch.Start();
- for (int i = 0; i < count; i++)
- {
- myrsa = new RSACryptoServiceProvider(size);
- size1 = myrsa.KeySize;
- }
- swatch.Stop();
- ms = swatch.ElapsedMilliseconds;
- results[0] = (double)((double)ms / count);
- // encrypt time
- swatch.Reset();
- swatch.Start();
- for (int i = 0; i < count; i++)
- {
- ciphertext = myrsa.Encrypt(plain, true);
- }
- swatch.Stop();
- ms = swatch.ElapsedMilliseconds;
- results[1] = (double)((double)ms / count);
- //decrypt time
- swatch.Reset();
- swatch.Start();
- for (int i = 0; i < count; i++)
- {
- plain = myrsa.Decrypt(ciphertext, true);
- }
- swatch.Stop();
- ms = swatch.ElapsedMilliseconds;
- results[2] = (double)((double)ms / count);
- //signature time
- swatch.Reset();
- swatch.Start();
- for (int i = 0; i < count; i++)
- {
- signature = myrsa.SignData(plain, myHash);
- }
- swatch.Stop();
- ms = swatch.ElapsedMilliseconds;
- results[3] = (double)((double)ms / count);
- //verify time
- signature = myrsa.SignData(plain, myHash);
- swatch.Reset();
- swatch.Start();
- for (int i = 0; i < count; i++)
- {
- verified = myrsa.VerifyData(plain, myHash, signature);
- }
- swatch.Stop();
- ms = swatch.ElapsedMilliseconds;
- results[4] = (double)((double)ms / count);
- return results;
- }
- }
- }
- //////////////////////////l6
- package com.mycompany.lab7_sss;
- import java.security.InvalidAlgorithmParameterException;
- import java.security.InvalidKeyException;
- import java.security.Key;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.NoSuchAlgorithmException;
- import java.security.SecureRandom;
- import java.security.spec.InvalidKeySpecException;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.ShortBufferException;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.PBEKeySpec;
- import javax.crypto.spec.SecretKeySpec;
- public class Lab7_SSS {
- public static void main(String[] args)
- throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException {
- // declare secure PRNG
- SecureRandom myPRNG = new SecureRandom();
- byte[] keyBytes = new byte[16];
- char[] password = "short_password".toCharArray();
- byte[] salt = new byte[16];
- int iteration_count = 10000;
- int key_size = 128;//in biti, transformat din bytes, pentru DES avem 64, pentru 3DES 192
- // set salt values to random
- myPRNG.nextBytes(salt);
- // initialize key factory for HMAC-SHA1 derivation
- SecretKeyFactory keyFactory =
- SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- // set key specification
- PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,
- key_size);
- // generate the key
- SecretKey myAESPBKey = new SecretKeySpec(
- keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
- // print the key
- System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
- // seed the key
- //myPRNG.nextBytes(keyBytes);
- keyBytes=myAESPBKey.getEncoded();
- // build the key from random key bytes
- SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
- // instantiate AES object for ECB with no padding
- Cipher myAES = Cipher.getInstance("AES/CBC/PKCS5Padding");
- // initialize AES objecy to encrypt mode
- myAES.init(Cipher.ENCRYPT_MODE, myKey,new IvParameterSpec(new byte[16]));
- // initialize plaintext
- byte[] plaintext = new byte[16];
- plaintext= javax.xml.bind.DatatypeConverter.parseHexBinary("AABBCCDDAABBCCDDAABBCCDDAABBCCDD");
- //initialize ciphertext
- byte[] ciphertext = new byte[32];
- // update cipher with the plaintext
- int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,
- 0);
- // process remaining blocks of plaintext
- cLength += myAES.doFinal(ciphertext, cLength);
- // print plaintext and ciphertext
- System.out.println("plaintext: " +
- javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
- System.out.println("ciphertext: " +
- javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
- // initialize AES for decryption
- myAES.init(Cipher.DECRYPT_MODE, myKey,new IvParameterSpec(new byte[16]));
- // initialize a new array of bytes to place the decryption
- byte[] dec_plaintext = new byte[16];
- cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,
- 0);
- // process remaining blocks of ciphertext
- cLength += myAES.doFinal(dec_plaintext, cLength);
- // print the new plaintext (hopefully identical to the initial one)
- System.out.println("decrypted: " +
- javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
- //// get a Cipher instance for RSA with PKCS1 padding
- //Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
- //// get an instance for the Key Generator
- //KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
- //// generate an 1024 bit key
- //myRSAKeyGen.initialize(1024, myPRNG);
- //KeyPair myRSAKeyPair= myRSAKeyGen.generateKeyPair();
- //// store the public and private key individually
- //Key pbKey = myRSAKeyPair.getPublic();
- //Key pvKey = myRSAKeyPair.getPrivate();
- //// init cipher for encryption
- //myRSA.init(Cipher.ENCRYPT_MODE, pbKey, myPRNG);
- //// encrypt, as expected we encrypt a symmetric key with RSA rather than
- ////a file or some longer stream which should be encrypted with AES
- //ciphertext = myRSA.doFinal(keyBytes);
- //// init cipher for decryption
- //myRSA.init(Cipher.DECRYPT_MODE, pvKey);
- //// decrypt
- //plaintext = myRSA.doFinal(ciphertext);
- //System.out.println("plaintext: " +
- //javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
- //System.out.println("ciphertext: " +
- //javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
- //System.out.println("keybytes: " +
- //javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement