· 8 years ago · Apr 21, 2017, 06:10 PM
1CEASERCIPHER :
2
3#include <iostream>
4using namespace std;
5string encrypt(string text, int s)
6{
7string result = "";
8for (int i=0;i<text.length();i++)
9Â Â Â {
10Â Â Â Â Â Â Â if (isupper(text[i]))
11Â Â Â Â Â Â Â Â Â Â Â Â result += char(int(text[i]+s-65)%26 +65);
12else
13Â Â Â Â Â Â Â Â result += char(int(text[i]+s-97)%26 +97);
14Â Â Â Â }
15Â Â Â Â return result;
16}
17Â int main()
18{
19Â Â Â Â string text="ATTACKATONCE";
20Â Â Â Â int s = 4;
21Â Â Â Â cout << "Text : " << text;
22Â Â Â Â cout << "\nShift: " << s;
23Â Â Â Â cout << "\nCipher: " << encrypt(text, s);
24Â Â Â Â return 0;
25}
26
27SHA:
28
29import java.security.MessageDigest;
30import java.util.Scanner;
31
32public class SHA1
33{
34 private static MessageDigest messageDigest;
35 private static Scanner inp = new Scanner(System.in);
36
37 public static void main(String[] args) throws Exception
38 {
39 System.out.print("Enter Text : ");
40 String data = inp.nextLine();
41
42 messageDigest = MessageDigest.getInstance("SHA1");
43 messageDigest.update(data.getBytes());
44
45 byte[] messageDi = messageDigest.digest();
46 StringBuffer stringBuffer = new StringBuffer();
47
48 for (byte bytes : messageDi)
49 stringBuffer.append(String.format("%02x",bytes&0xff));
50
51System.out.println("digestedSHA1(hex) is :" + stringBuffer.toString());
52 }
53}
54
55HILL:
56#include<stdio.h>
57#include<string.h>
58int main()
59{
60int a[3][3]={{7,2,1},{0,3,-1},{-3,4,-2}};
61int b[3][3]={{-2,8,-5},{3,-11,7},{9,-34,21}};
62int i,j;
63int c[20],d[20];
64char msg[20];
65int determinant=0,t=0;;
66printf("Enter plain text\n ");
67scanf("%s",msg);
68for(i=0;i<strlen(msg);i++)
69{
70c[i]=msg[i]-65;
71printf("%d ",c[i]);
72}
73for(i=0;i<3;i++)
74{
75t=0;
76for(j=0;j<3;j++)
77{
78t=t+(a[i][j]*c[j]);
79}
80
81d[i]=t%26;
82}
83
84
85
86for(i=0;i<3;i++)
87{
88
89while(d[i]<0)
90{
91
92d[i]=d[i]+26;
93
94}
95}
96
97printf("\nEncrypted Cipher Text :");
98for(i=0;i<3;i++)
99printf(" %c",d[i]+65);
100for(i=0;i<3;i++)
101{
102t=0;
103for(j=0;j<3;j++)
104{
105t=t+(b[i][j]*d[j]);
106}
107
108c[i]=t%26;
109}
110for(i=0;i<3;i++)
111{
112
113while(c[i]<0)
114{
115c[i]=c[i]+26;
116}
117}
118
119printf("\nDecrypted Cipher Text :");
120for(i=0;i<3;i++)
121printf(" %c",c[i]+65);
122return 0;
123}
124
125MONO:
126
127#include <iostream>
128#include<string.h>
129using namespace std;
130
131int main()
132{
133 string s,k="qwertyuiopasdfghjklzxcvbnm";
134 cout<<"enter the string\n";
135 getline(cin,s);
136 int n,i;
137 n=s.length();
138 string res;
139 for(i=0;i<n;i++)
140 {
141 if(s[i]==' ')
142 {
143 res[i]=' ';
144 continue;
145 }
146 int p=s[i]-'a';
147 res[i]=k[p];
148
149 }
150for(i=0;i<n;i++)
151 cout<<res[i];
152 cout<<"\n";
153 cout << "decryption\n";
154 for(i=0;i<n;i++)
155 {
156 if(res[i]==' ')
157 continue;
158 res[i]=k.find(res[i])+'a';
159 }
160 for(i=0;i<n;i++)
161 cout<<res[i];
162 cout<<"\n";
163 return 0;
164}
165
166BLOWFISH:
167
168import javax.crypto.Cipher;
169import javax.crypto.KeyGenerator;
170import javax.crypto.SecretKey;
171public class Blowfish {
172private static Cipher encryptCipher;
173private static Cipher decryptCipher;
174public static void main(String[] args) throws Exception{
175KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
176SecretKey secretKey = keygenerator.generateKey();
177encryptCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
178encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
179byte[] encryptedData = encryptData("MY LAST LAB");
180decryptCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
181decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
182decryptData(encryptedData);
183}
184private static byte[] encryptData(String data)
185throws Exception {
186System.out.println("Data Before Encryption :" + data);
187byte[] dataToEncrypt = data.getBytes();
188byte[] encryptedData = encryptCipher.doFinal(dataToEncrypt);
189System.out.println("Encrypted Data: " + encryptedData);
190return encryptedData;
191}
192private static void decryptData(byte[] data)
193throws Exception {
194byte[] textDecrypted = decryptCipher.doFinal(data);
195System.out.println("Decrypted Data: " + new String(textDecrypted));
196}
197}
198
199
200DES:
201import javax.crypto.Cipher;
202import javax.crypto.KeyGenerator;
203import javax.crypto.SecretKey;
204public class DES
205{
206private static Cipher encryptCipher;
207private static Cipher decryptCipher;
208public static void main(String[] args) throws Exception{
209KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
210SecretKey secretKey = keygenerator.generateKey();
211encryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
212encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
213byte[] encryptedData = encryptData("MY LAST LAB");
214decryptCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
215decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
216decryptData(encryptedData);
217}
218private static byte[] encryptData(String data)
219throws Exception {
220System.out.println("Data Before Encryption :" + data);
221byte[] dataToEncrypt = data.getBytes();
222byte[] encryptedData = encryptCipher.doFinal(dataToEncrypt);
223System.out.println("Encrypted Data: " + encryptedData);
224return encryptedData;
225}
226
227private static void decryptData(byte[] data)
228throws Exception {
229byte[] textDecrypted = decryptCipher.doFinal(data);
230System.out.println("Decrypted Data: " + new String(textDecrypted));
231}
232}
233
234
235RSA:
236
237import java.security.KeyPair;
238import java.security.KeyPairGenerator;
239import java.security.PrivateKey;
240import java.security.PublicKey;
241import javax.crypto.Cipher;
242import javax.crypto.IllegalBlockSizeException;
243
244public class Rsa {
245 public static void main(String args[]) throws Exception {
246 Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
247 byte[] textByte = "cbit".getBytes();
248 System.out.println("Plain text=" + new String(textByte));
249 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
250 kpg.initialize(512);
251 KeyPair pair = kpg.genKeyPair();
252 byte[] cipher = encrypt(textByte, pair, rsaCipher);
253 System.out.println("The Encryption using RSA Algorithm : " + cipher);
254 String plain = decrypt(cipher, pair, rsaCipher);
255 System.out.println("The Decryption using RSA Algorithm : " + plain);
256 }
257public static byte[] encrypt(byte[] textBytes, KeyPair pair, Cipher rsaCipher) throws Exception {
258 PublicKey pk = pair.getPublic();
259 rsaCipher.init(Cipher.ENCRYPT_MODE, pk);
260 byte[] encByte = rsaCipher.doFinal(textBytes);
261 return encByte;
262 }// end encrypt
263
264 public static String decrypt(byte[] cipherBytes, KeyPair pair, Cipher rsaCipher) throws Exception {
265 PrivateKey pvk = pair.getPrivate();
266 rsaCipher.init(Cipher.DECRYPT_MODE, pvk);
267 byte[] decByte = rsaCipher.doFinal(cipherBytes);
268 return new String(decByte);
269 }
270}
271
272
273RSA:
274import javax.crypto.KeyGenerator;
275import javax.crypto.Mac;
276import javax.crypto.SecretKey;
277public class MAC
278{
279public static void main(String[] argv) throws Exception
280{
281KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
282SecretKey key = keyGen.generateKey();
283Mac mac = Mac.getInstance(key.getAlgorithm());
284mac.init(key);
285String str = "CSE";
286byte[] utf8 = str.getBytes("UTF8");
287byte[] digest = mac.doFinal(utf8);
288String digestB64 = new sun.misc.BASE64Encoder().encode(digest);
289System.out.println(digestB64);
290}
291}
292
293DIFFIE:
294import java.io.*;
295import java.math.BigInteger;
296class DiffieHellman
297{
298 public static void main(String[]args)throws IOException
299 {
300 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
301 System.out.println("Enter prime number:");
302 BigInteger p=new BigInteger(br.readLine());
303 System.out.print("Enter primitive root of "+p+":");
304 BigInteger g=new BigInteger(br.readLine());
305 System.out.println("Enter value for x less than "+p+":");
306 BigInteger x=new BigInteger(br.readLine());
307 BigInteger R1=g.modPow(x,p);
308 System.out.println("R1="+R1);
309 System.out.print("Enter value for y less than "+p+":");
310 BigInteger y=new BigInteger(br.readLine());
311 BigInteger R2=g.modPow(y,p);
312 System.out.println("R2="+R2);
313 BigInteger k1=R2.modPow(x,p);
314 System.out.println("Key calculated at Alice's side:"+k1);
315 BigInteger k2=R1.modPow(y,p);
316 System.out.println("Key calculated at Bob's side:"+k2);
317 System.out.println("deffie hellman secret key Encryption has Taken");
318 }
319}