0% found this document useful (0 votes)
27 views10 pages

Crypto Codes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

Crypto Codes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Ceaser Cipher

public class ceasercipher {


public static void main(String[] args) {

String out = encrypt("HELLO", 1);


System.out.println(out);
String dout = decrypt(out, 1);
System.out.println(dout);
}

static String encrypt(String text, int shift) {


String result = "";
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alphabets = alphabet.toCharArray();

for (int i = 0; i < text.length(); i++) {


int temp = text.charAt(i);
temp = (temp - 65 + shift) % 26;
result += alphabets[temp];
}
return result;
}

static String decrypt(String text, int shift) {


String result = "";
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alphabets = alphabet.toCharArray();
for (int i = 0; i < text.length(); i++) {
int temp = text.charAt(i);
temp = (temp - 65 - shift) % 26;
result += alphabets[temp];
}
return result;
}
}
VignereCipher

public class vignerecipher {


public static void main(String[] args) {

String text = "GEEKSFORGEEKS";


String key = "AYUSHAYUSHAYU";
String out = encrypt(text, key);
System.out.println(out);
}

static String encrypt(String text, String key) {


String result = "";

String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


char[] alphabets = alphabet.toCharArray();

for (int i = 0; i < text.length(); i++) {


int temp = (text.charAt(i) - 65 + key.charAt(i) -
65) % 26;
result += alphabets[temp];
}
return result;
}
}
HillCIpher

public class hillcipher {


public static void main(String[] args) {

String keyString = "GYBNQKURP";


String plainText = "ACT";
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alphabets = alphabet.toCharArray();

int n = 3;
int[][] keyMatrix = new int[n][n];

int[][] plainTextMatrix = new int[n][1];


int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int temp = keyString.charAt(k) - 65;
keyMatrix[i][j] = temp;
k += 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(keyMatrix[i][j] + " ");
}
}

k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1; j++) {
int temp = plainText.charAt(k) - 65;
plainTextMatrix[i][j] = temp;
k += 1;
System.out.println(temp);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1; j++) {
System.out.print(plainTextMatrix[i][j] + " ");
}
}
int[][] matrix = new int[3][1];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 1; j++) {
matrix[i][j] = 0;
for (int z = 0; z < 3; z++) {
matrix[i][j] += (keyMatrix[i][z] *
plainTextMatrix[z][j]);
}
matrix[i][j] = matrix[i][j] % 26;
}
}
String result = "";
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1; j++) {
result += alphabets[matrix[i][j]];
}
}
System.out.println(result);
}

}
SHA512

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA512 {


public static void main(String[] args) {
String s1 = "GeeksForGeeks";
System.out.println("\n" + s1 + " : " + encrypt(s1));
}

public static String encrypt(String plainText) {


try {
MessageDigest md = MessageDigest.getInstance("SHA-
512");

byte[] MessageDigest =
md.digest(plainText.getBytes());

BigInteger number = new BigInteger(1,


MessageDigest);

String cipherText = number.toString(16);

while (cipherText.length() < 32) {


cipherText = "0" + cipherText;
}

return cipherText;
} catch (NoSuchAlgorithmException e) {
System.err.println(e);
return "";

}
}

}
MD5

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {


public static void main(String[] args) {
String s1 = "GeeksForGeeks";
System.out.println("\n" + s1 + " : " + encrypt(s1));
}

public static String encrypt(String plainText) {


try {
MessageDigest md =
MessageDigest.getInstance("MD5");

byte[] MessageDigest =
md.digest(plainText.getBytes());

BigInteger number = new BigInteger(1,


MessageDigest);

String hashtext = number.toString(16);

while (hashtext.length() < 16) {


hashtext = "0" + hashtext;
}
return hashtext;
} catch (NoSuchAlgorithmException e) {
System.err.println(e);
return "";
}

}
}
Diffie hellman

public class diffiehellman {


public static void main(String[] args) {
long P, G, a, b, x, y, ka, kb;

P = 23;
G = 9;

a = 4;
b = 3;

x = power(G, a, P);

y = power(G, b, P);

ka = power(y, a, P);

kb = power(x, b, P);

System.out.println("Secret key for Alice is: " + ka);


System.out.println("Secret key for Bob is: " + kb);

public static long power(long a, long b, long p) {


if (b == 1) {
return a;
}
return ((long) Math.pow(a, b) % p);
}
}
Elgamal

public class elgamal {


public static void main(String[] args) {
long p, g, d, e, m, k, y1, y2;

p = 13;
g = 2;
d = 3;

e = power(g, d, p);

m = 4;

k = 7;

y1 = power(g, k, p);
y2 = (m * power(e, k, p)) % p;

long plainText = (y2 * power(y1, p - 1 - d, p)) % p;

System.out.println("Encrypted message: (" + y1 + ", "


+ y2 + ")");
System.out.println("Decrypted message: " + plainText);

public static long power(long a, long b, long p) {


if (b == 1) {
return a;
}
return ((long) Math.pow(a, b) % p);
}
}
AES

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {


public static void main(String[] args) {
try {
String key = "1234567890123456";
String data = "Hello World";

SecretKeySpec secretkey = new


SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretkey);

byte[] encrypted =
cipher.doFinal(data.getBytes());
System.out.println(new String(encrypted));
} catch (Exception e) {
System.out.println(e);
}
}
}
DES

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DES {


public static void main(String[] args) throws Exception {
String text = "Hello, DES!";

// Generate a DES key


KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey secretKey = keyGen.generateKey();

// Encrypt the text


Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
String encryptedText =
Base64.getEncoder().encodeToString(encrypted);
System.out.println("Encrypted: " + encryptedText);

// Decrypt the text


cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted =
cipher.doFinal(Base64.getDecoder().decode(encryptedText));
System.out.println("Decrypted: " + new
String(decrypted));
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy