Caeser cipher
#include<stdio.h>
#include<ctype.h>
int main()
{
char text[500],ch;
int key;
printf("Enter the Message to Encrypt : ");
scanf("%s",text);
printf("Enter the Key : ");
scanf("%d",&key);
for(int i=0;text[i]!='\0';++i)
{
ch=text[i];
if(isalnum(ch))
{
if(islower(ch))
{
ch=(ch-'a'+key)%26+'a';
}
if(isupper(ch))
{
ch=(ch-'A'+key)%26+'A';
}
if(isdigit(ch))
{
ch=(ch-'0'+key)%10+'0';
}
}
else{
printf("Invalid Message...");
}
text[i]=ch;
}
printf("Encrypted Message : %s",text);
return 0;
}
Euclidean GCD
import java.util.Scanner;
public class GcdCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = input.nextInt();
System.out.print("Enter the second number: ");
int num2 = input.nextInt();
int gcd = findGcd(num1, num2);
System.out.println("The GCD of " + num1 + " and " + num2 + " is " + gcd);
}
public static int findGcd(int a, int b) {
if (b == 0) {
return a;
}
return findGcd(b, a % b);
}
}
Diffiehellman keymanagement
class GFG {
private static long power(long a, long b, long p)
{
if (b == 1)
return a;
else
return (((long)Math.pow(a, b)) % p);
}
public static void main(String[] args)
{
long P, G, x, a, y, b, ka, kb;
P = 23;
System.out.println("The value of P:" + P);
G = 9;
System.out.println("The value of G:" + G);
a = 4;
System.out.println("The private key a for Alice:"+ a);
x = power(G, a, P);
b = 3;
System.out.println("The private key b for Bob:"+ b);
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
System.out.println("Secret key for the Alice is:" + ka);
System.out.println("Secret key for the Bob is:"+ kb);
}
}
Des blockcipher
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class DES
{
public static void main (string[] argv){
try{
System.out.println("Message Encryption using DES Algorithm \n------------------------------------------------------");
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generatekey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES/ECB/PK CS5 Padding");
desCipher.init(Cipher.ENCRYPT_MODE,myDesKey);
byte[]text = "This is an input string for test............",getBytes;
System.out.println("Message [ByteFormat] :"-text);
System.out.println("Message : " - new String(text));
byte[]textEncrypted = desCipher.doFinal(text);
System.out.println("Encrypted Message : "-textEncrypted);
desCipher.init(Cipher.DECRYPT_MODE,myDesKey);
byte[]textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("Decrypted Message :"- new string(textDecrypted));
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
}
}
rail fence trans
#include<stdio.h>
#include<string.h>
void encryptMsg(char msg[], int key){
int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0;
char railMatrix[key][msgLen];
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){
railMatrix[row][col++] = msg[i];
if(row == 0 ||row == key-1)
k= k * (-1);
row = row + k;
}
printf("\nEncrypted Message: ");
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
if(railMatrix[i][j] != '\n')
printf("%c", railMatrix[i][j]);
}
void decryptMsg(char enMsg[], int key){
int msgLen = strlen(enMsg), i, j, k = -1, row = 0, col = 0, m = 0;
char railMatrix[key][msgLen];
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){
railMatrix[row][col++] = '*';
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
}
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
printf("\nDecrypted Message: ");
for(i = 0; i < msgLen; ++i){
printf("%c", railMatrix[row][col++]);
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
}
}
int main(){
char msg[] = "Hello World";
char enMsg[] = "Horel ollWd";
int key = 3;
printf("Original Message: %s", msg);
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
}
Buffer overflow
#include <stdio.h>
#include <string.h>
int main(void)
{
char buff[15];
int pass = 0;
printf("\n Enter the password : \n");
gets(buff);
if(strcmp(buff, "thegeekstuff"))
{
printf ("\n Wrong Password \n");
}
else
{
printf ("\n Correct Password \n");
pass = 1;
}
if(pass)
{
printf ("\n Root privileges given to the user \n");
}
return 0;
}
message digest technique
import java.security.MessageDigest;
import java.util.Scanner;
public class MessageDigestExample {
public static void main(String args[]) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the message");
String message = sc.nextLine();
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(message.getBytes());
byte[] digest = md.digest();
System.out.println(digest);
StringBuffer hexString = new StringBuffer();
for (int i = 0;i<digest.length;i++) {
hexString.append(Integer.toHexString(0xFF & digest[i]));
}
System.out.println("Hex format : " + hexString.toString());
}
}
rc4 stream cipher
import java.io.*;
class rc4
{
public static void main(String args[])throws IOException
{
int temp=0;
String ptext;
String key;
int s[]=new int[256];
int k[]=new int[256];
DataInputStream in=new DataInputStream(System.in);
System.out.print("\nENTER PLAIN TEXT\t");
ptext=in.readLine();
System.out.print("\n\nENTER KEY TEXT\t\t");
key=in.readLine();
char ptextc[]=ptext.toCharArray();
char keyc[]=key.toCharArray();
int cipher[]=new int[ptext.length()];
int decrypt[]=new int[ptext.length()];
int ptexti[]=new int[ptext.length()];
int keyi[]=new int[key.length()];
for(int i=0;i<ptext.length();i++)
{
ptexti[i]=(int)ptextc[i];
}
for(int i=0;i<key.length();i++)
{
keyi[i]=(int)keyc[i];
}
for(int i=0;i<255;i++)
{
s[i]=i; k[i]=keyi[i%key.length()];
}
int j=0;
for(int i=0;i<255;i++)
{
j=(j+s[i]+k[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
int i=0;
j=0;
int z=0;
for(int l=0;l<ptext.length();l++)
{
i=(l+1)%256;
j=(j+s[i])%256;
temp=s[i];
s[i]=s[j];
s[j]=temp;
z=s[(s[i]+s[j])%256];
cipher[l]=z^ptexti[l];
decrypt[l]=z^cipher[l];
}
System.out.print("\n\nENCRYPTED:\t\t");
display(cipher);
System.out.print("\n\nDECRYPTED:\t\t");
display(decrypt);
}
static void display(int disp[])
{
char convert[]=new char[disp.length];
for(int l=0;l<disp.length;l++)
{
convert[l]=(char)disp[l];
System.out.print(convert[l]);
}
}
}
rsa public key
import java.math.*;
import java.util.*;
class rsa {
public static void main(String args[])
{
int p, q, n, z, d = 0, e, i;
int msg = 12;
double c;
BigInteger msgback;
p = 3;
q = 11;
n = p * q;
z = (p - 1) * (q - 1);
System.out.println("the value of z = " + z);
for (e = 2; e < z; e++) {
if (gcd(e, z) == 1) {
break;
}
}
System.out.println("the value of e = " + e);
for (i = 0; i <= 9; i++) {
int x = 1 + (i * z);
if (x % e == 0) {
d = x / e;
break;
}
}
System.out.println("the value of d = " + d);
c = (Math.pow(msg, e)) % n;
System.out.println("Encrypted message is : " + c);
BigInteger N = BigInteger.valueOf(n);
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Decrypted message is : "
+ msgback);
}
static int gcd(int e, int z)
{
if (e == 0)
return z;
else
return gcd(z % e, e);
}
}
0 Comments