/**
  * Converts a PEM-encoded PKCS#8 private key into an RSAPrivateKey instance useable with JCE
  *
  * @param privateKey private key in PKCS#8 format and PEM-encoded
  * @return RSAPrivateKey instance containing the key
  */
 private static RSAPrivateKey rsaPrivateKeyDecode(final String privateKey) {
   try {
     final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
     final KeySpec ks = new PKCS8EncodedKeySpec(Base64.decodePadded(privateKey));
     return (RSAPrivateKey) keyFactory.generatePrivate(ks);
   } catch (final Exception e) {
     throw new RuntimeException("Failed to decode built-in private key", e);
   }
 }
Esempio n. 2
0
  // encrypts string
  public String encrypt(String toEncrypt) {
    try {
      // encrypt
      byte[] bytes = toEncrypt.getBytes("ASCII");
      byte[] encoded = encryptionCipher.doFinal(bytes);

      return Base64.encode(encoded);
    } catch (Exception e) {
      System.out.println("Encryption Error: " + e);
    }
    return null;
  }
Esempio n. 3
0
  // decrypts string
  public String decrypt(String toDecrypt) {
    try {
      // see the above comment for rant, replacing Encoder with Decoder... kthxbye
      // note: "above comment" no longer exists
      byte[] decoded = Base64.decode(toDecrypt);

      // decrypt
      byte[] bytes = decryptionCipher.doFinal(decoded);
      return new String(bytes, "ASCII");
    } catch (Exception e) {
      System.out.println("Decryption Error: " + e);
      e.printStackTrace();
    }
    return null;
  }