Java Code Examples for java.security.Key#getFormat()

The following examples show how to use java.security.Key#getFormat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PEMEncoder.java    From fusionauth-jwt with Apache License 2.0 6 votes vote down vote up
private void addOpeningTag(Key key, StringBuilder sb) {
  String format = key.getFormat();
  if (key instanceof PrivateKey) {
    if (format.equals("PKCS#1")) {
      sb.append(PEM.PKCS_1_PRIVATE_KEY_PREFIX).append("\n");
    } else if (format.equals("PKCS#8")) {
      sb.append(PEM.PKCS_8_PRIVATE_KEY_PREFIX).append("\n");
    } else {
      throw new PEMEncoderException(
          new InvalidParameterException("Unexpected Private Key format, expecting PKCS#1 or PKCS#8 but found " + format + "."));
    }
  } else {
    if (format.equals("X.509")) {
      sb.append(PEM.X509_PUBLIC_KEY_PREFIX).append("\n");
    } else {
      throw new PEMEncoderException(
          new InvalidParameterException("Unexpected Public Key format, expecting X.509 but found " + format + "."));
    }
  }
}
 
Example 2
Source File: SignatureKeyImpl.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
public SignatureKeyImpl(Key publicKey) {
  this(publicKey.getEncoded(), publicKey.getAlgorithm(), publicKey.getFormat());
}
 
Example 3
Source File: HookCryptoImpl.java    From Introspy-Android with GNU General Public License v2.0 4 votes vote down vote up
public void execute(Object... args) {
	// let's not care about init since we are hooking 
	// the key class already
	// BUT it can be useful to get a state of the mode 
	// if needed later
	if (_resources != null) {
		try {
			_lastCipher = (Cipher) _resources;
			
			// get the mode
			Integer mode = _lastMode = (Integer) args[0];
			String smode;
			switch (mode) {
				case Cipher.DECRYPT_MODE: 
					smode = "DECRYPT_MODE";
					break;
				case Cipher.ENCRYPT_MODE: 
					smode = "ENCRYPT_MODE";
					break;
				default: 
					smode = "???";
			}
			
			_logBasicInfo();
			
			String out = "-> Mode: " + smode;
			
			// get the key
			Key key = (Key) args[1];
			String skey = "";
			if (key != null) {
				skey = _getReadableByteArr(key.getEncoded());
				out += ", Key format: " + key.getFormat() + 
						", Key: [" + skey + "]";
			}
			_logParameter("Mode", smode);
			_logParameter("Key", skey);
			_logParameter("Key Format", key.getFormat());

			_logFlush_I(out);
			
		} catch (Exception e) {
			Log.w(_TAG_ERROR, "Error in Intro_CRYPTO: " + e);
		}
	}
}