RsaKeys.java

  1. package cn.home1.oss.lib.common.crypto;

  2. import static cn.home1.oss.lib.common.crypto.CryptoConstants.COLON;
  3. import static cn.home1.oss.lib.common.crypto.RsaKey.KEY_FORMAT_PKCS1;
  4. import static cn.home1.oss.lib.common.crypto.RsaKey.KEY_FORMAT_PKCS8;
  5. import static cn.home1.oss.lib.common.crypto.RsaKey.KEY_FORMAT_PKCS8_X509;
  6. import static cn.home1.oss.lib.common.crypto.RsaKey.KEY_FORMAT_X509;
  7. import static cn.home1.oss.lib.common.crypto.RsaKey.KEY_TYPE_PAIR;
  8. import static cn.home1.oss.lib.common.crypto.RsaKey.KEY_TYPE_PRIVATE;
  9. import static cn.home1.oss.lib.common.crypto.RsaKey.KEY_TYPE_PUBLIC;
  10. import static cn.home1.oss.lib.common.crypto.RsaKey.extractPrivateKey;
  11. import static cn.home1.oss.lib.common.crypto.RsaKey.extractPublicKey;
  12. import static cn.home1.oss.lib.common.crypto.RsaKey.keySize;
  13. import static cn.home1.oss.lib.common.crypto.RsaKey.keySpec;
  14. import static cn.home1.oss.lib.common.crypto.RsaKeyGenerator.pem;
  15. import static java.nio.charset.StandardCharsets.US_ASCII;
  16. import static lombok.AccessLevel.PRIVATE;
  17. import static org.apache.commons.io.FileUtils.writeStringToFile;

  18. import cn.home1.oss.lib.common.CodecUtils;

  19. import lombok.NoArgsConstructor;
  20. import lombok.SneakyThrows;

  21. import java.io.File;

  22. /**
  23.  * Created by zhanghaolun on 16/11/13.
  24.  */
  25. @NoArgsConstructor(access = PRIVATE)
  26. public abstract class RsaKeys {

  27.   public static String generateRsaKey(final int keySize) {
  28.     final String spec = RsaKey.keySpec(KEY_FORMAT_PKCS8_X509, keySize, KEY_TYPE_PAIR);
  29.     final RsaKeyGenerator rsaKeyGenerator = new RsaKeyGenerator(spec);
  30.     final KeyExpression pairPkcs8X509 = rsaKeyGenerator.generateKey();
  31.     final KeyExpression pairPkcs1 = RsaKeyGenerator.convertPairFromPkcs8X509ToPkcs1(pairPkcs8X509);

  32.     final StringBuilder result = new StringBuilder();
  33.     //
  34.     System.err.println("privateKey PKCS8: " + writePemFile(pairPkcs8X509, KEY_FORMAT_PKCS8, KEY_TYPE_PRIVATE));
  35.     final String privateKeyPkcs1PemFile = writePemFile(pairPkcs1, KEY_FORMAT_PKCS1, KEY_TYPE_PRIVATE);
  36.     System.err.println("privateKey PKCS1: " + privateKeyPkcs1PemFile);
  37.     System.err.println("Check with command line OpenSSL that the key format is as expected:");
  38.     System.err.println("openssl rsa -in " + privateKeyPkcs1PemFile + " -noout -text");
  39.     //
  40.     System.err.println("publicKey  x509: " + writePemFile(pairPkcs8X509, KEY_FORMAT_X509, KEY_TYPE_PUBLIC));
  41.     System.err.println("publicKey PKCS1: " + writePemFile(pairPkcs1, KEY_FORMAT_PKCS1, KEY_TYPE_PUBLIC));
  42.     //
  43.     return result //
  44.         .append(pairPkcs8X509.toString()).append("\n") //
  45.         .append(pairPkcs1.toString()).append("\n") //
  46.         .append(keySpec(KEY_TYPE_PRIVATE, keySize, KEY_FORMAT_PKCS1)).append(COLON) //
  47.         .append(extractPrivateKey(pairPkcs1)).append("\n") //
  48.         .append(keySpec(KEY_TYPE_PRIVATE, keySize, KEY_FORMAT_PKCS8)).append(COLON) //
  49.         .append(extractPrivateKey(pairPkcs8X509)).append("\n") //
  50.         .append(keySpec(KEY_TYPE_PUBLIC, keySize, KEY_FORMAT_PKCS1)).append(COLON) //
  51.         .append(extractPublicKey(pairPkcs1)).append("\n") //
  52.         .append(keySpec(KEY_TYPE_PUBLIC, keySize, KEY_FORMAT_X509)).append(COLON) //
  53.         .append(extractPublicKey(pairPkcs8X509)) //
  54.         .toString();
  55.   }

  56.   public static File keyFile(final String keyFormat, final int keySize, final String keyType) {
  57.     //final String targetDirectory = System.getProperty("java.io.tmpdir", "/tmp");
  58.     final String targetDirectory = System.getProperty("user.dir", "/tmp");
  59.     return new File(targetDirectory + "/" + keySpec(keyType, keySize, keyFormat) + ".pem");
  60.   }

  61.   @SneakyThrows
  62.   public static String writePemFile(final KeyExpression pair, final String keyFormat, final String keyType) {
  63.     final int keySize = keySize(pair.getSpec());
  64.     final File pemFile = keyFile(keyFormat, keySize, keyType);
  65.     final byte[] bytes = CodecUtils.decodeBase64(KEY_TYPE_PRIVATE.equals(keyType) ? //
  66.         extractPrivateKey(pair) : extractPublicKey(pair));
  67.     writeStringToFile(pemFile, pem(bytes, keyFormat, keyType), US_ASCII);
  68.     return pemFile.getPath();
  69.   }
  70. }