高于“s”的字符是否被加密?

时间:2012-12-09 15:50:34

标签: java encryption

我按照以下视频制作了简单的加密和解密方法:http://www.youtube.com/watch?v=8AID7DKhSoM&feature=g-hist 但是当它在我的程序中实现时,任何高于“s”的字符都被加密为“?” 然后在教程中似乎没有发生这种情况,即使他的一些角色被大量增加了。那么为什么会这样呢? 顺便说两句,这是我的计划的相关部分:

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

这是一个例子: 密码(“astu”)需要加密才能输入,这样就完成了:

 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();
            }

在另一节课中,McIntoshcrypted被宣布为:

McIntosh_custom_pwd= fileob.string_to_char(cryptob.decrypt(FileData[0]));

fileob是类Files

的对象

cryptob是类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));

     }

在txt文件中“as ??” <和/>是看到的结果

System.out.print(Login_info.McIntosh_custom_pwd);

是“as33”。希望我能正确解释这个......

编辑:尝试解决方案

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

1 个答案:

答案 0 :(得分: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;