public class Encryption {
/**
 * @Description 加密的时候要使用的密钥,这个方法就是生成一个密钥并保存在文件中
 *              这个每次生成的密钥都不同,所以这个生成一次就行了,在写入文件的时候应该判断这个文件是否存在,这样是否更合理。
 */
private void createKey() {
try {
// 得到密钥的实例 以什么方式加密。加密的方式比较多。
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56);
SecretKey key = kg.generateKey();
// 将生成的密钥对象写入文件。
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
new FileOutputStream(new File("e:\\key.obj")));
objectOutputStream.writeObject(key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
 * @param KeyFilePath
 *            密钥Key对象的路径。注意使用该方法的时候,确保你已经生成了密钥。
 * @return
 * @Description 从文件中读出Key,用于加密使用。
 */
private static Key getKey(String KeyFilePath) {
Key key = null;
try {
// 将生成的密钥对象从文件中读取出来,然后再强制转换成一个密钥对象。
ObjectInputStream objectInputStream = new ObjectInputStream(
new FileInputStream(new File(KeyFilePath)));
key = (Key) objectInputStream.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return key;
}      /**
           加密
        **/
public  byte[] encrypt(String source) {
byte[] target = null;
try {
byte[] center = source.getBytes("UTF-8");
Key key = getKey("e:\\key.obj");
Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); target = cipher.doFinal(center);

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}

return target;
}
/**解密*/
public  byte[] decrypt(byte[] source) {
byte[] dissect = new byte[32];
try {
Key key = getKey("e:\\key.obj");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);// 使用私钥解密
dissect = cipher.doFinal(source);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return dissect;
}

解决方案 »

  1.   

    太着急了 问题都还没说。现在我已经将字符串加密好了, 返回的一个字节数组,我通过IO流写入到文件中了。
    然后我再从文件中读出byte[] 数组,用来解密。问题一、解密算法写对了么?
    问题二、解密后返回的是一个byte[]  如何转换成原来的字符串。谢谢了!
      

  2.   

    解密算法 对不对  自己跑一遍new String(byte[] byte)
      

  3.   


    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Encryption e = new Encryption();
    e.createKey();
    byte[] a = e.encrypt("csdner");
    byte[] b = e.decrypt(a);

    System.out.println(new String(b));

    }
      

  4.   

    中文都没看懂 =。=!
    解密完了 new String(decrypt(byte[] source));
    然后输出就行了你这是用的什么加密算法?