Created
February 14, 2022 03:03
-
-
Save wicksome/309ad433392db38ebcf9a2da5427b1d4 to your computer and use it in GitHub Desktop.
RSA Example
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
package com.example.rsa; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import java.nio.charset.StandardCharsets; | |
import java.security.*; | |
import java.security.spec.InvalidKeySpecException; | |
import java.security.spec.PKCS8EncodedKeySpec; | |
import java.security.spec.X509EncodedKeySpec; | |
import java.util.Base64; | |
public class Rsa { | |
private static final String ALGORITHM = "RSA"; | |
private static final String TRANSFORMATION = "RSA/ECB/PKCS1Padding"; | |
private static final Base64.Encoder ENCODER = Base64.getEncoder(); | |
private static final Base64.Decoder DECODER = Base64.getDecoder(); | |
public static KeyPair generateKeyPair(final Integer keySize) { | |
try { | |
final KeyPairGenerator instance = KeyPairGenerator.getInstance(ALGORITHM); | |
instance.initialize(keySize == null ? 2024 : keySize); | |
return instance.genKeyPair(); | |
} catch (NoSuchAlgorithmException e) { | |
// should not happen | |
throw new RuntimeException(e); | |
} | |
} | |
public static String encrypt(String publicKey, String data) throws InvalidKeySpecException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { | |
final Cipher cipher; | |
try { | |
// Cipher is not thread safe | |
cipher = Cipher.getInstance(TRANSFORMATION); | |
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) { | |
// should not happen | |
throw new RuntimeException(e); | |
} | |
// key setting | |
final PublicKey key = convertPublicKey(publicKey); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
// encrypt | |
final byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); | |
return new String(ENCODER.encode(result), StandardCharsets.UTF_8); | |
} | |
public static String decrypt(String privateKey, String data) throws InvalidKeySpecException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { | |
final Cipher cipher; | |
try { | |
// Cipher is not thread safe | |
cipher = Cipher.getInstance(TRANSFORMATION); | |
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) { | |
// should not happen | |
throw new RuntimeException(e); | |
} | |
// key setting | |
final PrivateKey key = convertPrivateKey(privateKey); | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
// encrypt | |
final byte[] result = cipher.doFinal(DECODER.decode(data.getBytes(StandardCharsets.UTF_8))); | |
return new String(result, StandardCharsets.UTF_8); | |
} | |
private static PublicKey convertPublicKey(final String publicKey) throws InvalidKeySpecException { | |
final byte[] publicKeyBytes = DECODER.decode(publicKey.getBytes(StandardCharsets.UTF_8)); | |
final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); | |
try { | |
return KeyFactory.getInstance(ALGORITHM).generatePublic(keySpec); | |
} catch (NoSuchAlgorithmException e) { | |
// should not happen | |
throw new RuntimeException(e); | |
} | |
} | |
private static PrivateKey convertPrivateKey(final String privateKey) throws InvalidKeySpecException { | |
final byte[] publicKeyBytes = DECODER.decode(privateKey.getBytes(StandardCharsets.UTF_8)); | |
final PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(publicKeyBytes); | |
try { | |
return KeyFactory.getInstance(ALGORITHM).generatePrivate(keySpec); | |
} catch (NoSuchAlgorithmException e) { | |
// should not happen | |
throw new RuntimeException(e); | |
} | |
} | |
} |
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
package com.example.rsa; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.IllegalBlockSizeException; | |
import java.nio.charset.StandardCharsets; | |
import java.security.InvalidKeyException; | |
import java.security.KeyPair; | |
import java.security.spec.InvalidKeySpecException; | |
import java.util.Base64; | |
public class HelloWorld { | |
public static void main(String []args) throws IllegalBlockSizeException, InvalidKeySpecException, BadPaddingException, InvalidKeyException { | |
final String text = "Hello World"; | |
final KeyPair keyPair = Rsa.generateKeyPair(2024); | |
final String publicKey = new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded()), StandardCharsets.UTF_8); | |
final String privateKey = new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded()), StandardCharsets.UTF_8); | |
final String encrypted = Rsa.encrypt(publicKey, text); | |
final String decrypted = Rsa.decrypt(privateKey, encrypted); | |
System.out.println(text); | |
System.out.println(encrypted); | |
System.out.println(decrypted); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment