Question: Cryptography

Ads

 
 
 

Share on Google+Share on Google+

Asia Khan
Cryptography
3 Answer(s)      5 years and a month ago
Posted in : Java Beginners

I am trying to make an algo for encryption and decrytion.But I dont want to use the existing Algo's like dES,AES,RC4 etc.I want help for logic in writing an algo of my own.

Ads
View Answers

March 21, 2012 at 3:14 PM


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);
BufferedReader br=new BufferedReader(new FileReader(new File("data.txt")));
String st,str;
while((st=br.readLine()) != null)   {
    str+=st+" ";
}
byte[] cleartext = null;
cleartext = str.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));
}
}


March 21, 2012 at 3:30 PM


import java.security.*; 
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;

class EncryptAndDecrypt {

public static void main (String[] args) throws Exception{ 
    SecretKeyFactory key = SecretKeyFactory.getInstance("DES");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

    byte[] desKeyData = { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08 }; 
    DESKeySpec desKeySpec = new DESKeySpec(desKeyData);

    Key keys = key.generateSecret(desKeySpec);
    Cipher cipher = Cipher.getInstance("DES");
    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));
    }
}


March 21, 2012 at 5:19 PM


import java.security.*;
   import javax.crypto.*;
   import javax.crypto.spec.*;
   import java.io.*;

   public class EncryptAndDecrypt {
     public static String asHex (byte buf[]) {
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
      int i;

      for (i = 0; i < buf.length; i++) {
       if (((int) buf[i] & 0xff) < 0x10)
        strbuf.append("0");

       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
      }
      return strbuf.toString();
     }

     public static void main(String[] args) throws Exception {
       String message="Welcome";
       KeyGenerator kgen = KeyGenerator.getInstance("AES");
       kgen.init(128); 
       SecretKey skey = kgen.generateKey();
       byte[] raw = skey.getEncoded();

       SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
       Cipher cipher = Cipher.getInstance("AES");

       cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

       byte[] encrypted = cipher.doFinal((args.length == 0 ?"Welcome" : args[0]).getBytes());
       System.out.println("encrypted string: " + asHex(encrypted));

       cipher.init(Cipher.DECRYPT_MODE, skeySpec);
       byte[] original = cipher.doFinal(encrypted);
       String originalString = new String(original);
       System.out.println("Original string: " +originalString + " " + asHex(original));
     }
   }

Ads









Related Tutorials/Questions & Answers:
cryptography - Security
cryptography  What is cryptography
Advertisements
JAVA Cryptography
JAVA Cryptography   Hey Guys am new in here so take it easy on me ... i wanna ask you about java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * Basic symmetric encryption example */ public class
JAVA Cryptography
JAVA Cryptography   Hey Guys am new in here so take it easy on me ... i wanna ask you about java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * Basic symmetric encryption example */ public class
visual cryptography - Java Beginners
visual cryptography  iam doing M.E cse iam undergoing project phase doing project in Halftone visual cryptography may i know how to convert the single image into 2 shares by encoging in java
a competitive study on cryptography techniques over block cipher
a competitive study on cryptography techniques over block cipher  i need "a competitive study on cryptography techniques over block cipher" project source code... plz reply to my post... for more information about this project
Java Security
) In this tutorial, the author explains the cryptography-related concepts... learnt will be useful in understanding the Cryptography API in MFC also...; JCA/JCE (Java Cryptography Architecture & Java Cryptography Extensions
Java One way Hashing
Java One way Hashing One way hash functions are a major tool in cryptography. It is used to create digital signatures, which in turn identify and authenticate the message. It can have other practical applications as well
PHP Random Number
PHP Generate Random Numbers: A random number is becoming more useful these days, like captcha, statistical sampling, cryptography, computer simulation etc. To generate random number, PHP provides rand() function. 
Hi friends
Hi friends  How to create a guy based application for cryptography(encryption and decryption) with multiple algorithms like caesar, hash ..etc
Regarding project - Applet
Regarding project  hi friend , iam doing project in Visual cryptography in Java so i need the Help regarding how to make a share of a original imahe into shares anu
how to produce halftone shares? - Java Beginners
the original image.  Hi friend, Visual cryptography encodes a secret... to achieve a higher level of security, halftone visual cryptography was proposed
Help needed for J2me and PHP - Development process
Help needed for J2me and PHP  Hello Sir I am a degree student. I have a small CRM application using J2me and PHP. I want to do cryptography using bouncy castle and decrypt the same in php with mcrypt extension. I did try
What is WTLS?
What is WTLS?  hii, What is WTLS?   hello, Wireless Transport Layer Security protocal (WTLS) does all cryptography oriented features of WAP. WTLS handles encryption/decryption, user authentication and data integrity
library
(); Course c1 = new Course("Cryptography"); Course c2 = new Course("Java
Java SE 7
: Elliptic Curve Cryptography (ECC), CertPath Algorithm Disabling  etc Swing
Other Base Packages
and authorization, configurable access control, digital signing, cryptography
What are New Features in JAVA SE 6
of security APIs. These Security APIs span a wide range of areas, including cryptography
plsz i want the coding details of line
this code....its come visual cryptography grayconversion coding plsz anybody
plsz i want the coding details of line
get this value... plsz explain this code....its come visual cryptography
what is the difference between jdk 1.4 and 1.5 and 1.6
and XSLT processor (JAXP) - Integrated security and cryptography extensions (JCE
SQUANDER CRPTOGRAPHY
", Lord Peter Wimsey describes a cryptography technique that is simple for encoding... together in a pair, so where that happens, we shove in a Q, which won't confuse... letter at the end, we'd add on another Q to square it up. Now we take our first
SQUANDER CRPTOGRAPHY
", Lord Peter Wimsey describes a cryptography technique that is simple for encoding... together in a pair, so where that happens, we shove in a Q, which won't confuse... letter at the end, we'd add on another Q to square it up. Now we take our first
New Features of JAVA SE 6.
of areas, including cryptography public key infrastructure, secure communication
Java API
    Java Cryptography Extension JCE
Java Example Codes and Tutorials
, the author explains the cryptography-related concepts and packages in JDK, with code... in understanding the Cryptography API in MFC also. Browser All
What are the level in Jboss 5 application server to deploy the application
and cryptography extensions (JCE, JSSE, JAAS) - Java web start J2SE 5.0 (september 30
Java SE 6
APIs. These Security APIs span a wide range of areas, including cryptography public
Tutorials   
Java Spring Hibernate Struts Training What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java Java Example Codes and Tutorials in 2017 How do I read a large file quickly in Java? Is learning Java worthwhile? How to create first Java Program? Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly?

Ads

 
Advertisement null

Ads