Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class Activation {
- public static String getId(){
- //pobiera id urządzenia i go hashuje SHA-256
- String deviceId = getDeviceId();
- if(deviceId!=null) return hash256(deviceId);
- else return null;
- }
- public static String getDeviceId() {
- //wykorzystanie biblioteki Apache do stwierdzenia systemu
- if(SystemUtils.IS_OS_WINDOWS){
- return WindowsID.getSerialNumber();
- }
- else return null;
- }
- public static String hash256(String data) {
- try {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- md.update(data.getBytes());
- return bytesToHex(md.digest());
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String bytesToHex(byte[] bytes) {
- StringBuffer result = new StringBuffer();
- for (byte byt : bytes) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
- return result.toString();
- }
- public static String decrypt(byte[] keyFromServer, byte[] keyPublic)
- throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
- InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
- //odszyfrowywuje zbiór bajtów z klucza weryfikacyjnego
- //keyFromServer - klucz weryfikacyjny
- //keyPublic - klucz publiczny RSA
- //zwracany jest odszyfrowany klucz, już w formie łańcucha znaków
- X509EncodedKeySpec spec = new X509EncodedKeySpec(keyPublic);
- KeyFactory kf = KeyFactory.getInstance("RSA");
- RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(spec);
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.DECRYPT_MODE, pubKey);
- byte[] decodedText = cipher.doFinal(keyFromServer);
- return new String(decodedText);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement