Advertisement
Alx09

testsss

Apr 13th, 2022
1,432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.85 KB | None | 0 0
  1. ///l5
  2. package lab5_r;
  3.  
  4. import java.math.BigInteger;
  5.  
  6. public class MainApp {
  7.  
  8.     public static void main(String[] args) {
  9.         // TODO Auto-generated method stub
  10.         //recursively searches for the sqr root of a in interval [left, right]
  11.         BigInteger n= new BigInteger("837210799");
  12.         BigInteger e= new BigInteger("7");
  13.         BigInteger d= new BigInteger("478341751");
  14.         BigInteger one= new BigInteger("1");
  15.         BigInteger two= new BigInteger("2");
  16.         BigInteger pplusq;
  17.         BigInteger s,k;
  18.         k=((d.multiply(e)).subtract(one)).divide(n);
  19.         k=k.add(one);
  20.         System.out.print(" k=" +k.toString());;
  21.         pplusq=(((k.multiply(n.add(one))).add(one)).subtract(e.multiply(d))).divide(k);
  22.         System.out.print(" p+q=" +pplusq.toString());;
  23.         BigInteger delta;
  24.         delta=(pplusq.pow(2)).subtract((k.multiply(one)).multiply(n));
  25.         System.out.print(" delta=" +delta.toString());;
  26.         BigInteger x1,x2;
  27.         x1=(pplusq.add(SquareRoot(delta))).divide(two);
  28.         x2=(pplusq.subtract(SquareRoot(delta))).divide(two);
  29.         System.out.print(" x1=" +x1.toString());;
  30.         System.out.print(" x2=" +x2.toString());;
  31.         //d=(x1.subtract(one)).multiply(x2.subtract(one));
  32.         e= new BigInteger("17");
  33.         BigInteger aux;
  34.         aux=(x1.subtract(one)).multiply(x2.subtract(one));
  35.         d=e.modInverse(aux);
  36.         System.out.println("pt e=17, d=" +d.toString());;
  37.        
  38.    
  39.     }
  40.     private static BigInteger NaiveSquareRootSearch(BigInteger a, BigInteger left,BigInteger right)
  41.      {
  42.      // fix root as the arithmetic mean of left and right
  43.      BigInteger root = left.add(right).shiftRight(1);
  44.      // if the root is not between [root, root+1],
  45.      //is not an integer and root is our best integer approximation
  46.     if(!((root.pow(2).compareTo(a) == -1)&&(root.add(BigInteger.ONE).pow(2).compareTo(a) == 1))){
  47.      if (root.pow(2).compareTo(a) == -1) root = NaiveSquareRootSearch(a, root, right);
  48.      if (root.pow(2).compareTo(a) == 1) root = NaiveSquareRootSearch(a, left, root);
  49.      }
  50.      return root;
  51.      }
  52.  
  53.     public static BigInteger SquareRoot(BigInteger a)
  54.     {
  55.      return NaiveSquareRootSearch(a, BigInteger.ZERO, a);
  56.     }
  57.  
  58. }
  59. ///////////////////////////////////////////l4
  60. using System;
  61. using System.Collections.Generic;
  62. using System.Linq;
  63. using System.Text;
  64. using System.Security.Cryptography;
  65.  
  66. namespace LAB4a
  67. {
  68.     class Program
  69.     {
  70.         static RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
  71.         static void Main(string[] args)
  72.         {
  73.             double[] results = new double[5];
  74.             byte[] plain = Encoding.ASCII.GetBytes("ftk");
  75.             int optiune;
  76.             do
  77.             {
  78.                 Console.WriteLine("1. 1024");
  79.                 Console.WriteLine("2. 2048");
  80.                 Console.WriteLine("3. 3072");
  81.                 Console.WriteLine("4. 4096");
  82.                 Console.WriteLine("5. exit");
  83.                 Console.Write("Introduceti optiunea :");
  84.                 optiune = Convert.ToInt16(Console.ReadLine());
  85.                 switch (optiune)
  86.                 {
  87.                     case 1:
  88.                         results = time_generation(1024, plain);
  89.                         Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
  90.                         Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
  91.                         Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
  92.                         Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
  93.                         Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
  94.                         Console.ReadKey();
  95.                         break;
  96.                     case 2:
  97.                         results = time_generation(2048, plain);
  98.                         Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
  99.                         Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
  100.                         Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
  101.                         Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
  102.                         Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
  103.                         Console.ReadKey();
  104.                         break;
  105.                     case 3:
  106.                         results = time_generation(3072, plain);
  107.                         Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
  108.                         Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
  109.                         Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
  110.                         Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
  111.                         Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
  112.                         Console.ReadKey();
  113.                         break;
  114.                     case 4:
  115.                         results = time_generation(4096, plain);
  116.                         Console.WriteLine("Generation time at 1024 bit ... " + results[0].ToString() + " ms");
  117.                         Console.WriteLine("Encryption time at 1024 bit ... " + results[1].ToString() + " ms");
  118.                         Console.WriteLine("Decryption time at 1024 bit ... " + results[2].ToString() + " ms");
  119.                         Console.WriteLine("Sign time at 1024 bit ... " + results[3].ToString() + " ms");
  120.                         Console.WriteLine("Verify time at 1024 bit ... " + results[4].ToString() + " ms");
  121.                         Console.ReadKey();
  122.                         break;
  123.                 }
  124.             } while (optiune != 5);
  125.         }
  126.  
  127.         public static double[] time_generation(int size, byte[] plain)
  128.         {
  129.             double[] results = new double[5];
  130.             long ms;
  131.             System.Diagnostics.Stopwatch swatch = new System.Diagnostics.Stopwatch();
  132.             int size1;
  133.             int count = 100;
  134.             byte[] ciphertext = myrsa.Encrypt(plain, true);
  135.             SHA256Managed myHash = new SHA256Managed();
  136.             byte[] signature;
  137.             bool verified;
  138.  
  139.             // generation time
  140.             swatch.Start();
  141.             for (int i = 0; i < count; i++)
  142.             {
  143.                 myrsa = new RSACryptoServiceProvider(size);
  144.                 size1 = myrsa.KeySize;
  145.             }
  146.             swatch.Stop();
  147.             ms = swatch.ElapsedMilliseconds;
  148.             results[0] = (double)((double)ms / count);
  149.  
  150.             // encrypt time
  151.             swatch.Reset();
  152.             swatch.Start();
  153.             for (int i = 0; i < count; i++)
  154.             {
  155.                 ciphertext = myrsa.Encrypt(plain, true);
  156.             }
  157.             swatch.Stop();
  158.             ms = swatch.ElapsedMilliseconds;
  159.             results[1] = (double)((double)ms / count);
  160.  
  161.             //decrypt time
  162.             swatch.Reset();
  163.             swatch.Start();
  164.             for (int i = 0; i < count; i++)
  165.             {
  166.                 plain = myrsa.Decrypt(ciphertext, true);
  167.             }
  168.             swatch.Stop();
  169.             ms = swatch.ElapsedMilliseconds;
  170.             results[2] = (double)((double)ms / count);
  171.  
  172.             //signature time
  173.             swatch.Reset();
  174.             swatch.Start();
  175.             for (int i = 0; i < count; i++)
  176.             {
  177.                 signature = myrsa.SignData(plain, myHash);
  178.             }
  179.             swatch.Stop();
  180.             ms = swatch.ElapsedMilliseconds;
  181.             results[3] = (double)((double)ms / count);
  182.  
  183.  
  184.             //verify time
  185.             signature = myrsa.SignData(plain, myHash);
  186.             swatch.Reset();
  187.             swatch.Start();
  188.             for (int i = 0; i < count; i++)
  189.             {
  190.                 verified = myrsa.VerifyData(plain, myHash, signature);
  191.             }
  192.             swatch.Stop();
  193.             ms = swatch.ElapsedMilliseconds;
  194.             results[4] = (double)((double)ms / count);
  195.             return results;
  196.         }
  197.     }
  198. }
  199.  
  200. //////////////////////////l6
  201.  
  202.  
  203. package com.mycompany.lab7_sss;
  204.  
  205.  
  206.  
  207. import java.security.InvalidAlgorithmParameterException;
  208. import java.security.InvalidKeyException;
  209. import java.security.Key;
  210. import java.security.KeyPair;
  211. import java.security.KeyPairGenerator;
  212. import java.security.NoSuchAlgorithmException;
  213. import java.security.SecureRandom;
  214. import java.security.spec.InvalidKeySpecException;
  215. import javax.crypto.BadPaddingException;
  216. import javax.crypto.Cipher;
  217. import javax.crypto.IllegalBlockSizeException;
  218. import javax.crypto.NoSuchPaddingException;
  219. import javax.crypto.SecretKey;
  220. import javax.crypto.SecretKeyFactory;
  221. import javax.crypto.ShortBufferException;
  222. import javax.crypto.spec.IvParameterSpec;
  223. import javax.crypto.spec.PBEKeySpec;
  224. import javax.crypto.spec.SecretKeySpec;
  225.  
  226. public class Lab7_SSS {
  227.  
  228.      public static void main(String[] args)
  229.             throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException {
  230.        
  231.         // declare secure PRNG
  232. SecureRandom myPRNG = new SecureRandom();
  233.         byte[] keyBytes = new byte[16];
  234.        char[] password = "short_password".toCharArray();
  235. byte[] salt = new byte[16];
  236. int iteration_count = 10000;
  237. int key_size = 128;//in biti, transformat din bytes, pentru DES avem 64, pentru 3DES 192
  238. // set salt values to random
  239.  
  240. myPRNG.nextBytes(salt);
  241.  
  242. // initialize key factory for HMAC-SHA1 derivation
  243. SecretKeyFactory keyFactory =
  244. SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  245. // set key specification
  246. PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,
  247. key_size);
  248. // generate the key
  249. SecretKey myAESPBKey = new SecretKeySpec(
  250. keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
  251. // print the key
  252. System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
  253.  
  254.  
  255. // seed the key
  256. //myPRNG.nextBytes(keyBytes);
  257. keyBytes=myAESPBKey.getEncoded();
  258. // build the key from random key bytes
  259. SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
  260. // instantiate AES object for ECB with no padding
  261. Cipher myAES = Cipher.getInstance("AES/CBC/PKCS5Padding");
  262. // initialize AES objecy to encrypt mode
  263. myAES.init(Cipher.ENCRYPT_MODE, myKey,new IvParameterSpec(new byte[16]));
  264. // initialize plaintext
  265. byte[] plaintext = new byte[16];
  266. plaintext= javax.xml.bind.DatatypeConverter.parseHexBinary("AABBCCDDAABBCCDDAABBCCDDAABBCCDD");
  267. //initialize ciphertext
  268.  
  269. byte[] ciphertext = new byte[32];
  270. // update cipher with the plaintext
  271. int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,
  272. 0);
  273. // process remaining blocks of plaintext
  274. cLength += myAES.doFinal(ciphertext, cLength);
  275. // print plaintext and ciphertext
  276. System.out.println("plaintext: " +
  277. javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
  278. System.out.println("ciphertext: " +
  279. javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
  280. // initialize AES for decryption
  281. myAES.init(Cipher.DECRYPT_MODE, myKey,new IvParameterSpec(new byte[16]));
  282. // initialize a new array of bytes to place the decryption
  283. byte[] dec_plaintext = new byte[16];
  284. cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,
  285. 0);
  286. // process remaining blocks of ciphertext
  287. cLength += myAES.doFinal(dec_plaintext, cLength);
  288. // print the new plaintext (hopefully identical to the initial one)
  289. System.out.println("decrypted: " +
  290. javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
  291.  
  292. //// get a Cipher instance for RSA with PKCS1 padding
  293. //Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  294. //// get an instance for the Key Generator
  295. //KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
  296. //// generate an 1024 bit key
  297. //myRSAKeyGen.initialize(1024, myPRNG);
  298. //KeyPair myRSAKeyPair= myRSAKeyGen.generateKeyPair();
  299. //// store the public and private key individually
  300. //Key pbKey = myRSAKeyPair.getPublic();
  301. //Key pvKey = myRSAKeyPair.getPrivate();
  302. //// init cipher for encryption
  303. //myRSA.init(Cipher.ENCRYPT_MODE, pbKey, myPRNG);
  304. //// encrypt, as expected we encrypt a symmetric key with RSA rather than
  305. ////a file or some longer stream which should be encrypted with AES
  306. //ciphertext = myRSA.doFinal(keyBytes);
  307. //// init cipher for decryption
  308. //myRSA.init(Cipher.DECRYPT_MODE, pvKey);
  309. //// decrypt
  310. //plaintext = myRSA.doFinal(ciphertext);
  311. //System.out.println("plaintext: " +
  312. //javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
  313. //System.out.println("ciphertext: " +
  314. //javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
  315. //System.out.println("keybytes: " +
  316. //javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
  317.      }
  318. }
  319.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement