Created
April 24, 2018 10:44
-
-
Save thermatk/169544d3de8953361e3823366d862119 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.io.ByteArrayInputStream; | |
import java.security.*; | |
import java.security.interfaces.RSAPublicKey; | |
import java.security.spec.AlgorithmParameterSpec; | |
import java.security.spec.InvalidKeySpecException; | |
import java.security.spec.X509EncodedKeySpec; | |
import java.util.Arrays; | |
import java.util.Base64; | |
public class TelegramFun { | |
private static String publicKey = "-----BEGIN RSA PUBLIC KEY-----\n" + | |
"MIIBCgKCAQEAyr+18Rex2ohtVy8sroGP\n" + | |
"BwXD3DOoKCSpjDqYoXgCqB7ioln4eDCFfOBUlfXUEvM/fnKCpF46VkAftlb4VuPD\n" + | |
"eQSS/ZxZYEGqHaywlroVnXHIjgqoxiAd192xRGreuXIaUKmkwlM9JID9WS2jUsTp\n" + | |
"zQ91L8MEPLJ/4zrBwZua8W5fECwCCh2c9G5IzzBm+otMS/YKwmR1olzRCyEkyAEj\n" + | |
"XWqBI9Ftv5eG8m0VkBzOG655WIYdyV0HfDK/NWcvGqa0w/nriMD6mDjKOryamw0O\n" + | |
"P9QuYgMN0C9xMW9y8SmP4h92OAWodTYgY1hZCxdv6cs5UnW9+PWvS+WIbkh+GaWY\n" + | |
"xwIDAQAB\n" + | |
"-----END RSA PUBLIC KEY-----"; | |
// openssl rsa -RSAPublicKey_in -in public_key.txt -pubout | |
private static String publicKeyPKCS8 = "-----BEGIN PUBLIC KEY-----\n" + | |
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyr+18Rex2ohtVy8sroGP\n" + | |
"BwXD3DOoKCSpjDqYoXgCqB7ioln4eDCFfOBUlfXUEvM/fnKCpF46VkAftlb4VuPD\n" + | |
"eQSS/ZxZYEGqHaywlroVnXHIjgqoxiAd192xRGreuXIaUKmkwlM9JID9WS2jUsTp\n" + | |
"zQ91L8MEPLJ/4zrBwZua8W5fECwCCh2c9G5IzzBm+otMS/YKwmR1olzRCyEkyAEj\n" + | |
"XWqBI9Ftv5eG8m0VkBzOG655WIYdyV0HfDK/NWcvGqa0w/nriMD6mDjKOryamw0O\n" + | |
"P9QuYgMN0C9xMW9y8SmP4h92OAWodTYgY1hZCxdv6cs5UnW9+PWvS+WIbkh+GaWY\n" + | |
"xwIDAQAB\n" + | |
"-----END PUBLIC KEY-----"; | |
private static String test = "xAUKjvvGYpmctHB79as/lAPGZzHSFWvbWSRBLP/B8OcaAMpk9oqDeXzuZ9aDOnRfTAvTTBgb1fi0baicrpUPK0qNYwMhtOpCicMda62JwWk5ogrYJ2x2L+FpDCdW+jvAkWSr9K29O8VNKw1IEL3i5FqAKJNay5/JbpVbUr3x033h1ZHTh5sGLz7LMs6J43fR4WWX2oHwpCG3e4ugOYeuclbQ69h33PqfB0y2LfU/x65TiCmpw7nT3+enOb8MxVaeEMQVCUCqSi7qsTE+mHFSVAq1v0T8h4lnQcqXAvagxc4s7HGhC0OY81ZFXbfZR8Ve8ChgSbAIslelpIFQDb1Zww=="; | |
public static void main(String[] args) { | |
print("Telegram-IP-Fun"); | |
byte[] inputBytes = Base64.getDecoder().decode(test); | |
Cipher cipher = null; | |
try { | |
cipher = Cipher.getInstance("RSA/ECB/NoPadding"); | |
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) { | |
e.printStackTrace(); | |
} | |
try { | |
cipher.init(Cipher.DECRYPT_MODE, getPublicKeyFromString(publicKeyPKCS8)); | |
} catch (InvalidKeyException e) { | |
e.printStackTrace(); | |
} | |
byte[] RSADecoded = null; | |
try { | |
RSADecoded = cipher.doFinal(inputBytes); | |
} catch (IllegalBlockSizeException | BadPaddingException e) { | |
e.printStackTrace(); | |
} | |
print("Decoded bytes: "+ RSADecoded.length); | |
byte[] AESEncoded = Arrays.copyOfRange(RSADecoded, 32, 256); | |
byte[] AESKey = Arrays.copyOfRange(RSADecoded, 0, 32); | |
byte[] AESIV = Arrays.copyOfRange(RSADecoded, 16, 32); | |
//byte[] AESEncodedSHA256 = getSHA256(AESEncoded); | |
try { | |
cipher = Cipher.getInstance("AES/CBC/NoPadding"); | |
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) { | |
e.printStackTrace(); | |
} | |
try { | |
cipher.init(Cipher.DECRYPT_MODE, getKey(AESKey), getIV(AESIV)); | |
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) { | |
e.printStackTrace(); | |
} | |
byte[] AESDecoded = null; | |
try { | |
AESDecoded = cipher.doFinal(AESEncoded); | |
} catch (IllegalBlockSizeException | BadPaddingException e) { | |
e.printStackTrace(); | |
} | |
print(AESDecoded.toString()); | |
ByteArrayInputStream in = new ByteArrayInputStream(AESIV); | |
} | |
private static RSAPublicKey getPublicKeyFromString(String key) { | |
String publicKeyPEM = key | |
.replace("-----BEGIN PUBLIC KEY-----\n", "") | |
.replace("\n-----END PUBLIC KEY-----", "") | |
.replace("\n", ""); | |
// Base64 decode data | |
byte[] encoded = Base64.getDecoder().decode(publicKeyPEM); | |
KeyFactory kf = null; | |
try { | |
kf = KeyFactory.getInstance("RSA"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
RSAPublicKey pubKey = null; | |
try { | |
pubKey = (RSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded)); | |
} catch (InvalidKeySpecException e) { | |
e.printStackTrace(); | |
} | |
return pubKey; | |
} | |
private static AlgorithmParameterSpec getIV(byte[] iv) { | |
return new IvParameterSpec(iv); | |
} | |
private static Key getKey(byte[] key) { | |
try { | |
MessageDigest md = MessageDigest.getInstance("SHA-256"); | |
byte[] mdKey = md.digest(key); | |
return new SecretKeySpec(mdKey, "AES"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
private static byte[] getSHA256(byte[] input) { | |
MessageDigest digest = null; | |
try { | |
digest = MessageDigest.getInstance("SHA-256"); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
byte[] encodedhash = digest.digest(input); | |
return encodedhash; | |
} | |
private static void print(String output) { | |
System.out.println(output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment