]> git.somenet.org - pub/jan/dslab.git/blob - ue3/src/Test.java
all the dslab stuff
[pub/jan/dslab.git] / ue3 / src / Test.java
1 import java.math.*;\r
2 import java.io.*;\r
3 import java.security.*;\r
4 import javax.crypto.*;\r
5 \r
6 public class Test {\r
7 public static void main (String[] args) throws NoSuchAlgorithmException, \r
8 \r
9 InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException, \r
10 \r
11 BadPaddingException, NoSuchPaddingException {\r
12 \r
13 /* Generate a RSA key pair */\r
14 \r
15 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");\r
16 SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");\r
17 \r
18 keyGen.initialize(512, random);\r
19 \r
20 KeyPair pair = keyGen.generateKeyPair();\r
21 PrivateKey priv = pair.getPrivate();\r
22 PublicKey pub = pair.getPublic();\r
23 //System.out.println("Public is" + pub);\r
24 //System.out.println("Private is" + priv); \r
25 \r
26 /* Create the cipher */\r
27 Cipher rsaCipher = Cipher.getInstance("RSA");\r
28 rsaCipher.init(Cipher.ENCRYPT_MODE, pub);\r
29 \r
30 // Cleartext\r
31 byte[] cleartext = "This is Bilal".getBytes();\r
32 System.out.println("the original cleartext is: " + new String(cleartext));\r
33 //System.out.println("the original cleartext is: " + cleartext);\r
34 \r
35 // Encrypt the cleartext\r
36 byte[] ciphertext = null;\r
37 ciphertext = rsaCipher.doFinal(cleartext);\r
38 //byte[] ciphertext = rsaCipher.doFinal(cleartext);\r
39 //String ciphertext = rsaCipher.doFinal(cleartext);\r
40 System.out.println("the encrypted text is: " + new String(ciphertext));\r
41 \r
42 ciphertext = rsaCipher.doFinal(cleartext);\r
43 //byte[] ciphertext = rsaCipher.doFinal(cleartext);\r
44 //String ciphertext = rsaCipher.doFinal(cleartext);\r
45 System.out.println("the encrypted text is: " + new String(ciphertext));\r
46 \r
47 ciphertext = rsaCipher.doFinal(cleartext);\r
48 //byte[] ciphertext = rsaCipher.doFinal(cleartext);\r
49 //String ciphertext = rsaCipher.doFinal(cleartext);\r
50 System.out.println("the encrypted text is: " + new String(ciphertext));\r
51 \r
52 // Initialize the same cipher for decryption\r
53 rsaCipher.init(Cipher.DECRYPT_MODE, priv);\r
54 \r
55 // Decrypt the ciphertext\r
56 byte[] cleartext1 = rsaCipher.doFinal(ciphertext);\r
57 //String cleartext1 = rsaCipher.doFinal(ciphertext);\r
58 System.out.println("the final cleartext is: " + new String(cleartext1));\r
59 //System.out.println("the final cleartext is: " + cleartext1);\r
60 }\r
61 }