org.bouncycastle.openpgp.PGPSessionKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpg-jdk14 Show documentation
Show all versions of bcpg-jdk14 Show documentation
The Bouncy Castle Java API for handling the OpenPGP protocol. This jar contains the OpenPGP API for JDK 1.4. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.
package org.bouncycastle.openpgp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bouncycastle.util.encoders.Hex;
public class PGPSessionKey
{
private final int algorithm;
private final byte[] sessionKey;
public PGPSessionKey(int algorithm, byte[] sessionKey)
{
this.algorithm = algorithm;
this.sessionKey = sessionKey;
}
public int getAlgorithm()
{
return algorithm;
}
public byte[] getKey()
{
byte[] copy = new byte[sessionKey.length];
System.arraycopy(sessionKey, 0, copy, 0, sessionKey.length);
return copy;
}
public String toString()
{
// mote: we only print the reference to sessionKey to prevent accidental disclosure of the actual key value.
return algorithm + ":" + sessionKey;
}
public static PGPSessionKey fromAsciiRepresentation(String ascii)
{
Pattern pattern = Pattern.compile("(\\d{1,3}):([0-9A-Fa-f]+)");
Matcher matcher = pattern.matcher(ascii);
if (!matcher.matches())
{
throw new IllegalArgumentException("Provided ascii encoding does not match expected format : ");
}
String alg = matcher.group(1);
String hexKey = matcher.group(2);
return new PGPSessionKey(Integer.parseInt(alg), Hex.decode(hexKey));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy