I made simple encryption and decryption methods by following this video: http://www.youtube.com/watch?v=8AID7DKhSoM&feature=g-hist however when its implemented in my program any character higher than "s" is encrypted to a "?" and then decrypted to a 3. It doesn't seem to happen in the tutorial however even though some of his characters get increased by large numbers. So why does this happen? Btw here's the relevant 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's an example : a password ("astu") needs to be encrypted so its 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 class crypt
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??" is seen, and the result of
System.out.print(Login_info.McIntosh_custom_pwd);
is "as33". Hope I explained this correctly...
edit: tried 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;
}