This Java program code will encrypt the password before storing in database and will decrypt the same before using those details in the application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | class EncryptDecrypt{ public static void main(String arg[]){ EncryptDecrypt edObj = new EncryptDecrypt(); String str = arg[ 0 ]; String enCryptStr = edObj.Encrypt(str); System.out.println ( " Encrypted string : " + enCryptStr ); System.out.println ( " Decrypted string : " + edObj.Decrypt(enCryptStr)); } //Encryption method public String Encrypt(String passwd) { int i,len,asci_code,temp_val= 0 ; char ch; String out_str = new String(); len = passwd.length(); for (i= 0 ;i<len;i++) { ch = passwd.charAt(i); asci_code = getAscii(ch); if (i < len/ 2 ) { temp_val = (asci_code * 3 ) + 10 ; } else { temp_val = (asci_code * 4 ) + 11 ; } out_str = out_str + temp_val; } return (out_str); } private char getCharec( int i) { /*for(int x=0;x<=arr.length;x++) { if(arr[x] == i) { return (chars.charAt(x)); } } return 0;*/ return (char)i; } private int getAscii(char c){ /*for(int x=0;x<=chars.length();x++){ if(chars.charAt(x) == c) { return (arr[x]); } } return 0;*/ return (int)c; } /** * this function is to decrypt the password * @param string password */ public String Decrypt(String passwd){ int i,len,asci_code,temp_val= 0 ; String out_str = new String(); String char_str = new String(); len = passwd.length(); for (i= 0 ;i<len;i+= 3 ) { if ((i+ 3 ) <= len){ char_str = passwd.substring(i,i+ 3 ); asci_code = Integer.parseInt(char_str); if (i<(len - 2 )/ 2 ) { temp_val = ((asci_code - 10 )/ 3 ); } else { temp_val = ((asci_code - 11 )/ 4 ); } } out_str = out_str + this .getCharec(temp_val); } return (out_str); } } |