ns
ns
class CaesarCipher {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
} else {
} else {
encoded.append(i);
return encoded.toString();
System.out.println(CaesarCipher.encode(msg, 3));
}
OUTPUT
Playfair Cipher
import java.awt.Point;
class playfairCipher {
s = s.toUpperCase().replaceAll("[^A-Z]", "");
char c = s.charAt(i);
charTable[k / 5][k % 5] = c;
k++;
char a = txt.charAt(i);
if (row1 == row2) {
} else {
col1 = col2;
col2 = tmp;
txt.setCharAt(i, charTable[row1][col1]);
txt.setCharAt(i + 1, charTable[row2][col2]);
return txt.toString();
if (i == sb.length() - 1) {
sb.insert(i + 1, 'X');
String txt = "Security Lab"; /* make sure string length is even */ /* change J
to I */
createTbl(key, chgJtoI);
}
OUTPUT
Hill Cipher
class hillCipher {
} };
int x, y, z;
a = key.charAt(x % 26);
b = key.charAt(y % 26);
c = key.charAt(z % 26);
ret = "" + a + b + c;
return ret;
int x, y, z;
invkeymat[2][1];
invkeymat[2][2];
ret = "" + a + b + c;
return ret;
String msg;
int n;
msg = ("SecurityLaboratory");
msg = msg.toUpperCase();
msg += 'X';
}
OUTPUT
Vigenere Cipher
text = text.toUpperCase();
char c = text.charAt(i);
continue;
j = ++j % key.length();
return res;
text = text.toUpperCase();
char c = text.charAt(i);
continue;
j = ++j % key.length();
return res;
}
OUTPUT
RailFence Cipher
class railfenceCipherHelper {
int depth;
int r = depth;
int l = msg.length();
int c = l / depth;
int k = 0;
if (k != l) {
mat[j][i] = msg.charAt(k++);
} else {
mat[j][i] = 'X';
enc += mat[i][j];
return enc;
int r = depth;
int l = encmsg.length();
int c = l / depth;
int k = 0;
mat[i][j] = encmsg.charAt(k++);
dec += mat[j][i];
return dec;
class railFenceCipher {
int depth = 2;
}
OUTPUT
Row and Column Transformation Technique
import java.util.*;
class TransCipher {
String pl = sc.nextLine();
sc.close();
String s = "";
int start = 0;
s = s + pl.substring(start, i);
start = i + 1;
s = s + pl.substring(start);
System.out.print(s);
System.out.println();
int k = s.length();
int l = 0;
int col = 4;
if (l < k) {
ch[i][j] = s.charAt(l);
l++;
} else {
ch[i][j] = '#';
// arranged in matrix
trans[j][i] = ch[i][j];
System.out.print(trans[i][j]);
// display
System.out.println();
}
OUTPUT
DES Algorithm
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;
try{
Cipher desCipher;
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
}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();
}
OUTPUT
AES Algorithm
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
try {
setKey(secret);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return
Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
return null;
try {
setKey(secret);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new
String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
} catch (Exception e) {
return null;
}
OUTPUT
RSA Algorithm
<html>
<head>
<title>RSA Encryption</title>
</head>
<body>
<center>
<h1>RSA Algorithm</h1>
<hr>
<table>
<tr>
</tr>
<tr>
</td>
</tr>
<tr>
</td>
</tr>
<tr>
<td>Public Key:</td>
<td>
<p id="publickey"></p>
</td>
</tr>
<tr>
<td>Exponent:</td>
<td>
<p id="exponent"></p>
</td>
</tr>
<tr>
<td>Private Key:</td>
<td>
<p id="privatekey"></p>
</td>
</tr>
<tr>
<td>Cipher Text:</td>
<td>
<p id="ciphertext"></p>
</td>
</tr>
<tr>
</tr>
</table>
</center>
</body>
<script type="text/javascript">
function RSA() {
p = document.getElementById('p').value;
q = document.getElementById('q').value;
no = document.getElementById('msg').value;
n = p * q;
t = (p - 1) * (q - 1);
if (gcd(e, t) == 1) {
break;
x=1+i*t
if (x % e == 0) {
d = x / e;
break;
ct = ctt % n;
dt = dtt % n;
document.getElementById('publickey').innerHTML = n;
document.getElementById('exponent').innerHTML = e;
document.getElementById('privatekey').innerHTML = d;
document.getElementById('ciphertext').innerHTML = ct;
</script>
</html>
OUTPUT
Diffie-Hellman Key exchange Algorithm
class DiffieHellman {
else
}
OUTPUT
SHA-1 Algorithm
import java.security.*;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Algorithm=" + md.getAlgorithm());
System.out.println("Provider=" + md.getProvider());
System.out.println("ToString=" + md.toString());
md.update(input.getBytes());
System.out.println();
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println();
} catch (Exception e) {
System.out.println("Exception:" + e);
}
private static String bytesToHex(byte[] b) {
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
for (byte aB : b) {
return buf.toString();
}
OUTPUT
Digital Signature Standard
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Scanner;
keyPairGen.initialize(2048);
sign.initSign(privKey);
sign.update(bytes);
"UTF8"));
}
OUTPUT
Message Digest Algorithm
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
try {
digest.update(input.getBytes(), 0, input.length());
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return md5;
}
OUTPUT