Encrypting and Decrypting User Inputs Using Java


This tutorial is all about Encrypting and Decrypting User Inputs Using Java. This tutorial will help you on how you can create a program that encrypt and decrypt the user inputs.

What is encrypt and decrypt? The encryption is a process in which the user inputs is converted into multiple types of characters so that the vulnerable users cannot understand what the value is. The decryption is a reverse mechanism, it means that the encrypted value will be converted to the original value and it is understandable by the users.

This program uses two methods, one for the encrypt function and one for the decrypt function. Please follow all the steps to complete this tutorial.

Encrypting and Decrypting User Input Using Java

  1. Create a new Form inside your source package.

2. Design your form just look like the image below. The form is uses java label, and text field element.

3. Insert the following codes below inside your class. The following codes compose of two methods that handles encrypt and decrypt functions.

  1. //encrypting value
  2. public static String encrypt(String key) {
  3. String result = "";
  4. int l = key.length();
  5. char ch;
  6. for(int i = 0; i < l; i++){
  7. ch = key.charAt(i);
  8. ch += 10;
  9. result += ch;
  10. }
  11. return result;
  12. }
  13. //decrypting value
  14. public static String decrypt(String key) {
  15. String result = "";
  16. int l = key.length();
  17. char ch;
  18. for(int i = 0; i < l; i++){
  19. ch = key.charAt(i);
  20. ch -= 10;
  21. result += ch;
  22. }
  23. return result;
  24. }

4. Right click your user input text field and select Events > Key > KeyReleased to generate the key released method.

  1. private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
  2. }

5. Insert the codes below inside your new created key released method.

  1. //for encryption
  2. String en = encrypt(jTextField1.getText());
  3. jTextField2.setText(en);
  4. //for decryption
  5. String de = decrypt(jTextField2.getText());
  6. jTextField3.setText(de);

6. Run your program and the output should look like the image below.

Encrypting and Decrypting values are very helpful to all programs that need security. Some programmers use the encrypt and decrypt features in the login function and user registration function. It gives security to a program and avoids any vulnerable users.

After completing this tutorial, you are now learned and understand on how your program equipped with encrypt and decrypt features needed in your program and database security. If you have questions and suggestions regarding the encrypt and decrypt features, feel free to use our contact information.

Related Articles You May Like:

Facebook Comments


Please enter your comment!
Please enter your name here