本文整理匯總了Java中java.security.spec.EncodedKeySpec類的典型用法代碼示例。如果您正苦於以下問題:Java EncodedKeySpec類的具體用法?Java EncodedKeySpec怎麽用?Java EncodedKeySpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
EncodedKeySpec類屬於java.security.spec包,在下文中一共展示了EncodedKeySpec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: decodePublicKey
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Create a new PublicKey from encoded X.509 data
*/
public static PublicKey decodePublicKey(byte[] encodedKey)
{
try
{
EncodedKeySpec encodedkeyspec = new X509EncodedKeySpec(encodedKey);
KeyFactory keyfactory = KeyFactory.getInstance("RSA");
return keyfactory.generatePublic(encodedkeyspec);
}
catch (NoSuchAlgorithmException var3)
{
;
}
catch (InvalidKeySpecException var4)
{
;
}
LOGGER.error("Public key reconstitute failed!");
return null;
}
示例2: generatePublicKeyFromString
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
private static PublicKey generatePublicKeyFromString(String key, String algorithm) {
PublicKey publicKey = null;
if(key.length()>1){
key = key.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "")
.replaceAll("\\s+", "").replaceAll("\\r+", "").replaceAll("\\n+", "");
byte[] keyByteArray = java.util.Base64.getDecoder().decode(key);
try {
KeyFactory kf = KeyFactory.getInstance(algorithm);
EncodedKeySpec keySpec = new X509EncodedKeySpec(keyByteArray);
publicKey = kf.generatePublic(keySpec);
} catch (Exception e) {
ConsoleOut.output(e.getMessage());
}
}
return publicKey;
}
示例3: generatePrivateKeyFromString
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
private static PrivateKey generatePrivateKeyFromString(String key, String algorithm) {
PrivateKey privateKey = null;
if(key.length()>1){
key = key.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "")
.replaceAll("\\s+", "").replaceAll("\\r+", "").replaceAll("\\n+", "");
byte[] keyByteArray = Base64.decode(key);
try {
KeyFactory kf = KeyFactory.getInstance(algorithm);
EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyByteArray);
privateKey = kf.generatePrivate(keySpec);
} catch (Exception e) {
ConsoleOut.output(e.getMessage());
}
}
return privateKey;
}
示例4: bytesToPrivatePublicKeys
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Method convertes the bytes arrays back to private and public key objects
*/
public static Key[] bytesToPrivatePublicKeys( String algorithm, byte[] privKeyBytes, byte[] pubKeyBytes) throws Exception{
PrivateKey privKey = null;
PublicKey pubKey = null;
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
if (privKeyBytes != null) {
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privKeyBytes);
privKey = keyFactory.generatePrivate(privateKeySpec);
}
if (pubKeyBytes != null) {
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKeyBytes);
pubKey = keyFactory.generatePublic(publicKeySpec);
}
return new Key[] { privKey, pubKey };
}
示例5: sign
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
@Override
public String sign(HttpRequest request, HttpParameters requestParams) throws OAuthMessageSignerException {
byte[] decodedPrivateKey;
try {
decodedPrivateKey = Base64.decode(getConsumerSecret());
String baseString = new SignatureBaseString(request, requestParams).generate();
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_TYPE);
EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
signature.initSign(privateKey);
signature.update(baseString.getBytes());
byte[] rsasha1 = signature.sign();
return Base64.encode(rsasha1);
} catch (IOException | NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException | SignatureException e) {
throw new OAuthMessageSignerException(e);
}
}
示例6: sign
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Generates the RSA-SHA1 signature of OAuth request elements.
*
* @param baseString the combined OAuth elements to sign.
* @param secrets the secrets object containing the private key for generating the signature.
* @return the OAuth signature, in base64-encoded form.
* @throws InvalidSecretException if the supplied secret is not valid.
*/
@Override
public String sign(final String baseString, final OAuth1Secrets secrets) throws InvalidSecretException {
byte[] decodedPrivateKey;
try {
decodedPrivateKey = Base64.decode(secrets.getConsumerSecret());
} catch (IOException ioe) {
throw new InvalidSecretException(LocalizationMessages.ERROR_INVALID_CONSUMER_SECRET(ioe));
}
try {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_TYPE);
EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
signature.initSign(privateKey);
signature.update(baseString.getBytes());
byte[] rsasha1 = signature.sign();
return Base64.encode(rsasha1);
} catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException | SignatureException e) {
throw new IllegalStateException(e);
}
}
示例7: testGetEncoded
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Tests that <code>getEncoded()</code> method returns valid byte array
*/
public final void testGetEncoded() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
/* Get encoded key */
byte[] ek = meks.getEncoded();
/* Check returned array */
boolean result = true;
for (int i = 0; i < encodedKey.length; i++) {
if (encodedKey[i] != ek[i]) {
/* indicate failure */
result = false;
}
}
/* passed */
assertTrue(result);
}
示例8: testIsStatePreserved1
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Tests that internal state of the object can not be modified by modifying
* initial array value
*/
public final void testIsStatePreserved1() {
/* Create initial byte array */
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
/* Modify initial array's value */
encodedKey[3] = (byte) 5;
/* Get encoded key */
byte[] ek = meks.getEncoded();
/* Check that byte value has not been changed */
assertTrue(ek[3] == (byte) 4);
}
示例9: testIsStatePreserved2
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Tests that internal state of the object can not be modified using
* returned value of <code>getEncoded()</code> method
*/
public final void testIsStatePreserved2() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
/* Get encoded key */
byte[] ek = meks.getEncoded();
/* Modify returned value */
ek[3] = (byte) 5;
/* Get encoded key again */
byte[] ek1 = meks.getEncoded();
/* Check that byte value has not been changed */
assertTrue(ek1[3] == (byte) 4);
}
示例10: testGetEncoded
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Tests that <code>getEncoded()</code> method returns valid byte array
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "getEncoded",
args = {}
)
public final void testGetEncoded() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
/* Get encoded key */
byte[] ek = meks.getEncoded();
/* Check returned array */
boolean result = true;
for (int i = 0; i < encodedKey.length; i++) {
if (encodedKey[i] != ek[i]) {
/* indicate failure */
result = false;
}
}
/* passed */
assertTrue(result);
}
示例11: testIsStatePreserved1
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Tests that internal state of the object can not be modified by modifying
* initial array value
*/
@TestTargetNew(
level = TestLevel.PARTIAL_COMPLETE,
notes = "",
method = "getEncoded",
args = {}
)
public final void testIsStatePreserved1() {
/* Create initial byte array */
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
/* Modify initial array's value */
encodedKey[3] = (byte) 5;
/* Get encoded key */
byte[] ek = meks.getEncoded();
/* Check that byte value has not been changed */
assertTrue(ek[3] == (byte) 4);
}
示例12: testIsStatePreserved2
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Tests that internal state of the object can not be modified using
* returned value of <code>getEncoded()</code> method
*/
@TestTargetNew(
level = TestLevel.PARTIAL_COMPLETE,
notes = "",
method = "getEncoded",
args = {}
)
public final void testIsStatePreserved2() {
byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
EncodedKeySpec meks = new MyEncodedKeySpec(encodedKey);
/* Get encoded key */
byte[] ek = meks.getEncoded();
/* Modify returned value */
ek[3] = (byte) 5;
/* Get encoded key again */
byte[] ek1 = meks.getEncoded();
/* Check that byte value has not been changed */
assertTrue(ek1[3] == (byte) 4);
}
示例13: testPKCS8EncodedKeySpec
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Test for <code>PKCS8EncodedKeySpec</code> constructor<br>
* Assertion: constructs new <code>PKCS8EncodedKeySpec</code>
* object using valid parameter
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "PKCS8EncodedKeySpec",
args = {byte[].class}
)
public final void testPKCS8EncodedKeySpec() {
byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
EncodedKeySpec eks = new PKCS8EncodedKeySpec(encodedKey);
assertTrue(eks instanceof PKCS8EncodedKeySpec);
try {
eks = new PKCS8EncodedKeySpec(null);
fail("expected NullPointerException");
} catch (NullPointerException e) {
// ok
}
}
示例14: testX509EncodedKeySpec
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* Test for <code>X509EncodedKeySpec</code> constructor<br>
* Assertion: constructs new <code>X509EncodedKeySpec</code>
* object using valid parameter
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "X509EncodedKeySpec",
args = {byte[].class}
)
public final void testX509EncodedKeySpec() {
byte[] encodedKey = new byte[] {(byte)1,(byte)2,(byte)3,(byte)4};
EncodedKeySpec eks = new X509EncodedKeySpec(encodedKey);
assertTrue(eks instanceof X509EncodedKeySpec);
try {
eks = new X509EncodedKeySpec(null);
fail("expected NullPointerException");
} catch (NullPointerException e) {
// ok
}
}
示例15: getRSADecryptCipher
import java.security.spec.EncodedKeySpec; //導入依賴的package包/類
/**
* @return an RSA decryption cipher
*/
protected synchronized AsymmetricBlockCipher getRSADecryptCipher()
{
if (decodeCipher == null)
{
try
{
byte[] bytes = getEncoder().decode(privateKey);
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(bytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
PrivateKey key = keyFactory.generatePrivate(privateKeySpec);
this.decodeCipher = new PKCS1Encoding(new RSABlindedEngine());
decodeCipher.init(false, generatePrivateKeyParameter((RSAPrivateKey) key));
}
catch (Exception e)
{
throw new RuntimeException("Error constructing Cipher: ", e);
}
}
return decodeCipher;
}