4

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;
    }
4

1 回答 1

1

我猜您使用将不可打印字符转换为 的操作将值输出到文本文件(网页?)?,然后当您读回它时会发生解密问题。如果您想做类似的事情,您您需要将加密限制为仅输出可打印字符。一种方法是使用模运算来确保您的加密字符在可打印集(ASCII 32 到 ASCII 126)内。如果您读/写二进制文件,您拥有的代码将正确转换,但如果您将其输出为 ASCII 文本,则不会。

加密

ch = (char)((ch - 32) + 12) % 126 + 32; // extended expression to show rebasing/modulus

解密

ch = ch - 32;
if (ch < 0) ch = ch + 126;
ch = ch - 12 + 32;
于 2012-12-09T16:00:23.663 回答