Created
August 11, 2022 14:05
-
-
Save YasharHND/70ffde723207f1d6bec476cba2da6151 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.Cipher; | |
import java.io.FileOutputStream; | |
import java.security.KeyFactory; | |
import java.security.KeyPair; | |
import java.security.KeyPairGenerator; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.security.spec.EncodedKeySpec; | |
import java.security.spec.PKCS8EncodedKeySpec; | |
import java.security.spec.X509EncodedKeySpec; | |
import java.util.Base64; | |
import java.util.UUID; | |
public class KeyPairEncryption { | |
private static final String PUBLIC_KEY = | |
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0UW6nxjPgny1gE0GdKJq2pTfXrVJuJYtefbDRB8+DF1BIUOtIBZYkI0drYHl+3SU6v94qbN0BGqLFs6Kv67A0UxRri1dsVv7zGadWkGsqLxXWsyqg2YzHNK1l4R0UXGSU6CIz8yu7N6me0U8JKDsFdtd2sp23LyhjLbMv4RJVtN0+2J8JmBhST4w2Fj82q4QYDVn3g+rx5lq1obNBgyXNwdUx5/8F2u36ygbetKvkJyxU4d5ChZY0Bk7relo+BJm9PGOrJqq3ojR3+XXsFJx7Dymp4iR3Fz9gYXCl6T/MDbsxl+FNtEIY2qDnQfZIM+n4ordoKZAjTATZ52m02NRZwIDAQAB"; | |
private static final String PRIVATE_KEY = | |
"MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDRRbqfGM+CfLWATQZ0omralN9etUm4li159sNEHz4MXUEhQ60gFliQjR2tgeX7dJTq/3ips3QEaosWzoq/rsDRTFGuLV2xW/vMZp1aQayovFdazKqDZjMc0rWXhHRRcZJToIjPzK7s3qZ7RTwkoOwV213aynbcvKGMtsy/hElW03T7YnwmYGFJPjDYWPzarhBgNWfeD6vHmWrWhs0GDJc3B1THn/w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tp1QhbuvLnzRAG6j14x1m9MNt4SbpX9YmgLGiQWWkKYw79YzWyvmQei38oerfLQuVyIFrT+AhsH9lhLoUCV90FECgYBPtSa4O0toq1rBicZjGMTFfkFW+s8Xq529Dcus3m/Q5HzuzpOikNXYfDTClbbVdYb3Qjulx3psBoulZ8CwKoz79fE7mqSqHIG2RB6ddaTgRX60gZdKQe/um+7DAv9wbDz4JFJcaK3IoGSf9F80H8bSXcMnDA+fmOeMRKTAAK0pbw=="; | |
public static String getEncrypted(String data) throws Exception { | |
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, getPublicKeyFromMemory()); | |
byte[] encryptedBytes = cipher.doFinal(data.getBytes()); | |
return new String(Base64.getEncoder().encode(encryptedBytes)); | |
} | |
public static String getDecrypted(String data) throws Exception { | |
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); | |
cipher.init(Cipher.DECRYPT_MODE, getPrivateKeyFromMemory()); | |
byte[] encryptedBytes = cipher.doFinal(Base64.getDecoder().decode(data.getBytes())); | |
return new String(encryptedBytes); | |
} | |
public static void main(String... args) throws Exception { | |
final String message = UUID.randomUUID().toString(); | |
System.out.println(" Message: " + message); | |
String encrypted = getEncrypted(message); | |
System.out.println("ENCRYPTED: " + encrypted); | |
String decrypted = getDecrypted(encrypted); | |
System.out.println("DECRYPTED: " + decrypted); | |
} | |
private static void generateKeyPair() throws Exception { | |
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); | |
generator.initialize(2048); | |
KeyPair pair = generator.generateKeyPair(); | |
PrivateKey privateKey = pair.getPrivate(); | |
PublicKey publicKey = pair.getPublic(); | |
try (FileOutputStream fos = new FileOutputStream("exchange_token_server.pub")) { | |
fos.write(Base64.getEncoder().encodeToString(publicKey.getEncoded()).getBytes()); | |
} | |
try (FileOutputStream fos = new FileOutputStream("exchange_token_server.key")) { | |
fos.write(Base64.getEncoder().encodeToString(privateKey.getEncoded()).getBytes()); | |
} | |
} | |
private static PublicKey getPublicKeyFromMemory() throws Exception { | |
return KeyFactory.getInstance("RSA") | |
.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(PUBLIC_KEY))); | |
} | |
private static PrivateKey getPrivateKeyFromMemory() throws Exception { | |
return KeyFactory.getInstance("RSA") | |
.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(PRIVATE_KEY))); | |
} | |
private static PublicKey getPublicKeyFromFile() throws Exception { | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
byte[] publicKeyBytes = KeyPairEncryption.class.getClassLoader().getResourceAsStream("exchange_token_server.pub").readAllBytes(); | |
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyBytes)); | |
return keyFactory.generatePublic(publicKeySpec); | |
} | |
private static PrivateKey getPrivateKeyFromFile() throws Exception { | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
byte[] privateKeyBytes = KeyPairEncryption.class.getClassLoader().getResourceAsStream("exchange_token_server.key").readAllBytes(); | |
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyBytes)); | |
return keyFactory.generatePrivate(privateKeySpec); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment