Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Encript and Decript Data Using with BASE64Encoder and BASE64Decoder

    • 0
    • 2
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 697
    Comment on it

    Below written code will be Encrypt and Decrypt Data.This program having used Base64 Encoding which is commonly used in a number of applications including email via MIME, and storing complex data in XML.

    1. import java.net.URLDecoder;
    2. import java.net.URLEncoder;
    3. import javax.crypto.Cipher;
    4. import javax.crypto.SecretKey;
    5. import javax.crypto.SecretKeyFactory;
    6. import javax.crypto.spec.PBEParameterSpec;
    7.  
    8.  
    9. /**
    10. * An easy to use class to encrypt and decrypt a string. Just call the simplest
    11. * constructor and the needed methods.
    12. *
    13. */
    14.  
    15. public class StringEncryptor
    16. {
    17. private Cipher encryptCipher;
    18. private Cipher decryptCipher;
    19. @SuppressWarnings("restriction")
    20. private sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    21.  
    22.  
    23. @SuppressWarnings("restriction")
    24. private sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
    25.  
    26. final private String charset = "UTF-8";
    27. final private String defaultEncryptionPassword = "PAOSIDUFHQWER8978QWE378AHASDF93HASDF9238HAJSDF989";
    28. final private byte[] defaultSalt = {
    29.  
    30. (byte)0xa3, (byte)0x21, (byte)0x24, (byte)0x2c,
    31.  
    32. (byte)0xf2, (byte)0xd2, (byte)0x3e, (byte)0x19};
    33.  
    34. /**
    35. * The simplest constructor which will use a default password and salt to
    36. * encode the string.
    37. *
    38. * @throws SecurityException
    39. */
    40. public StringEncryptor() throws SecurityException
    41. {
    42. setupEncryptor(defaultEncryptionPassword, defaultSalt);
    43. }
    44.  
    45. /**
    46. * Dynamic constructor to give own key and salt to it which going to be used
    47. * to encrypt and then decrypt the given string.
    48. *
    49. * @param encryptionPassword
    50. * @param salt
    51. */
    52. public StringEncryptor(String encryptionPassword, byte[] salt)
    53. {
    54. setupEncryptor(encryptionPassword, salt);
    55. }
    56.  
    57. public void init(char[] pass, byte[] salt, int iterations) throws SecurityException
    58. {
    59. try
    60. {
    61. PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
    62.  
    63. SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    64.  
    65. SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pass));
    66.  
    67. encryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
    68.  
    69. encryptCipher.init(Cipher.ENCRYPT_MODE, k, ps);
    70.  
    71. decryptCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
    72.  
    73. decryptCipher.init(Cipher.DECRYPT_MODE, k, ps);
    74. }
    75. catch (Exception e)
    76. {
    77. throw new SecurityException("Could not initialize CryptoLibrary: " + e.getMessage());
    78. }
    79. }
    80.  
    81. /**
    82. *
    83. * method to decrypt a string.
    84. *
    85. * @param str
    86. * Description of the Parameter
    87. *
    88. * @return String the encrypted string.
    89. *
    90. * @exception SecurityException
    91. * Description of the Exception
    92. */
    93.  
    94. @SuppressWarnings("restriction")
    95. public synchronized String encrypt(String str) throws SecurityException
    96. {
    97. try
    98. {
    99. System.out.println("Encript Method Invoked");
    100.  
    101. byte[] utf8 = str.getBytes(charset);
    102.  
    103. byte[] enc = encryptCipher.doFinal(utf8);
    104.  
    105. return URLEncoder.encode(encoder.encode(enc), charset);
    106. }
    107.  
    108. catch (Exception e)
    109.  
    110. {
    111. throw new SecurityException("Could not encrypt: " + e.getMessage());
    112. }
    113. }
    114.  
    115. /**
    116. *
    117. * method to encrypting a string.
    118. *
    119. * @param str
    120. * Description of the Parameter
    121. *
    122. * @return String the encrypted string.
    123. *
    124. * @exception SecurityException
    125. * Description of the Exception
    126. */
    127.  
    128. public synchronized String decrypt(String str) throws SecurityException
    129. {
    130. try
    131. {
    132.  
    133. System.out.println("decrypt Method Invoked");
    134. @SuppressWarnings("restriction")
    135. byte[] dec = decoder.decodeBuffer(URLDecoder.decode(str, charset));
    136. byte[] utf8 = decryptCipher.doFinal(dec);
    137.  
    138. return new String(utf8, charset);
    139.  
    140. }
    141. catch (Exception e)
    142. {
    143. throw new SecurityException("Could not decrypt: " + e.getMessage());
    144. }
    145. }
    146.  
    147. @SuppressWarnings("restriction")
    148. private void setupEncryptor(String defaultEncryptionPassword, byte[] salt)
    149. {
    150.  
    151. java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());
    152.  
    153. char[] pass = defaultEncryptionPassword.toCharArray();
    154.  
    155. int iterations = 3;
    156.  
    157. init(pass, salt, iterations);
    158. }
    159.  
    160. public static void main(String[] args)
    161. {
    162.  
    163. StringEncryptor st = new StringEncryptor();
    164.  
    165. System.out.println(st.encrypt("EVON Technology Dehradun"));
    166.  
    167. System.out.println(st.defaultEncryptionPassword);
    168.  
    169. System.out.println(st.decrypt("Put your Encrypted Text here it will decrypt "));
    170. }
    171. }

    more details about Base64 Encoding and Decoding please follow below link
    http://en.wikipedia.org/wiki/Base64

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: