
How to create a guy based application for cryptography(encryption and decryption) with multiple algorithms like caesar, hash ..etc

Here is a code of encryption and decryption using RSA algorithm
import java.io.*;
import java.security.*;
import javax.crypto.*;
class EncryptAndDecrypt {
public static void main (String[] args) throws Exception{
KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keygenerator.initialize(1024, random);
KeyPair keypair = keygenerator.generateKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String st= "roseindia";
byte[] cleartext = null;
cleartext = st.getBytes();
byte[] ciphertext = null;
ciphertext = cipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + ciphertext.toString());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cleartext1 = cipher.doFinal(ciphertext);
System.out.println("the decrypted cleartext is: " + new String(cleartext1));
}
}

Here is a code of encryption and decryption using DES algorithm.
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
class EncryptAndDecrypt {
public static void main (String[] args) throws Exception{ //KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RC4"); SecretKeyFactory key = SecretKeyFactory.getInstance("DES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); //keygenerator.initialize(1024, random); byte[] desKeyData = { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08 }; DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
//KeyPair keypair = keygenerator.generateKeyPair(); Key keys = key.generateSecret(desKeySpec); //PrivateKey privateKey = keypair.getPrivate(); //PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("DES");
System.out.println("publicKey="+keys.toString()); //System.out.println("privateKey="+privateKey.toString());
cipher.init(Cipher.ENCRYPT_MODE, keys); String st= "Welcome"; byte[] cleartext = null; cleartext = st.getBytes(); byte[] ciphertext = null; ciphertext = cipher.doFinal(cleartext); System.out.println("the encrypted text is: " + ciphertext.toString());
cipher.init(Cipher.DECRYPT_MODE, keys); byte[] cleartext1 = cipher.doFinal(ciphertext); System.out.println("the decrypted cleartext is: " + new String(cleartext1)); } }

Here is a code of encryption and decryption using AES algorithm.
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
/* Mode = CipherMode.CBC,-( Cipher-block chaining)
Padding = PaddingMode.PKCS7 or PKCS5,
KeySize = 128,
BlockSize = 128,
Key = keyBytes - password,
IV = keyBytes - password*/
public class testencrypt {
Cipher cipher;
// Input encrypted String
static String input = "Hi This is my String";
// password for encryption
final static String strPassword = "password12345678";
// put this as key in AES
static SecretKeySpec key = new SecretKeySpec(strPassword.getBytes(), "AES");
public static void main(String args []) throws Exception{
// Parameter specific algorithm
AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes());
//Whatever you want to encrypt/decrypt
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// You can use ENCRYPT_MODE (ENCRYPTunderscoreMODE) or DECRYPT_MODE (DECRYPT underscore MODE)
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
// encrypt data
byte[] ecrypted = cipher.doFinal(input.getBytes());
// encode data using standard encoder
String output = new BASE64Encoder().encode(ecrypted);
System.out.println("Orginal tring: " + input);
System.out.println("Encripted string: " + output);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.