Здравствуйте... Хочу хешировать пароли в проге... есть такой класс | Код | import java.security.InvalidKeyException; import java.security.Key; import java.security.MessageDigest; import java.util.Arrays;
import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.spec.SecretKeySpec; public class EncryptAndDecrypt { private static String algorithm = "DESede"; private static Key key = null; private static Cipher cipher = null; private static EncryptAndDecrypt obj = new EncryptAndDecrypt(); private EncryptAndDecrypt() { try { //key = KeyGenerator.getInstance(algorithm).generateKey(); /* byte[] tsu = ("testdfghdfghdfghdfghdfghGHDF").getBytes("UTF-8"); MessageDigest sha = MessageDigest.getInstance("SHA-1"); keyTemp = sha.digest(keyTemp); keyTemp = Arrays.copyOf(keyTemp, 16); // use only first 128 bit */ byte[] raw = {'4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4','4'}; key = new javax.crypto.spec.SecretKeySpec(raw, "AES");
cipher = Cipher.getInstance(algorithm); } catch (Exception e) { } } public static EncryptAndDecrypt getInstance() { return obj; } public static byte[] encrypt(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { cipher.init(Cipher.ENCRYPT_MODE, key); byte[] inputBytes = input.getBytes(); return cipher.doFinal(inputBytes); } /** * * @param input * @return * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static String getEncryptStringValue(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { return new String(encrypt(input)); } /** * * @param encryptionBytes * @return * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static String decrypt(byte[] encryptionBytes) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { cipher.init(Cipher.DECRYPT_MODE, key); byte[] recoveredBytes = cipher.doFinal(encryptionBytes); String recovered = new String(recoveredBytes); return recovered; } /** * * @param encryptionBytes * @return * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static String decrypt(String encryptionBytes) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { return EncryptAndDecrypt.decrypt(encryptionBytes.getBytes()); } }
|
Если так делать | Код |
key = KeyGenerator.getInstance(algorithm).generateKey();
|
То пашет... но каждый раз новый ключ... а я данные в базу пишу толку конечно же нету... Хотел свой задать... но не могу понять как это делать... строю методом тыка Не подскажите как свой ключ сгенерировать? А то сейчас получаю ошибку | Код | Exception in thread "main" java.security.InvalidKeyException: Invalid key length: 32 bytes at com.sun.crypto.provider.DESedeCipher.engineGetKeySize(DashoA13*..) at javax.crypto.Cipher.b(DashoA13*..) at javax.crypto.Cipher.a(DashoA13*..) at javax.crypto.Cipher.a(DashoA13*..) at javax.crypto.Cipher.a(DashoA13*..) at javax.crypto.Cipher.init(DashoA13*..) at javax.crypto.Cipher.init(DashoA13*..) at EncryptAndDecrypt.encrypt(EncryptAndDecrypt.java:43) at Main.encrypt(Main.java:195) at Main.main(Main.java:47)
|
Не могу понять какой длинны он ключ хочет Это сообщение отредактировал(а) Muerto - 3.9.2011, 12:42
|