adeamin2
Chief Gray
Joined: 13 Jan 2011
Posts: 259
Read: 0 topics
Warns: 0/5 Location: England
|
Posted: Tue 17:34, 15 Feb 2011 Post subject: the northwardly face Asymmetric Cryptography in Ja |
|
|
??????????? /**Method used to decrypt the string and return as bytes.,[link widoczny dla zalogowanych]
??????????????????????? try
??????????????????????????????????? ?* PublicKey and PrivateKey files.
??????????? {
??????????????????????? ?* The following line is used to
??????????? /**Method to return the @link PrivateKey
??????????? ?* @link PublicKey. In this usage you have to pass
??????????? /**
?* @author Debadatta Mishra(PIKU)
??????????? ?* @return the object of type @link PublicKey
??????????????????????? /*
??????????? public PublicKey getPublicKey()
???????????????????????
???????????
??????????????????????????????????? e.printStackTrace();
??????????? ?*/
??????????????????????? {
??????????? ?* @param filename of type String indicating the
??????????????????????? {
??????????? public PrivateKey getPrivateKey( String filename ) darts Exception
??????????? }
??????????????????????????????????? System.out.println("Public Key----"+publicKey2);
??????????????????????? return baos.toByteArray();
??????????????????????? return decryptedBytes;
?* This class is used to read the Private and Public key
??????????? ?* file name.
??????????? ?* You have to encrypt the string using your private
??????????????????????? try
??????????????????????????????????? keyCreator.writeKey("Public.key", publicKey.getEncoded());
???????
???????????
??????????? ?* Here the input parameter will be your Private key.
?
??????????????????????????????????? privateKey = keyFactory.generatePrivate(encodedPrivateKey);
??????????????????????????????????? System.out.println("Decrypted String----"+new String(decryptedBytes));
??????????? static
The following class is accustom to read the ��Public.key�� and ��Private.key�� generated along the upon program. If you are the landlord you can have the ��Private.key�� file based upon which you must encrypt the file and give your ��Public.key�� document to other end who absences to decrypt the data. In this emulating level, you tin peruse both the ��Public.key�� and ��Private.key�� files.
??????????????????????? }
??????????????????????? {
??????????? /**Method to return the @link PublicKey
??????????? }
??????????? ?* acted.
??????????? private KeyFactory keyFactory = null;
?
??????????? ?* Object of type @link KeyFactory
??????????? ?* The file can be a Public key file or a Private key
import java.io.ByteArrayOutputStream;
??????????? ?* Default constructor to initialize the
??????????????????????? cipher.init(Cipher.ENCRYPT_MODE , privateKey );
??????????????????????????????????? PrivateKey privateKey2 = keyReader.getPrivateKey("Private.key");
??????????????????????? byte[] encryptedBytes = null;
Class Name :- SecurityTestHarness.java
??????????????????????? } catch (IOException e) {
?* encrypt and decrypt the string based
??????????????????????? try
???????
{
?*/
??????????? ?* Object of type @link PrivateKey
??????????? ?* string to encrypt
?* the key. The key can be your either
??????????? }
??????????? ?* string to encrypt
??????????????????????? {
??????????? ?*/
??????????? ?* Here KeyPair object is initialized and
??????????????????????????????????? System.out.println("Private Key----"+privateKey2);
public class SecurityTestHarness
??????????????????????????????????? PublicKey publicKey2 = keyReader.getPublicKey("Public.key");
??????????????????????? super();
??????????? ?* file name.
Technicalities For data security Java Cryptography provides a appropriate structure to implement several kinds of cryptography. However there are basically 2 types of cryptography. Once is Symmetric Cryptography and Asymmetric Cryptography. When both the ends communicate the secured data with a prevalent opener because encryption and decryption, it is shrieked the Symmetric Cryptography. In this circumstance a shared key is used by both the parties to encrypt and decrypt the data. However there is a problem relating to commute of key for symmetric cryptography. To surmount this problem java provides dissimilar approach for the cryptography cried Asymmetric Cryptography. In case of Asymmetric cryptography, there ambition be 2 keys unlike one key in case of symmetric cryptography. One key is cried Private key and other is called Public key. These 2 keys are generated attach and can be used for encryption and decryption. In this case the Public key is used by anyone who wishes to communicate securely with the owner of the Private key. The Private key is used by the main owner and the owner gives the Public key so namely they can decrypt the data. In this article I will give you the sample ashore Asymmetric cryptography. You can ascertain extra tutorials and conception above Sun��s JCE(Java Cryptography Extension). In my next article, I will cater you the sample above Symmetric cryptography.
?
??????????????????????? cipher.init(Cipher.DECRYPT_MODE , publicKey );
??????????????????????? }
}
??????????????????????????????????? /*
Class Name:- KeyReader.java
?
??????????? /**This method is used to read the bytes from the file.
??????????? public PrivateKey getPrivateKey() {
???????????
??????????? ?* the file name of the Public.key file.
import javax.crypto.Cipher;
??????????? ?* @return encrypted bytes
?
package com.dds.security;
??????????? public KeyReader()
??????????????????????? catch( Exception e )
???????????????????????
?* This class also aids to test how
??????????? e.printStackTrace();
??????????? }
??????????? ?* @throws Exception
??????????????????????? KeyPair keyPair = KeyPairGenerator
??????????? FileOutputStream fos = current FileOutputStream(filename);
import java.io.IOException;
/**This class is used to generate the Private and Public key file.
??????????????????????????????????? ?*/
??????????? ?*/
??????????????????????????????????? KeyReader keyReader = new KeyReader();
??????????? ?* @param messsageBytes , it is the bytes from the
?
??????????????????????? return privateKey;
??????????????????????????????????? ?*/
??????????? ?* @throws Exception
??????????? ?* @param messsageBytes , it is the bytes from the
??????????????????????????????????? byte[] keydata = getKeyData(filename);
import java.security.PrivateKey;
??????????????????????? decryptedBytes = cipher.doFinal( messsageBytes );
??????????????????????? return privateKey;
??????????? ?*/
??????????? private PrivateKey privateKey = null;
??????????? /**
??????????????????????????????????? while ((b = fis.read()) ,[link widoczny dla zalogowanych]!= -1)
import java.security.PublicKey;
??????????????????????? ?* key.
?* new directory.
??????????????????????????????????? e.printStackTrace();
??????????? }
?* @author Debadatta Mishra(PIKU)
??????????? ?*/
??????????? fos.flush();
??????????????????????????????????? publicKey = keyFactory.generatePublic(encodedPublicKey);
???????????
??????? try
??????????? private PublicKey publicKey = null;
???????????
public class KeyCreator
??????????? ?*/
?*
??????????????????????????????????? /*
??????????? public void writeKey(String filename, byte[] contents)
??????????? ?*/
??????????????????????? ?* generate the Public and Private
import java.security.PublicKey;
???????????
??????????? public KeyCreator() throws Exception
??????????? private static Cipher cipher = null;
/**
??????????????????????????????????? X509EncodedKeySpec encodedPublicKey = new X509EncodedKeySpec(keydata);
???????????????????????
??????????????????????????????????? byte[] encryptedBytes = SecurityUtil.getEncryptedBytes(
??????????? ?* @throws Exception
??????????????????????? }
}
??????????? public static byte[] getDecryptedBytes( byte[] messsageBytes ,[link widoczny dla zalogowanych], PublicKey publicKey)throws Exception
??????????????????????????????????? KeyCreator keyCreator = new KeyCreator();
??????????????????????? PublicKey publicKey = null;
?* system.
??????????????????????? super();
??????????????????????????????????? ?* Get the public and private key
?
??????????? ?*/
The following class is used to create the Public key and Private key. This class contains generic methods to generate the Public and Private key. If you escape the testharness class, you will find the two files called ��Public.key�� and ��Private.key��. Please bring an end to ... the java docs mentioned in the methods.
import java.security.PrivateKey;
Asymmetric Cryptography in Java
?
??????????????????????? return publicKey;
??????????? {
??????????????????????????????????? ?* Generate two files named Public.key and Private.key
?* files generated using the Java's Asysmmetric Security
???????????
??????????????????????? try
???
??????????? ?* are created.
??????????? ?* Object of type @link Cipher
??????????????????????? int b;
package com.dds.security;
??????????? ?*/
??????????? ?* The following static is used to
??????????? ?* @param privateKey of type @link PrivateKey
??????????????????????? {
??????????????????????? ByteArrayOutputStream baos = new ByteArrayOutputStream();
??????????? ?* the name of Public alternatively Private key
import java.security.spec.X509EncodedKeySpec;
??????????? {
??????????????????????????????????? e.printStackTrace();
??????????????????????????????????????????????????????????? stringBytes, privateKey2);
??????????? ?* @throws Exception
??????????????????????????????????? e.printStackTrace();
import java.security.PrivateKey;
??????????????????????? publicKey = keyPair.getPublic();
??????????? /**This method is used to return the object of type
??????????????????????????????????? String str = "Hi, Hello World, Welcome to the World of Java";
??????????????????????????????????? e.printStackTrace();
??????????????????????? {
??????????? {
?
??????????? ?* @return the object of type @link PrivateKey
??????????? ?* based upon which encryption and decryption can be
??????????????????????? try
??????????????????????? {
??????????????????????????????????? PrivateKey privateKey = keyCreator.getPrivateKey();
To test the above programs, amuse create the suitable package as mentioned in the program. You can also create your own package and modify the package name in the above programs. You can entire the code in your gainful java editor.
Complete Example This example is only averaged for learning and no for anyone characteristic use. You can take the piece of code to test in your system to study the above concept.
???????
??????????? public static void main(String[] args)
?*/
???????????
??????????? ?*/
??????????? {
???????????????????????
???????????????????????
??????????????????????? encryptedBytes = cipher.doFinal(messsageBytes);
??????????????????????????????????? byte[] stringBytes = str.getBytes();
??????????? public static byte[] getEncryptedBytes( byte[] messsageBytes ,[link widoczny dla zalogowanych], PrivateKey privateKey) throws Exception
??????????? }
??????????? ?* key at your end.
??????????? ?* You must decrypt the string using your Public
??????????? ?* @return decrypted bytes
?* @author Debadatta Mishra(PIKU)
import java.security.KeyPair;
?
??????????????????????????????????????????????? baos.write(b);
import java.security.PrivateKey;
?* upon the Public and Private key.
import java.security.KeyPairGenerator;
?*
import java.security.PublicKey;
??????????? ?*/
??????????? private byte[] getKeyData( String fileName ) throws Exception
import com.dds.security.KeyReader;
???????????????????????????????????
import java.security.KeyFactory;
??????????? ?*/
??????????????????????????????????? System.out.println("EncryptedString----"+encryptedString);
??????????????????????? return encryptedBytes;
import com.dds.security.KeyCreator;
?
??????????????????????????????????? baos.flush();
??????????? ?* @return the bytes from the file
??????????? ?* keyFactory.
Class Name : - KeyCreator.java
??????????????????????? .getInstance("RSA")
?*
?
??????????????????????? catch( Exception e )
public class KeyReader
??????????? ?* file. In this file, you have stored the security key,[link widoczny dla zalogowanych],
??????????? ?* @param contents of the key
??????????????????????? FileInputStream fis = new FileInputStream(fileName);
??????????? ?* @param publicKey of type @link PublicKey
ClassName :- SecurityUtil.java
??????????? fos.write(contents);
archive com.dds.security;
?*/
??????????? public PublicKey getPublicKey( String filename ) throws Exception
??????????? /**This method is used to return the object of type
??????????????????????????????????? /*
??????????????????????? }
??????????????????????????????????? baos.close();
import java.io.FileOutputStream;
Conclusion I hope that you will enjoy my article. If you find any problems or peccadilloes, please feel free to bring me a mail in the residence [link widoczny dla zalogowanych] . This article is only meant for those who are new to java evolution. This article does not bear any commercial significance. Please provide me the response almost this article.
??????????????????????? byte[] decryptedBytes = null;
/**This is a utility class to provide
??????????? }
??????????? ?* initialize the Cipher object
??????????????????????? PrivateKey privateKey = null;
??????????????????????? catch( Exception e )
??????????? /**Method used to encrypt the string and return as bytes.
??????????????????????????????????? keyCreator.writeKey("Private.key",[link widoczny dla zalogowanych], privateKey.getEncoded());
import java.io.FileInputStream;
import com.dds.security.SecurityUtil;
??????????? ?* @return the @link PublicKey
??????????????????????????????????? ?*/
??????????????????????? }
??????????????????????????????????? e.printStackTrace();
??????????????????????????????????? ?* The following lines will generate the
??????????????????????????????????? PublicKey publicKey = keyCreator.getPublicKey();
??????????? ?* thereby public key and private key objects
??????? }
import java.io.IOException;
??????????????????????????????????? cipher = Cipher.getInstance("RSA");
??????????????????????????????????? byte[] decryptedBytes = SecurityUtil.getDecryptedBytes(encryptedBytes,[link widoczny dla zalogowanych], publicKey2);
??????????? /**Default constructor.
?
?* Public and Private key can be created.
??????????????????????? ?*/
public class SecurityUtil
The retinue is test saddle class to test the functionality of the above program. Please all over the comments and java docs of the above and below programs.
??????????? ?* the file name of the Private.key file.
?*/
??????????? ?* @param filename of type String indicating the
?
{
??????????? /**
??????????? /**
?* encryption and decryption based upon
??????????? ?* Object of type @link PublicKey
??????????? ?* @throws Exception
Security melodramas a premonitory role in our daytime to day life. So far software applications are concerned, security of data is necessary for authentication and for several validations. Normally while developing the applications, we use the concept of cryptography for password encryption and decryption. Some applications require more security,[link widoczny dla zalogowanych], so they work for high end security system favor trusted security certificates. The security primarily focuses on the honesty of the data at the several ends.
??????????? ?* key at the destination end.
??????????? }
??????????? ?*/
??????????????????????? {
??????????? ?* @param filename of type String indicating
?*
??????????????????????????????????? {
??????????????????????? catch( Exception e )
import java.security.PublicKey;
??????????????????????????????????? PKCS8EncodedKeySpec encodedPrivateKey = new PKCS8EncodedKeySpec(keydata);
??????????? ?* @throws Exception
??????????? {
??????????????????????? {
???????????????????????????????????
??????????? /**
??????????? ?* key file.
?* Public or Private .
??????????? ?* @return the @link PrivateKey
???????????????????????????????????
??????????? ?*/
??????????? ?* @param fileName of type String indicating the file name
package com.security.testharness;
}
}
/**This is a test harness class used to
??????????????????????????????????? byte[] keydata = getKeyData(filename);
?* @author Debadatta Mishra(PIKU)
??????????????????????? try
??????? arrest (IOException e)
??????????????????????? }
?
??????????? ?* Here the input parameter will be your Public key.
??????????? ?* @link PrivateKey. In this method you have to pass
import java.security.spec.PKCS8EncodedKeySpec;
??????????????????????? {
?* The Public.key file and Private.key file will be generated in the
??????????? /*
??????????????????????? return publicKey;
??????????????????????? catch( Exception e )
??????????? fos.close();
??????????????????????? privateKey = keyPair.getPrivate();
??????????????????????????????????? fis.close();
?
??????????? /**Method used to jot the Public or Private
The following class is a utility class which is used to encrypt and decrypt the data.
??????????? ?*/
??????????????????????????????????? String encryptedString = new String(encryptedBytes);
??????????? }
??????????????????????????????????? keyFactory = KeyFactory.getInstance("RSA");
??????????????????????? .generateKeyPair();
??????????????????????? }
??????????? {
?
The post has been approved 0 times
|
|