Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pbkdf2encoder;
- import java.math.BigInteger;
- import java.security.GeneralSecurityException;
- import java.util.Scanner;
- //importy dla pbkdf2
- import javax.crypto.spec.PBEKeySpec;
- import javax.crypto.SecretKeyFactory;
- import java.util.Base64;
- public class PBKDF2Encoder
- {
- public static void main(String[] args) throws GeneralSecurityException
- {
- Encode();
- }
- public static String toHex(byte[] hash)
- {
- BigInteger bi = new BigInteger(1, hash);
- String hex = bi.toString(16);
- return hex;
- }
- public static byte[] PBKDF2(String paswd, byte[] salt, int pLength, int i)
- throws GeneralSecurityException
- {
- PBEKeySpec keySpec = new PBEKeySpec(paswd.toCharArray(), salt, i, pLength * 4);
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
- byte[] hash = keyFactory.generateSecret(keySpec).getEncoded();
- return hash;
- }
- public static void Encode() throws GeneralSecurityException
- {
- String paswd, acc ,id;
- int pLength, ver;
- Scanner scanner = new Scanner(System.in);
- System.out.print("Hasło: ");
- paswd = scanner.nextLine();
- System.out.print("Konto: ");
- acc = scanner.nextLine();
- System.out.print("Identyfikator: ");
- id = scanner.nextLine();
- System.out.print("Długość: ");
- pLength = scanner.nextInt();
- System.out.print("Numer wersji: ");
- ver = scanner.nextInt();
- // SecureRandom rnd = new SecureRandom();
- // byte[] salt = new byte[24];
- // rnd.nextBytes(salt);
- byte[] salt = (acc+id+ver).getBytes();
- byte[] hash = PBKDF2(paswd, salt, pLength, 1000);
- String hex = toHex(hash);
- String b64 = Base64.getEncoder().withoutPadding().encodeToString(hex.getBytes());
- char[] encd = new char[pLength];
- char[] b64c = b64.toCharArray();
- for(int i=0; i<pLength;i++)
- encd[i] = (char)(((int)b64c[i])+3);
- System.out.print("Zakodowane hasło: ");
- for(char e : encd)
- System.out.print(e);
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement