· 3 years ago · Apr 13, 2022, 06:20 AM
1///l5
2package lab5_r;
3
4import java.math.BigInteger;
5
6public 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
60using System;
61using System.Collections.Generic;
62using System.Linq;
63using System.Text;
64using System.Security.Cryptography;
65
66namespace 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
203package com.mycompany.lab7_sss;
204
205
206
207import java.security.InvalidAlgorithmParameterException;
208import java.security.InvalidKeyException;
209import java.security.Key;
210import java.security.KeyPair;
211import java.security.KeyPairGenerator;
212import java.security.NoSuchAlgorithmException;
213import java.security.SecureRandom;
214import java.security.spec.InvalidKeySpecException;
215import javax.crypto.BadPaddingException;
216import javax.crypto.Cipher;
217import javax.crypto.IllegalBlockSizeException;
218import javax.crypto.NoSuchPaddingException;
219import javax.crypto.SecretKey;
220import javax.crypto.SecretKeyFactory;
221import javax.crypto.ShortBufferException;
222import javax.crypto.spec.IvParameterSpec;
223import javax.crypto.spec.PBEKeySpec;
224import javax.crypto.spec.SecretKeySpec;
225
226public 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
232SecureRandom myPRNG = new SecureRandom();
233 byte[] keyBytes = new byte[16];
234 char[] password = "short_password".toCharArray();
235byte[] salt = new byte[16];
236int iteration_count = 10000;
237int key_size = 128;//in biti, transformat din bytes, pentru DES avem 64, pentru 3DES 192
238// set salt values to random
239
240myPRNG.nextBytes(salt);
241
242// initialize key factory for HMAC-SHA1 derivation
243SecretKeyFactory keyFactory =
244SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
245// set key specification
246PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,
247key_size);
248// generate the key
249SecretKey myAESPBKey = new SecretKeySpec(
250keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
251// print the key
252System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
253
254
255// seed the key
256//myPRNG.nextBytes(keyBytes);
257keyBytes=myAESPBKey.getEncoded();
258// build the key from random key bytes
259SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
260// instantiate AES object for ECB with no padding
261Cipher myAES = Cipher.getInstance("AES/CBC/PKCS5Padding");
262// initialize AES objecy to encrypt mode
263myAES.init(Cipher.ENCRYPT_MODE, myKey,new IvParameterSpec(new byte[16]));
264// initialize plaintext
265byte[] plaintext = new byte[16];
266plaintext= javax.xml.bind.DatatypeConverter.parseHexBinary("AABBCCDDAABBCCDDAABBCCDDAABBCCDD");
267//initialize ciphertext
268
269byte[] ciphertext = new byte[32];
270// update cipher with the plaintext
271int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,
2720);
273// process remaining blocks of plaintext
274cLength += myAES.doFinal(ciphertext, cLength);
275// print plaintext and ciphertext
276System.out.println("plaintext: " +
277javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
278System.out.println("ciphertext: " +
279javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
280// initialize AES for decryption
281myAES.init(Cipher.DECRYPT_MODE, myKey,new IvParameterSpec(new byte[16]));
282// initialize a new array of bytes to place the decryption
283byte[] dec_plaintext = new byte[16];
284cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,
2850);
286// process remaining blocks of ciphertext
287cLength += myAES.doFinal(dec_plaintext, cLength);
288// print the new plaintext (hopefully identical to the initial one)
289System.out.println("decrypted: " +
290javax.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