Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * SampleRSACrypto.java | ||
3 | */ | ||
4 | package lab.scd.encrypt.rsa; | ||
5 | |||
6 | /** | ||
7 | * Class created by @author Mihai HULEA. | ||
8 | * | ||
9 | * This class is part of the laborator4_2crypto project. | ||
10 | * | ||
11 | * Aplicatia exemplifica modul de reliazare a criptarii\decriptarii unui text | ||
12 | * folosind algoritmul RSA cu cheie asimetrica. | ||
13 | */ | ||
14 | import java.io.*; | ||
15 | import java.util.Arrays; | ||
16 | import java.security.Security; | ||
17 | import java.security.Provider; | ||
18 | import java.security.PrivateKey; | ||
19 | import java.security.PublicKey; | ||
20 | import java.security.KeyPair; | ||
21 | import java.security.KeyPairGenerator; | ||
22 | import java.security.NoSuchAlgorithmException; | ||
23 | import java.security.NoSuchProviderException; | ||
24 | import javax.crypto.Cipher; | ||
25 | import javax.crypto.NoSuchPaddingException; | ||
26 | import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
27 | |||
28 | |||
29 | public class SampleRSACrypto { | ||
30 | static final byte[] plainText = "Mesaj de test. Codare folosind algoritmul RSA".getBytes(); | ||
31 | |||
32 | public static void main(String argv[]) { | ||
33 | |||
34 | KeyPairGenerator rsaKeyPairGen; | ||
35 | KeyPair rsaKeyPair; | ||
36 | PublicKey rsaPub; | ||
37 | PrivateKey rsaPriv; | ||
38 | Cipher cp; | ||
39 | |||
40 | byte[] cipherText = null, newPlainText = null; | ||
41 | |||
42 | |||
43 | // Genereaza perechea cheie publica\privata | ||
44 | try { | ||
45 | rsaKeyPairGen = KeyPairGenerator.getInstance("RSA"); | ||
46 | } catch (NoSuchAlgorithmException ex) { | ||
47 | System.out.println("Eroare initializare generator chei."); | ||
48 | return; | ||
49 | } | ||
50 | |||
51 | // initializeaza generatorul de chei | ||
52 | rsaKeyPairGen.initialize(1024); | ||
53 | |||
54 | //genereaza perechea de chei | ||
55 | rsaKeyPair = rsaKeyPairGen.generateKeyPair(); | ||
56 | if (rsaKeyPair == null) { | ||
57 | System.out.println("Eroare generare pereche cheie publica - privata"); | ||
58 | return; | ||
59 | } | ||
60 | |||
61 | // cheia publica RSA | ||
62 | rsaPub = rsaKeyPair.getPublic(); | ||
63 | |||
64 | // cheia privata RSA | ||
65 | rsaPriv = rsaKeyPair.getPrivate(); | ||
66 | |||
67 | // initializeaza obiectul de tip Cipher folosit pentru criptare decriptare | ||
68 | try { | ||
69 | cp = Cipher.getInstance("RSA"); | ||
70 | } catch (NoSuchAlgorithmException ex) { | ||
71 | System.out.println("Algoritmul de criptare RSA nu exista."); | ||
72 | return; | ||
73 | } catch (NoSuchPaddingException ex) { | ||
74 | |||
75 | System.out.println("Eroare initializare Cipher."); | ||
76 | return; | ||
77 | } | ||
78 | |||
79 | // Criptare | ||
80 | try { | ||
81 | cp.init(Cipher.ENCRYPT_MODE, rsaPub); | ||
82 | System.out.println("Text initial: "+new String(plainText)); | ||
83 | cipherText = cp.doFinal(plainText); | ||
84 | System.out.println("Text criptat:"+new String(cipherText)); | ||
85 | |||
86 | } catch (Exception ex) { | ||
87 | System.out.println("Eroare criptare: " + ex.getMessage()); | ||
88 | return; | ||
89 | } | ||
90 | |||
91 | // Decriptare | ||
92 | try { | ||
93 | cp.init(Cipher.DECRYPT_MODE, rsaPriv); | ||
94 | newPlainText = cp.doFinal(cipherText); | ||
95 | System.out.println("Text decriptat:"+new String(newPlainText)); | ||
96 | } catch (Exception ex) { | ||
97 | System.out.println("Eroare decriptare: " + ex.getMessage()); | ||
98 | return; | ||
99 | } | ||
100 | |||
101 | // Verifica rezultatul | ||
102 | if (Arrays.equals(plainText, newPlainText)) { | ||
103 | System.out.println("Encriptare si decriptare realizata cu succes."); | ||
104 | } else { | ||
105 | System.out.println("Encriptarea si decriptarea nu au fost realizate corect."); | ||
106 | } | ||
107 | } | ||
108 | } |