CC Lab Spiral.
CC Lab Spiral.
CC Lab Spiral.
8. RSA Algorithm
AIM:
To write a c program that contain XOR each character in this string with zero.
Source Code:
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
clrscr();
char str1[11];
int i, len;
len = strlen(str);
for(i=0;i<len;i++)
str1[i]=str[i]^0;
printf("%c", str1[i]);
printf("\n");
getch();
}
Output:
Result:
Thus the program was successfully executed.
XOR a string with 127
Aim:
To write a C program that contain a string with a value hello world with AND or and 127.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
clrscr();
char str1[11];
char str2[11];
int i,len;
len = strlen(str);
for(i=0;i<len;i++)
{
str1[i]=str[i] & 127;
printf("%c", str1[i]);
}
printf("\n");
for(i=0;i<len;i++)
{
str2[i]=str[i]^127;
printf("%c", str2[i]);
}
printf("\n");
getch();
}
Output:
Result:
Thus the program was successfully executed.
Encryption And Decryption Using Algorithm
Aim:
To write a java program to perform encryption and decryption using algorithm.
Source Code:
a) Ceaser Cipher
import java.io.BufferedReader:
import java.io.IOException:
import java.io.InputStreamReader:
import java.util.Scanner:
System.out.print("Enter any String: "); String str = br.readLine(); System.out.print("\nEnter the Key: ");
int c = str.charAt(i):
if (Character.isUpperCase(c))
c=c+(key % 26);
if (c>'Z') cc-26:
else if (Character.isLowerCase(c))
int c = str.charAt(i):
if (Character.isUpperCase(c))
c=c(key% 26):
if (c<'A') c = c + 26:
else if (Character.isLowerCase(c))
{
if (c'a') cc+26:
}
decrypted += (char) c:
}
}
return decrypted;
}
b) Substitution Cipher
PROGRAM:
import java.io.*;
import java.util.*;
char c:
for(int i=0;i<str.length();i++)
{
c = str.charAt(i):
int j = a.indexOf(c):
decrypt = decrypt+b.charAt(j):
c)Hill Cipher
PROGRAM:
import java.io.*;
import java.util.*;
import java.io.*;
getkeymes():
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
for(int k=0:k<3:k++)
res[i][j]-res[i][j]+a[i][k]*mes[k][j]:
}
for(int i=0;i<3;i++)
{
System.out.print((char)(res[i][0]%26+97)): res[i][0]=res[i][0]:
inverse():
for(int i=0;i<3:i++)
for(int j=0;j<1:j++)
for(int k=0;k<3:k++)
{ decrypt[i][j] = decrypt[i][j]+b[i][k]*res[k][j]:
for(int i=0;i<3:i++)
{ System.out.print((char)(decrypt[i][0]%26+97));
}
System.out.print("\n");
} public static void getkeymes() throws IOException { System.out.println("Enter 3x3 matrix for key (It
should be inversible): ");
for(int j=0;j<3;j++)
a[i][j] = sc.nextFloat();
floatp.q: float[][] c = a:
for(int i=0:i<3:i++)
for(int j=0:j<3:j++)
//a[i][j]=sc.nextFloat():
if(ij) b[i][j]=1
else b[i][j]=0;
for(int k=0:k<3:k++)
{
for(int i=0:i<3:i++)
p=c[i][k]:
q=c[k][k];
for(int j=0:j<3:i++)
{
if(i!=k)
{
c[i][j]c[i][j]*q-p*c[k][j]:
c[i][j] = c[i][j]*q-p*c[k][j];
b[i][j] = b[i][j]*q-p*b[k][j]:
}
}
}
}
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
b[i][j]=b[i][j]/c[i][i]:
}
System.out.println("");
Result:
SOURCE CODE:
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class DESAlgorithm {
public static void main(String[] args) throws Exception {
String plainText = "Hello, world!";
String key = "mySecretKey"; // Replace with your secret key
// Encryption
byte[] encrypted = encrypt(plainText, key);
System.out.println("Encrypted text: " + new String(encrypted));
// Decryption
String decrypted = decrypt(encrypted, key);
System.out.println("Decrypted text: " + decrypted);
}
public static byte[] encrypt(String plainText, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
SecretKey secretKey = new SecretKeySpec(key.getBytes(), "DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(plainText.getBytes());
}
public static String decrypt(byte[] cipherText, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
SecretKey secretKey = new SecretKeySpec(key.getBytes(), "DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(cipherText);
return new String(decryptedBytes);
}
}
Output:
Result:
Thus the above program was successfully executed.
BLOWFISH Algorithm Logic
Aim:
To write a program to implement the Blowfish algorithm Logic.
Source Code:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
Result:
AIM:
SOURCE CODE:
import javax.crypto.*;
import javax.crypto.spec.*;
class Rijndael {
StringBuffer(buf.length*2);
int i;
for(i=0;i<buf.length;i++) {
if(((int)buf[i]&0xff)<0x10)
strbuf.append("0");
strbuf.append(Long.toString((int)buf[i]&0xff,16));
return strbuf.toString();
KeyGenerator kgen=
KeyGenerator.getInstance("AES");
available
SecretKeySpec(raw,"AES");
cipher.init(Cipher. ENCRYPT_MODE,skeySpec);
byte encrypted=
cipher.doFinal((args.length==0? message:
args[0]).getBytes());
asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
byte[]original=cipher.doFinal(encrypted);
System.out.println("Original String in
}
Output:
Result:
Aim:
To write java Cryptography, encrypt the text “Hello World” using BlowFish.
Source Code:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
Result:
Thus the program was successfully executed.
RSA Algorithm
AIM:
SOURCE CODE:
import java.math.*;
import java.util.*;
class RSA {
public static void main(String args[])
{
int p, q, n, z, d = 0, e, i;
Output:
Result:
Thus the program was successfully executed
Diffie-Hellman Key Exchange
Aim:
To implement the Diffie-Hellman Key Exchange mechanism using HTML and Javascript.
Source Code:
<script>
// Driver code
var P, G, x, a, y, b, ka, kb;
</script>
Output:
Result:
Thus the program was successfully executed.
SHA Algorithm
Aim:
To calculate the message digest of a text using the SHA-1 algorithm in java.
Source Code:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
try {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
byte[] digest = sha1.digest(text.getBytes());
StringBuilder hexString = new StringBuilder();
Result: