java中的加密/解密

时间:2013-11-21 04:18:10

标签: java encryption

嗨我在做了一些谷歌之后找到了一些代码,我使用这段代码来Encrypt字符串(我在web-service中设置为参数) 并且它工作正常,我很难理解这段代码所以把孔类放入。

public class RSA {
    Vector<Object> vectEnc;
    Object enc[];
    private long P, Q;
    private long N, M, E = 11;
    private long D;
    public RSA() {
        P = 6151;
        Q = 8807;
        N = P * Q;
        M = (P - 1) * (Q - 1);
        E = 11;
        D = 44310191;
        vectEnc = new Vector<Object>();
    }
    public String doEncryption(String message) {
        try {
            String str = new BASE64Encoder().encode(message.getBytes("UTF-8"));
            String encString = "";
            for (int i = 0; i < str.length(); i += 3) {
                String tempAsci = "1";
                String tempStr;
                for (int h = 0; h < 3; h++) {
                    int total = i + h;
                    if (total < str.length()) {
                        tempStr = String.valueOf((int) (str.subSequence(total,
                                total + 1).charAt(0)) - 30);
                        if (tempStr.length() < 2) {
                            tempStr = "0" + tempStr;
                        }
                    } else {
                        break;
                    }
                    tempAsci = tempAsci + tempStr;
                }
                vectEnc.add(tempAsci + "1");
            }
            enc = vectEnc.toArray();
            vectEnc.removeAllElements();
            for (int i = 0; i < enc.length; i++) {
                long base = Long.parseLong(enc[i].toString());
                long powMod = powMod(base, E, N);
                encString = encString + String.valueOf(powMod) + " ";
            }
            return encString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public String doDecryption(String codeMsg) {
        String[] decryptArray = codeMsg.split(" ");
        String decryptStr = "";
        String originalStr = "";
        for (int i = 0; i < decryptArray.length; i++) {
            long base = Long.parseLong(decryptArray[i]);
            long powMod = powMod(base, D, N);
            String powModString = String.valueOf(powMod);
            decryptStr = decryptStr
                    + powModString.subSequence(1, powModString.length() - 1);
        }
        for (int i = 0; i < decryptStr.length(); i += 2) {
            char ch = (char) (Integer.parseInt(decryptStr.subSequence(i, i + 2)
                    .toString()) + 30);
            originalStr = originalStr + ch;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] decBytes = null;
        try {
            decBytes = decoder.decodeBuffer(originalStr);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String decodeStr = new String(decBytes);
        return decodeStr;
    }
    public long powMod(long base, long exp, long modula) {
        long accum = 1;
        int i = 0;
        long base2 = base;
        while ((exp >> i) > 0) {
            if (((exp >> i) & 1) == 1) {
                accum = mo((accum * base2), modula);
            }
            base2 = mo((base2 * base2), modula);
            i++;
        }
        return accum;
    }
    public long mo(long g, long l) {
        return (long) (g - (l * Math.floor(g / l)));
    }
}

但问题是当字符串长度超过56它会抛出异常

java.lang.NumberFormatException: For input string: "174-17-201"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at java.lang.Long.parseLong(Unknown Source)
    at com.info.test.RSA.doEncryption(RSA.java:49)
    at com.info.test.Test.main(Test.java:56)

我甚至没有这个代码使用的算法是什么,我做了一些Google我发现简单的解决方案是做一个字符串的一部分并做Encryption就像这样。

int MAX_LAN = 55;
List<String> splitEqually  = splitEqually(string,MAX_LAN);
String encodeString = ""; 
for (int i = 0; i < splitEqually.size(); i++) {
    encodeString +=rsa.doEncryption(splitEqually.get(i));
}
System.out.println(encodeString);
public static List<String> splitEqually(String text, int size) {
    List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);
    for (int start = 0; start < text.length(); start += size) {
        ret.add(text.substring(start, Math.min(text.length(), start + size)));
    }
    return ret;
}

它工作正常,所以它是否适当的方法??

1 个答案:

答案 0 :(得分:0)

我强烈建议使用Java的内置加密库。请按照以下系列文章介绍如何在Java中执行RSA加密/解密:

http://www.javamex.com/tutorials/cryptography/rsa_encryption.shtml