Below I enclose the example logic for encrypting the password:
public byte[] encryptPassword(String pass, String key) throws Exception {
DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secKey = keyFactory.generateSecret(keySpec);
byte[] cleartext = pass.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
cipher.init(Cipher.ENCRYPT_MODE, secKey);
return Base64.encodeBase64(cipher.doFinal(cleartext));
}
And to decrypt the password one can use the following method:
public byte[] decryptPassword(String encryptedPass, String key) throws Exception {
DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secKey = keyFactory.generateSecret(keySpec);
byte[] encrypedPwdBytes = Base64.decodeBase64(encryptedPass);
Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
cipher.init(Cipher.DECRYPT_MODE, secKey);
return (cipher.doFinal(encrypedPwdBytes));
}
DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secKey = keyFactory.generateSecret(keySpec);
byte[] encrypedPwdBytes = Base64.decodeBase64(encryptedPass);
Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
cipher.init(Cipher.DECRYPT_MODE, secKey);
return (cipher.doFinal(encrypedPwdBytes));
}
I hope that these code snippets would be helpful also for you.
Brak komentarzy:
Prześlij komentarz