AuthStoreUtils { protected byte[] encrypt(char[] bytes, char[] password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { byte[] passBytes = charsToBytes(password); SecretKeySpec secretKeySpec = new SecretKeySpec(passBytes, ALGORITHM_BLOW_FISH); Cipher cipher = Cipher.getInstance(ALGORITHM_BLOW_FISH); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encrypted = cipher.doFinal(new String(bytes).getBytes("UTF-8")); return encrypted; } AuthStoreUtils(PasswordProvider passwordProvider); void save(AuthStore authStore, OutputStream out); void load(AuthStore authStore, InputStream in); PasswordProvider getPasswordProvider(); void setPasswordProvider(PasswordProvider passwordProvider); static final String USER; static final String HOST; static final String PROTOCOL; static final String ENTRY; static final String USER_AUTHENTICATION_DATA; static final String TYPE; static final String DATA; static final String ALGORITHM_BLOW_FISH; static final int SALT_LENGTH; }
@Test public void testEncrypt() throws IllegalBlockSizeException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, UnsupportedEncodingException { String password = "alma"; String message = "text"; byte[] encode = authStoreUtils.encrypt(message.toCharArray(), password.toCharArray()); System.out.flush(); byte[] decode = authStoreUtils.decrypt(encode, password.toCharArray()); assertEquals(new String(decode), message); }