· 6 years ago · Apr 17, 2019, 03:08 PM
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using System.Security.Cryptography;
11namespace test2SSS
12{
13 public partial class Form1 : Form
14 {
15 byte[] Copy;
16 ConversionHandler myConverter = new ConversionHandler();
17 RSACryptoServiceProvider myRSA = new RSACryptoServiceProvider(2048);
18 RSACryptoServiceProvider myRSA2 = new RSACryptoServiceProvider(2048);
19 RSAParameters RSAParam;
20 public Form1()
21 {
22 InitializeComponent();
23 }
24
25 private void button1_Click(object sender, EventArgs e)
26 {
27
28
29
30 //AesManaged myAES = new AesManaged();
31 byte[] RSAciphertext;
32 byte[] plaintext;
33 //generate an AES key
34 //myAES.GenerateKey();
35 plaintext = myConverter.StringToByteArray(textBox1.Text);
36 //encrypt an AES key with RSA
37 RSAciphertext = myRSA.Encrypt(plaintext, true);
38 Copy = RSAciphertext;
39 textBox2.Text = myConverter.ByteArrayToString(RSAciphertext);
40 //decrypt and recover the AES key
41 //plaintext = myRSA2.Decrypt(RSAciphertext, true);
42 RSAParam = myRSA.ExportParameters(true);
43 //textBox3.Text = myConverter.ByteArrayToString(plaintext);
44 }
45
46 private void button2_Click(object sender, EventArgs e)
47 {
48 byte[] plaintext;
49 myRSA2.ImportParameters(RSAParam);
50 plaintext = myRSA2.Decrypt(Copy, true);
51 textBox3.Text = myConverter.ByteArrayToString(plaintext);
52 }
53 }
54}
55
56
57
58using System;
59using System.Collections.Generic;
60using System.Linq;
61using System.Text;
62using System.Threading.Tasks;
63
64namespace test2SSS
65{
66 class ConversionHandler
67 {
68 public byte[] StringToByteArray(string s)
69 {
70 return CharArrayToByteArray(s.ToCharArray());
71 }
72 public byte[] CharArrayToByteArray(char[] array)
73 {
74 return Encoding.ASCII.GetBytes(array, 0, array.Length);
75 }
76 public string ByteArrayToString(byte[] array)
77 {
78 return Encoding.ASCII.GetString(array);
79 }
80 public string ByteArrayToHexString(byte[] array)
81 {
82 string s = "";
83 int i;
84 for (i = 0; i < array.Length; i++)
85 {
86 s = s + NibbleToHexString((byte)((array[i] >> 4) &
87 0x0F)) + NibbleToHexString((byte)(array[i] &
88 0x0F));
89 }
90 return s;
91 }
92 public byte[] HexStringToByteArray(string s)
93 {
94 byte[] array = new byte[s.Length / 2];
95 char[] chararray = s.ToCharArray();
96 int i;
97 for (i = 0; i < s.Length / 2; i++)
98 {
99 array[i] = (byte)(((HexCharToNibble(chararray[2 * i])
100 << 4) & 0xF0) | ((HexCharToNibble(chararray[2
101 * i + 1]) & 0x0F)));
102 }
103 return array;
104 }
105 public string NibbleToHexString(byte nib)
106 {
107 string s;
108 if (nib < 10)
109 {
110 s = nib.ToString();
111 }
112 else
113 {
114 char c = (char)(nib + 55);
115 s = c.ToString();
116 }
117 return s;
118 }
119 public byte HexCharToNibble(char c)
120 {
121 byte value = (byte)c;
122 if (value < 65)
123 {
124 value = (byte)(value - 48);
125 }
126 else
127
128 {
129 value = (byte)(value - 55);
130 }
131 return value;
132 }
133 }
134}
135///////////////////////////////////////////////////////////////////////////////////////////////
136/*
137 * To change this license header, choose License Headers in Project Properties.
138 * To change this template file, choose Tools | Templates
139 * and open the template in the editor.
140 */
141package javaapplication46;
142
143import java.security.InvalidAlgorithmParameterException;
144import java.security.Key;
145import java.security.KeyPair;
146import java.security.KeyPairGenerator;
147import java.security.SecureRandom;
148import javax.crypto.Cipher;
149import java.security.InvalidKeyException;
150import java.security.NoSuchAlgorithmException;
151import java.security.spec.InvalidKeySpecException;
152import javax.crypto.BadPaddingException;
153import javax.crypto.IllegalBlockSizeException;
154import javax.crypto.SecretKey;
155import javax.crypto.SecretKeyFactory;
156import javax.crypto.ShortBufferException;
157import javax.crypto.spec.IvParameterSpec;
158import javax.crypto.spec.PBEKeySpec;
159import javax.crypto.spec.SecretKeySpec;
160import java.security.Security;
161import javax.crypto.NoSuchPaddingException;
162import javax.rmi.CORBA.Util;
163
164
165import java.util.Random;
166
167public class Javaapplication46 {
168
169 public static void main(String[] args) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException {
170 byte[] keyBytes = new byte[16];
171 byte[] ran = new byte[16];
172 // declare secure PRNG
173 SecureRandom myPRNG = new SecureRandom();
174 // seed the key
175 myPRNG.nextBytes(keyBytes);
176 // build the key from random key bytes
177 SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
178 // instantiate AES object for ECB with no padding
179 Cipher myAES = Cipher.getInstance("AES/CBC/NoPadding");
180 myPRNG.nextBytes(ran);
181 IvParameterSpec ivspec=new IvParameterSpec(ran);
182 // initialize AES objecy to encrypt mode
183 myAES.init(Cipher.ENCRYPT_MODE, myKey, ivspec );
184 // initialize plaintext
185 byte[] plaintext = new byte[16];
186 new Random().nextBytes(plaintext);
187 //initialize ciphertext
188 byte[] ciphertext = new byte[16];
189 // update cipher with the plaintext
190 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
191 // process remaining blocks of plaintext
192 cLength += myAES.doFinal(ciphertext, cLength);
193 // print plaintext and ciphertext
194 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
195 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
196 // initialize AES for decryption
197 myAES.init(Cipher.DECRYPT_MODE, myKey, ivspec);
198 // initialize a new array of bytes to place the decryption
199 byte[] dec_plaintext = new byte[16];
200 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,0);
201 // process remaining blocks of ciphertext
202 cLength += myAES.doFinal(dec_plaintext, cLength);
203 // print the new plaintext (hopefully identical to the initial one)
204 System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
205
206 // get a Cipher instance for RSA with PKCS1 padding
207 Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
208 // get an instance for the Key Generator
209 KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
210 // generate an 1024 bit key
211 myRSAKeyGen.initialize(1024, myPRNG);
212 KeyPair myRSAKeyPair= myRSAKeyGen.generateKeyPair();
213 // store the public and private key individually
214 Key pbKey = myRSAKeyPair.getPublic();
215 Key pvKey = myRSAKeyPair.getPrivate();
216 // init cipher for encryption
217 myRSA.init(Cipher.ENCRYPT_MODE, pbKey, myPRNG);
218 // encrypt, as expected we encrypt a symmetric key with RSA rather than a file or some longer stream which should be encrypted with AES
219 ciphertext = myRSA.doFinal(keyBytes);
220 // init cipher for decryption
221 myRSA.init(Cipher.DECRYPT_MODE, pvKey);
222 // decrypt
223 plaintext = myRSA.doFinal(ciphertext);
224 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
225 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
226 System.out.println("keybytes: " + javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
227
228 char[] password = "short_password".toCharArray();
229 byte[] salt = new byte[16];
230 int iteration_count = 10000;
231 int key_size = 128;
232 myPRNG.nextBytes(salt);
233 // initialize key factory for HMAC-SHA1 derivation
234 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
235 // set key specification
236 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,key_size);
237 // generate the key
238 SecretKey myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
239 // print the key
240 System.out.println("AES key: " +javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
241 }
242}