Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. public final class Activation {
  2. public static String getId(){
  3. //pobiera id urządzenia i go hashuje SHA-256
  4. String deviceId = getDeviceId();
  5. if(deviceId!=null) return hash256(deviceId);
  6. else return null;
  7. }
  8. public static String getDeviceId() {
  9. //wykorzystanie biblioteki Apache do stwierdzenia systemu
  10. if(SystemUtils.IS_OS_WINDOWS){
  11. return WindowsID.getSerialNumber();
  12. }
  13. else return null;
  14. }
  15. public static String hash256(String data) {
  16. try {
  17. MessageDigest md = MessageDigest.getInstance("SHA-256");
  18. md.update(data.getBytes());
  19. return bytesToHex(md.digest());
  20. } catch (NoSuchAlgorithmException e) {
  21. e.printStackTrace();
  22. }
  23. return null;
  24. }
  25. public static String bytesToHex(byte[] bytes) {
  26. StringBuffer result = new StringBuffer();
  27. for (byte byt : bytes) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
  28. return result.toString();
  29. }
  30. public static String decrypt(byte[] keyFromServer, byte[] keyPublic)
  31. throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
  32. InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  33. //odszyfrowywuje zbiór bajtów z klucza weryfikacyjnego
  34. //keyFromServer - klucz weryfikacyjny
  35. //keyPublic - klucz publiczny RSA
  36. //zwracany jest odszyfrowany klucz, już w formie łańcucha znaków
  37. X509EncodedKeySpec spec = new X509EncodedKeySpec(keyPublic);
  38. KeyFactory kf = KeyFactory.getInstance("RSA");
  39. RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(spec);
  40.  
  41. Cipher cipher = Cipher.getInstance("RSA");
  42. cipher.init(Cipher.DECRYPT_MODE, pubKey);
  43.  
  44. byte[] decodedText = cipher.doFinal(keyFromServer);
  45. return new String(decodedText);
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement