I made simple encryption and decryption methods by following this video: http://www.youtube.com/watch?v=8AID7DKhSoM&feature=g-hist however when it is implemented in my program, any character above "s" is encrypted to "? " and then decrypts to 3. This does not seem to happen in the textbook, although some of his characters increase by large numbers. So why is this happening? Btw here is an important part of my program:
public class crypt { public String encrypt(String pass){ String cryppass_string=""; int l= pass.length(); char ch; for (int i=0; i<l; i++){ ch= pass.charAt(i); ch+= 12; cryppass_string+= ch; } return cryppass_string; } public String decrypt (String cryppass_string){ String pass_string= ""; int l= cryppass_string.length(); char ch; for (int i=0; i<l; i++){ ch= cryppass_string.charAt(i); ch-= 12; pass_string += ch; } return pass_string; } }
Here is an example: a password ("astu") must be encrypted to be entered, this is done:
char[] newpass= newPassField.getPassword(); char[] repass= rePassField.getPassword(); if(Arrays.equals( newpass , repass )) { if(number==1) { Login_info.McIntosh_custom_pwd= fileob.string_to_char(cryptob.encrypt(fileob.char_to_string(newpass))); fileob.evr_tofile(); }
In another class, McIntoshcrypted is declared as:
McIntosh_custom_pwd= fileob.string_to_char(cryptob.decrypt(FileData[0]));
fileob is an object of class Files
cryptob is an object of the crypt class
public class Files { File f= new File("Eng Dept.txt"); public Formatter x; public void openfile(){ try{ x= new Formatter ("Eng Dept.txt"); } catch (Exception error){ System.out.println("error"); } } public void writing(String towrite){ try{ String filename= "Eng Dept.txt"; String newLine = System.getProperty("line.separator"); FileWriter fw = new FileWriter(filename,true); fw.write(towrite); fw.write(newLine); fw.close(); } catch (Exception eror){ System.out.println("error"); } } public String reading_string(int linenum){ String readline= ""; String filename= "Eng Dept.txt"; int lineno; try{ FileReader fr= new FileReader(filename); BufferedReader br= new BufferedReader(fr); for (lineno=1; lineno<= 1000; lineno++){ if(lineno== linenum){ readline= br.readLine(); } else br.readLine(); } br.close(); } catch (Exception eror){ System.out.println("error"); } return readline; } public String char_to_string(char[] toconv){ int l= toconv.length; String converted= ""; for (int i=0; i<l; i++) { converted+= toconv[i]; } return converted; } public char[] string_to_char(String toconv){ int l= toconv.length(); char[] converted = new char[l]; for (int i= 0; i<l; i++) { converted[i]=toconv.charAt(i); } return converted; } public void evr_tofile() { f.delete(); openfile(); writing(char_to_string(Login_info.McIntosh_custom_pwd)); }
In the txt file "as ??" and result
System.out.print(Login_info.McIntosh_custom_pwd);
- "as33". I hope I explained it correctly ...
edit: proven solution
public String encrypt(String pass){ String cryppass_string=""; int l= pass.length(); int x=0; char ch; for (int i=0; i<l; i++){ ch= pass.charAt(i); x= ((ch - 32) + 12) % 126 + 32; ch = (char)x; cryppass_string+= ch; } return cryppass_string; } public String decrypt (String cryppass_string){ String pass_string= ""; int l= cryppass_string.length(); int x=0; char ch; for (int i=0; i<l; i++){ ch= cryppass_string.charAt(i); x= ch-32; ch= (char)x; if (ch < 0) x= ch+126; ch= (char)x; x= ch-12+32; ch= (char)x; pass_string += ch; } return pass_string; }