Lab C&ns
Lab C&ns
Write a C program that contains a string (char pointer) with a value ‘Hello
world’. The program should XOR each character in this string with 0 and
displays the result.
2. Write a C program that contains a string (char pointer) with a value ‘Hello
world’. The program should AND or and XOR each character in this string with
127 and display the result.
7. Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello
world” using Blowfish. Create your own key using Java key tool.
10. Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
11. Calculate the message digest of a text using the MD5 algorithm in JAVA
Program:1
#include <stdio.h>
#include <string.h>
int main() {
int i;
printf("Original string: %s\n", str);
printf("\n");
return 0;
Output:
Program:2
#include <stdio.h>
#include <string.h>
int main() {
int i;
printf("\n");
printf("\n");
return 0;
Output:
Program :3
a) CaesarCipher
if (Character.isLetter(c)) {
ciphertext.append(newCharacter);
} else {
ciphertext.append(c);
return ciphertext.toString();
int shift = 3;
}
Output:
b) SubstitutionCipher
if (Character.isLetter(c)) {
ciphertext.append(Character.isUpperCase(c) ? Character.toUpperCase(newCharacter) :
newCharacter);
} else {
ciphertext.append(c);
return ciphertext.toString();
if (Character.isLetter(c)) {
plaintext.append(Character.isUpperCase(c) ? Character.toUpperCase(newCharacter) :
newCharacter);
} else {
plaintext.append(c);
return plaintext.toString();
Output:
c) Hill Cipher
import java.util.Scanner;
if (plaintext.length() % SIZE != 0) {
plaintext += "x";
}
return result;
Output:
Plaintext: attackatdawn
Ciphertext: ndwjvzntkryh
4.des algo
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
cipher.init(Cipher.ENCRYPT_MODE, key);
cipher.init(Cipher.DECRYPT_MODE, key);
OUTPUT:
Plaintext: Hello, world!
Ciphertext: K???&7??F????
Decrypted text: Hello, world!
<script>
// This program calculates the Key for two persons
// using the Diffie-Hellman Key exchange algorithm
// Power function to return value of a ^ b mod P
function power(a, b, p)
{
if (b == 1)
return a;
else
return((Math.pow(a, b)) % p);
}
// Driver code
var P, G, x, a, y, b, ka, kb;
// Both the persons will be agreed upon the
// public keys G and P
// A prime number P is taken
P = 23;
document.write("The value of P:" + P + "<br>");
// A primitive root for P, G is taken
G = 9;
document.write("The value of G:" + G + "<br>");
// Alice will choose the private key a
// a is the chosen private key
a = 4;
document.write("The private key a for Alice:" +
a + "<br>");
// Gets the generated key
x = power(G, a, P);
// Bob will choose the private key b
// b is the chosen private key
b = 3;
document.write("The private key b for Bob:" +
b + "<br>");
// Gets the generated key
y = power(G, b, P);
// Generating the secret key after the exchange
// of keys
ka = power(y, a, P); // Secret key for Alice
kb = power(x, b, P); // Secret key for Bob
document.write("Secret key for the Alice is:" +
ka + "<br>");
document.write("Secret key for the Bob is:" +
kb + "<br>");
// This code is contributed by Ankita saini
</script>
Note : write this code in notepad save this file with .html extension
Output:
Program in java
class GFG{
// Power function to return value of a ^ b mod P
private static long power(long a, long b, long p)
{
if (b == 1)
return a;
else
return (((long)Math.pow(a, b)) % p);
}
// Driver code
public static void main(String[] args)
{
long P, G, x, a, y, b, ka, kb;
// Both the persons will be agreed upon the
// public keys G and P
// A prime number P is taken
P = 23;
System.out.println("The value of P:" + P);
// A primitive root for P, G is taken
G = 9;
System.out.println("The value of G:" + G);
// Alice will choose the private key a
// a is the chosen private key
a = 4;
System.out.println("The private key a for Alice:" + a);
// Gets the generated key
x = power(G, a, P);
// Bob will choose the private key b
// b is the chosen private key
b = 3;
System.out.println("The private key b for Bob:" + b);
// Gets the generated key
y = power(G, b, P);
// Generating the secret key after the exchange
// of keys
ka = power(y, a, P); // Secret key for Alice
kb = power(x, b, P); // Secret key for Bob
System.out.println("Secret key for the Alice is:" + ka);
System.out.println("Secret key for the Bob is:" + kb);
}
}
Output:
The value of P : 23
The value of G : 9
7.Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello
world” using Blowfish. Create your own key using Java key tool.
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class BlowFishCipher{
public static void main(String[]args) throws Exception{
//create a keygenerator based upon the
KeyGenerator keygenerator=
KeyGenerator.getInstance("Blowfish");
//create a key
SecretKey secretkey=keygenerator.generateKey();
//create a cipher based upon Blowfish
Cipher cipher=Cipher.getInstance("Blowfish");
//initialise cipher to with secretkey
cipher.init(Cipher.ENCRYPT_MODE,secretkey);
//get the text to encrypt
String inputText = "Hello world";
//encrypt message
byte[] encrypted=cipher.doFinal(inputText.getBytes());
//re-initialise the cipher to be in decrypt mode
cipher.init(Cipher.DECRYPT_MODE,secretkey);
byte[] decrypted=cipher.doFinal(encrypted);
System.out.println("Original String: " + inputText);
System.out.println("Encrypted: " + new String(encrypted));
System.out.println("Decrypted: " + new String(decrypted));
}
}
Output:
Original String: Hello worldEncrypted: ?6P??H??u?8???m?
Decrypted: Hello world
import java.util.Scanner;
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(message.getBytes());
System.out.println(digest);
Output:
java -cp /tmp/oy4rMs2UiS MessageDigestExample
Enter the message
hi
[B@4d405ef7
Hex format :
c22b5f917834269428d6f51b2c5af4cbde6a42
11.calculate message digest of a text using the md-5
algorithm in java
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
// digest() method is called to calculate message digest
return hashtext;
catch (NoSuchAlgorithmException e) {
// Driver code
String s = "cryptogrphy";
Output:
java -cp /tmp/oy4rMs2UiS MD5