Security plays a significant role in our daily lives. So far, software applications are concerned, the security of the data for authentication is required for multiple validations. Normally, during the development of applications, we use the concept of cryptography for password encryption and decryption. Some applications require more security, so they go for high-end security system as trusted security certificates. The security is focused mainly on the integrity of data from different ends. Technical aspects of data security, Java Cryptography provides a suitable framework to implement different types of cryptography. However, there are basically two types of cryptography. Once is Symmetric and asymmetric cryptography cryptography. If both ends of the secured data communicate with a common key for encryption and decryption, it is called the symmetric cryptography. In this case, a common key by both parties to encrypt and decrypt the data. However, there is a problem in relation to the exchange of keys for symmetric cryptography. To solve this problem Java has a different approach to the encryption key cryptography called. In asymmetric cryptography, there are two keys to a key, in contrast, in the case of symmetric cryptography. A key is a private key and other is called the Public Key. These two keys are generated together and can be used for encryption and decryption. In this case, the public key is by anyone who uses a secure communication with the owner of the private key needs. The private key by the owner and the owner gives the public key used, so that to decrypt the data. In this article I will give you the example of asymmetric cryptography. You can find more tutorials and the concept that Sun’s JCE (Java Cryptography Extension). In my next article I will give you the example of symmetric cryptography. Complete This example is intended only for learning and not for any specific use. You can test with the piece of code in your system to learn the definition above. The following class is used to create the public and private keys. This class contains generic methods to generate the public and private key. When you run the class TestHarness, you find two files named “Public. Key” and “Private. Key. Please go through the Java docs listed in the methods. Class Name: – KeyCreator. Java com package. DDS. Security; import java. io. FileOutputStream; import java. io. IOException; import java. Security. KeyPair; import java. Security. KeyPairGenerator; import java. Security. PrivateKey; import java. Security. PublicKey; / ** This class will be used to generate the private and public key. * The public. Key file and private. Key file will be generated in the * Current directory. * @ Author Debadatta Mishra (PIKU) * * / public class KeyCreator ( / ** * Object of type (@ link) PublicKey * / private PublicKey publicKey = null; / ** * Object of type (@ link) PrivateKey * / private PrivateKey privateKey = null; / ** Default constructor. * Here KeyPair object is initialized and * To the public and private key objects * Be created. * @ Throws Exception * / public KeyCreator () throws Exception ( super (); / * * The following line is used to * Production of public and private * Key. * / KeyPair key = KeyPairGenerator . getInstance (“RSA”) . generateKeyPair (); publicKey = key pair. getPublic (); privateKey = key pair. getPrivate (); ) / ** Method to return the (@ link) PublicKey * @ Return the (@ link) PublicKey * / public PublicKey GetPublicKey () ( PublicKey return; ) / ** Method to return the (@ link) PrivateKey * @ Return the (@ link) PrivateKey * / public PrivateKey getPrivateKey () ( PrivateKey return; ) / ** Used method to public or private contribution * Key file. * @ Param filename of type String indicating * The name of the public or private key * @ Param contents of the key * / public void writeKey (String filename, byte [] content) ( try ( FileOutputStream fos = new FileOutputStream (filename); fos. write (content); fos. flush (); fos. close (); ) catch (Exception e) ( e. printStackTrace (); ) ) ) The following class is used to read the “Public. Key” and “Private. Key” is generated by the above program. If you are the owner you can have the “Private. Key” file based on where you encrypt the data and give your “have Public. Key” to the other end of the file will decrypt the data. In this next class you can read both the “Public. Key” and “Private. Key” files. Class Name: – KeyReader. Java com package. DDS. Security; import java. io. ByteArrayOutputStream; import java. io. FileInputStream; import java. io. IOException; import java. Security. KeyFactory; import java. Security. PrivateKey; import java. Security. PublicKey; import java. Security. spec. PKCS8EncodedKeySpec; import java. Security. spec. X509EncodedKeySpec; / ** * This class is used to read the private and public key * Files using the Java Security Asysmmetric * System. * @ Author Debadatta Mishra (PIKU) * * / public class KeyReader ( / ** * Object of type (@ link) KeyFactory * / private KeyFactory keyFactory = null; / ** * Default constructor to initialize the * KeyFactory. * / public KeyReader () ( super (); try ( keyFactory = KeyFactory. getInstance (“RSA”); ) catch (Exception e) ( e. printStackTrace (); ) ) / ** This method is used to read the bytes from the file. * The file can be a public key file or private key * File. In this file, you have the security of stored keys, * Can, based on the encryption and decryption * Implemented. * @ Param file name of type String with the filename * @ Return the bytes from the file * @ Throws Exception * / private byte [] getKeyData (string filename) throws Exception ( FileInputStream fis = new FileInputStream (fileName); ByteArrayOutputStream baos = new ByteArrayOutputStream (); int b; try ( while ((b = fis. read ())! = -1) ( Bao. write (b); ) fis. close (); Bao. flush (); Bao. close (); ) Catch (IOException e) ( e. printStackTrace (); ) Bao’s return. toByteArray (); ) / ** This method is used to return the object of type * (@ Link) PrivateKey. With this method you must pass * The filename of the private sector. Key file. * @ Param filename of type string, the * Filename. * @ Return the object of type (@ link) PrivateKey * @ Throws Exception * / public PrivateKey getPrivateKey (String filename) throws Exception ( PrivateKey privateKey = null; try ( byte [] = Keydata getKeyData (filename); PKCS8EncodedKeySpec encodedPrivateKey = new PKCS8EncodedKeySpec (Keydata); privateKey = keyFactory. generate Private (encodedPrivateKey); ) catch (Exception e) ( e. printStackTrace (); ) PrivateKey return; ) / ** This method is used to return the object of type * (@ Link) PublicKey. In this method, you must pass * The file name of the Public. Key file. * @ Param filename of type string, the * Filename. * @ Return the object of type (@ link) PublicKey * @ Throws Exception * / public PublicKey GetPublicKey (String filename) throws Exception ( PublicKey PublicKey = null; try ( byte [] = Keydata getKeyData (filename); X509EncodedKeySpec encodedPublicKey = new X509EncodedKeySpec (Keydata); publicKey = keyFactory. generate Public (encodedPublicKey); ) catch (Exception e) ( e. printStackTrace (); ) PublicKey return; ) ) The following class is a helper class that is used to encrypt and decrypt the data. Class Name: – SecurityUtil. Java com package. DDS. Security; import java. Security. PrivateKey; import java. Security. PublicKey; Import javax. Crypto. Cipher; / ** This is a utility class to offer * Encryption and decryption based on * The key. The key can either use your * Public or private. * @ Author Debadatta Mishra (PIKU) * * / public class SecurityUtil ( / ** * Object of type (@ link) Cipher * / private static Cipher cipher = null; / * * The following static is used to * Initialize the Cipher object * / static ( try ( cipher = cipher. getInstance (“RSA”); ) catch (Exception e) ( e. printStackTrace (); ) ) / Used ** method to return the string to encrypt than bytes. * Here are the input parameters of your private key to decrypt. * The string to encrypt with your private * Button on your page. * @ Param messsageBytes, it is the bytes from the * String to encrypt * @ Param privateKey of type (@ link) PrivateKey * @ Return encrypted bytes * @ Throws Exception * / public static byte [] getEncryptedBytes (byte [] messsageBytes, PrivateKey privateKey) throws Exception ( byte [] encryptedBytes = null; Cipher. init (Cipher. ENCRYPT_MODE, privateKey); encryptedBytes = cipher. doFinal (messsageBytes); EncryptedBytes return; ) / ** Method used to decrypt the string and return as bytes. * Here are the input parameters will be your public key. * The string to decrypt with your public * Key at the end of the target. * @ Param messsageBytes, it is the bytes from the * String to encrypt * @ Param publicKey of type (@ link) PublicKey * @ Return decrypted bytes * @ Throws Exception * / public static byte [] getDecryptedBytes (byte [] messsageBytes, PublicKey publicKey) throws Exception ( byte [] decryptedBytes = null; Cipher. init (Cipher. DECRYPT_MODE, publicKey); decryptedBytes = cipher. doFinal (messsageBytes); DecryptedBytes return; ) ) The following is test harness class to test the functionality of the above program. Please by the comments and the Java docs go above and below programs. Class Name: – SecurityTestHarness. Java com package. Security. TestHarness; import java. Security. PrivateKey; import java. Security. PublicKey; Import com. DDS. Security. KeyCreator; Import com. DDS. Security. KeyReader; Import com. DDS. Security. SecurityUtil; / ** This is a test environment using class to * Encrypt and decrypt the string based * In the public and private key. * This class helps to examine how * Public and private key can be created. * @ Author Debadatta Mishra (PIKU) * * / public class SecurityTestHarness ( public static void main (String [] args) ( try ( / * * The following lines generate the * PublicKey and SSL files. * / KeyCreator KeyCreator KeyCreator = new (); PublicKey = publicKey KeyCreator. GetPublicKey (); PrivateKey privateKey = KeyCreator. getPrivateKey (); / * * Generate two files called Public. Key and Private. Key * / KeyCreator. writeKey (“Public. key”, publicKey. getEncoded ()); KeyCreator. writeKey (“Private. key”, privateKey. getEncoded ()); / * * Get the public and private key * / KeyReader KeyReader KeyReader = new (); PublicKey = publicKey2 KeyReader. GetPublicKey (“Public. Key”); System. out. Println (“Public Key —-” publicKey2 +); PrivateKey privateKey2 = KeyReader. getPrivateKey (“Private. key”); System. out. Println (“Private Key —-” privateKey2 +); String str = “Hello, Hello World, Welcome to the World of Java”; byte [] bytes = string Str getBytes (); byte [] = encryptedBytes SecurityUtil. getEncryptedBytes ( string bytes privateKey2); EncryptedString String = new String (encryptedBytes); System. out. Println (“+ encryptedString EncryptedString —-”); byte [] = decryptedBytes SecurityUtil. getDecryptedBytes (encryptedBytes, publicKey2); System. out. Println (“decoded string —-” + new String (decryptedBytes)); ) catch (Exception e) ( e. printStackTrace (); ) ) ) To test the above programs, please create the appropriate package as mentioned in the program. You can also change your own package and the package name in the above-mentioned programs. You can use the code in your favorable Java editor. Conclusion I hope you enjoy my article. If you have any problems or mistakes, please send me an e-mail address in the debadattamishra @ AOL. com. This item is only for those who are new in Java Development meant. This article makes no commercial significance. Please give me feedback on this product.
Apr 102010
