Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


madhu babu velpuri
Hi friends
3 Answer(s)      2 years and a month ago
Posted in : Java Beginners


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


Advertisement
View Answers

November 5, 2012 at 1:56 PM


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));
}
}


November 5, 2012 at 2:44 PM


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)); } }


November 5, 2012 at 2:52 PM


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);

   }
}


Related Tutorials/Questions & Answers:
Hi friends
Hi friends  How to create a guy based application for cryptography(encryption and decryption) with multiple algorithms like caesar, hash ..etc
Friends need a help on ruby..
Friends need a help on ruby..  Friends i need a ruby script and the requirement is When i am running a ruby script in cmd, after executing my script... the html report will have Passed Message. Friends please respond as soon
Advertisements
hi
hi   on clicking on button on 1st jsp page i want to display following options like friends,family,doctor etc.on next jsp page and also i want link... databases like the above i.e friends,family,etc pls can any one tell how can i do
hi
hi   on clicking on button on 1st jsp page i want to display following options like friends,family,doctor etc.on next jsp page and also i want link... databases like the above i.e friends,family,etc pls can any one tell how can i do
Get your friends' phone numbers
Get your friends' phone numbers  How to create on update trigger
Hi,
Hi,  Hi,what is the purpose of hash table
Login coding for gettin friends details
Login coding for gettin friends details  login codin in servlet?   Please visit the following link: Login Application
Hi..
Hi..   what are the keywords available in simple HelloWorld program
Hi..
Hi..  what are access specifier available in java
Hi,
Hi,   labels = new Hashtable<Integer,JLabel>
HI.
HI.  hi,plz send me the code for me using search button bind the data from data base in dropdownlist
Hi..
Hi..  null is a keyword.True/False?   hi friend, In Java true, false and null are not a Java keyword they are literals in Java. For reading in detail go through the following link The null keyword
hi....
hi....  plzz sent me d code for counting vowels in a string... gui programme
Hi
Hi  Hi How to implement I18N concept in struts 1.3? Please reply to me
Hi
Hi  Hi this is really good example to beginners who is learning struts2.0 thanks
hi,
hi,  print("code sample");how to display all elements in 2d array usin any one loop
Hi
Hi   how to read collection obj from jsp to servlet and from jsp - jsp?   Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/servlet-jsp-data-list.shtml Thanks
hi....
hi....  a programme which displays a combobox which contains num from 1 to 10 and calculates the factorial of dt num which user clicks
Hi
Hi  I want import txt fayl java.please say me...   Hi, Please clarify your problem! Thanks
hi
hi   I have connected mysql with jsp in linux and i have used JDBC connectivity but when i run the program, its not working the program is displaying
hi
hi   'print("code sample");`class example(){ public.static.void.main(); system.out.println("this is first pro");}   class example{ public static void main(String[]args){ System.out.println("this is first pro
Hi..
Hi..  diff between syntax and signature?   signature is a particular identity of a method in terms of its argument order ,type and their number e.g. void A(arguments) then here the order ,type and number of arguments
Hi
Hi  Hi All, I am new to roseindia. I want to learn struts. I do not know anything in struts. What exactly is struts and where do we use it. Please help me. Thanks in advance. Regards, Deepak
hi!
hi!  how can i write aprogram in java by using scanner when asking user to enter element user will be free to enter any type that he/she wants to to enter, like(int,double,float,String,....) thanx for answering....   Hi
Hi..
Hi..  what are the steps mandatory to develop a simple java program?   To develop a Java program following steps must be followed by a Java developer : First of all the JDK (Java Development Kit) must be available
hi
online multiple choice examination  hi i am developing online multiple choice examination for that i want to store questions,four options,correct answer in a xml file using jsp or java?can any one help me?   Please
hi
hi  what is the code for printing stars as follows * * * * * * * * * *print("code sample");   Hi Friend, Try... num=4; int p = num; int q = 0; for (int i = 0; i <= num; i++) { for (int j
hi
hi  int A[]=new int{2,5,4,1,3}; what is the error in declaration of this   Hi Friend, This is the wrong way of declaring an array. Array is always declared in the following ways: 1)int arr[]=new int[5]; 2)int arr
Hi
Hi   I need some help I've got my java code and am having difficulty to spot what errors there are is someone able to help import java.util.Scanner; public class Post { public static void main(String[] args) { Scanner sc
hi.......
hi.......  i've a project on railway reservation... i need to connect netbeans and mysql with the help of jdbc driver... then i need to design... for such a programme... plz help me...   Hi Friend, Try this: import java.awt.
hi!
hi!  public NewJFrame() { initComponents(); try { Class.forName("java.sql.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin"); } catch(Exception e
 

 

 

DMCA.com