1 package org.bouncycastle.jcajce.provider.asymmetric.util; 2 3 import org.bouncycastle.asn1.ASN1Encodable; 4 import org.bouncycastle.asn1.ASN1Encoding; 5 import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; 6 import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 7 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; 8 9 public class KeyUtil 10 { getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, ASN1Encodable keyData)11 public static byte[] getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, ASN1Encodable keyData) 12 { 13 try 14 { 15 return getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(algId, keyData)); 16 } 17 catch (Exception e) 18 { 19 return null; 20 } 21 } 22 getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] keyData)23 public static byte[] getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] keyData) 24 { 25 try 26 { 27 return getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(algId, keyData)); 28 } 29 catch (Exception e) 30 { 31 return null; 32 } 33 } 34 getEncodedSubjectPublicKeyInfo(SubjectPublicKeyInfo info)35 public static byte[] getEncodedSubjectPublicKeyInfo(SubjectPublicKeyInfo info) 36 { 37 try 38 { 39 return info.getEncoded(ASN1Encoding.DER); 40 } 41 catch (Exception e) 42 { 43 return null; 44 } 45 } 46 getEncodedPrivateKeyInfo(AlgorithmIdentifier algId, ASN1Encodable privKey)47 public static byte[] getEncodedPrivateKeyInfo(AlgorithmIdentifier algId, ASN1Encodable privKey) 48 { 49 try 50 { 51 PrivateKeyInfo info = new PrivateKeyInfo(algId, privKey.toASN1Primitive()); 52 53 return getEncodedPrivateKeyInfo(info); 54 } 55 catch (Exception e) 56 { 57 return null; 58 } 59 } 60 getEncodedPrivateKeyInfo(PrivateKeyInfo info)61 public static byte[] getEncodedPrivateKeyInfo(PrivateKeyInfo info) 62 { 63 try 64 { 65 return info.getEncoded(ASN1Encoding.DER); 66 } 67 catch (Exception e) 68 { 69 return null; 70 } 71 } 72 } 73