本文整理匯總了Java中java.security.Key.getEncoded方法的典型用法代碼示例。如果您正苦於以下問題:Java Key.getEncoded方法的具體用法?Java Key.getEncoded怎麽用?Java Key.getEncoded使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.security.Key
的用法示例。
在下文中一共展示了Key.getEncoded方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: engineWrap
import java.security.Key; //導入方法依賴的package包/類
@Override
protected synchronized byte[] engineWrap(Key key) throws IllegalBlockSizeException,
InvalidKeyException {
try {
byte[] encodedKey = key.getEncoded();
if ((encodedKey == null) || (encodedKey.length == 0)) {
throw new InvalidKeyException("Cannot get an encoding of " +
"the key to be wrapped");
}
if (encodedKey.length > buffer.length) {
throw new InvalidKeyException("Key is too long for wrapping. " +
"encodedKey.length: " + encodedKey.length +
". buffer.length: " + buffer.length);
}
return engineDoFinal(encodedKey, 0, encodedKey.length);
} catch (BadPaddingException e) {
// Should never happen for key wrapping
throw new UcryptoException("Internal Error", e);
}
}
示例2: generatePbkdf2Hash
import java.security.Key; //導入方法依賴的package包/類
/**
* generates a hash based on the <a href="http://en.wikipedia.org/wiki/PBKDF2">PKCS5S2 spec</a>
*
* Note: this has been implemented to generate hashes compatible with what JIRA generates.
* See the <a href="http://pythonhosted.org/passlib/lib/passlib.hash.atlassian_pbkdf2_sha1.html">JIRA's passlib</a>
* @param credentials the credentials
* @param algorithm the algorithm to use
* @param salt the optional salt
* @return the digested credentials
*/
private static byte[] generatePbkdf2Hash( byte[] credentials, LdapSecurityConstants algorithm, byte[] salt )
{
try
{
SecretKeyFactory sk = SecretKeyFactory.getInstance( algorithm.getAlgorithm() );
char[] password = Strings.utf8ToString( credentials ).toCharArray();
KeySpec keySpec = new PBEKeySpec( password, salt, 10000, PKCS5S2_LENGTH * 8 );
Key key = sk.generateSecret( keySpec );
return key.getEncoded();
}
catch ( Exception e )
{
throw new RuntimeException( e );
}
}
示例3: engineWrap
import java.security.Key; //導入方法依賴的package包/類
protected byte[] engineWrap(
Key key)
throws IllegalBlockSizeException, InvalidKeyException
{
byte[] encoded = key.getEncoded();
if (encoded == null)
{
throw new InvalidKeyException("Cannot wrap key, null encoding.");
}
try
{
return engineDoFinal(encoded, 0, encoded.length);
}
catch (BadPaddingException e)
{
throw new IllegalBlockSizeException(e.getMessage());
}
}
示例4: extractHFileKey
import java.security.Key; //導入方法依賴的package包/類
private static byte[] extractHFileKey(Path path) throws Exception {
HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), path,
new CacheConfig(conf), conf);
try {
reader.loadFileInfo();
Encryption.Context cryptoContext = reader.getFileContext().getEncryptionContext();
assertNotNull("Reader has a null crypto context", cryptoContext);
Key key = cryptoContext.getKey();
if (key == null) {
return null;
}
return key.getEncoded();
} finally {
reader.close();
}
}
示例5: engineGetKeySpec
import java.security.Key; //導入方法依賴的package包/類
protected KeySpec engineGetKeySpec(
Key key,
Class spec)
throws InvalidKeySpecException
{
if (spec.isAssignableFrom(PKCS8EncodedKeySpec.class) && key.getFormat().equals("PKCS#8"))
{
return new PKCS8EncodedKeySpec(key.getEncoded());
}
else if (spec.isAssignableFrom(X509EncodedKeySpec.class) && key.getFormat().equals("X.509"))
{
return new X509EncodedKeySpec(key.getEncoded());
}
throw new InvalidKeySpecException("not implemented yet " + key + " " + spec);
}
示例6: newMac
import java.security.Key; //導入方法依賴的package包/類
static Mac newMac(Key key) throws InvalidKeyException,
EncFSUnsupportedException {
Mac hmac;
try {
hmac = Mac.getInstance("HmacSHA1");
} catch (NoSuchAlgorithmException e) {
throw new EncFSUnsupportedException(e);
}
SecretKeySpec hmacKey = new SecretKeySpec(key.getEncoded(), "HmacSHA1");
hmac.init(hmacKey);
return hmac;
}
示例7: provideRandomKeyPair
import java.security.Key; //導入方法依賴的package包/類
@Override
public KeyPair provideRandomKeyPair(){
java.security.KeyPair keyPair = KEY_PAIR_GENERATOR.generateKeyPair();
Key privateKey = keyPair.getPrivate();
Key publicKey = keyPair.getPublic();
return new RsaKeyPair(privateKey.getEncoded(), publicKey.getEncoded());
}
示例8: engineGetKeySize
import java.security.Key; //導入方法依賴的package包/類
protected int engineGetKeySize(
Key key)
{
return key.getEncoded().length;
}
示例9: applyMAC
import java.security.Key; //導入方法依賴的package包/類
private void applyMAC(Key key) throws Exception {
SecretKey key2 = new SecretKey() {
@Override
public String getFormat() {
// TODO Auto-generated method stub
return key.getFormat();
}
@Override
public byte[] getEncoded() {
// TODO Auto-generated method stub
String hitesh = "This is from Hitesh";
byte[] secbytes = hitesh.getBytes();
byte[] origsecret = key.getEncoded();
byte[] ns = new byte[origsecret.length + secbytes.length];
System.arraycopy(origsecret, 0, ns, 0, origsecret.length);
System.arraycopy(secbytes, 0, ns, origsecret.length, secbytes.length);
return ns;
}
@Override
public String getAlgorithm() {
// TODO Auto-generated method stub
return key.getAlgorithm();
}
};
// Generate secret key for HMAC-MD5
// KeyGenerator kg = KeyGenerator.getInstance("HmacMD5");
// SecretKey sk = kg.generateKey();
// Get instance of Mac object implementing HMAC-MD5, and
// initialize it with the above secret key
System.out.println("Key2 secret " + toHexString(key2.getEncoded()));
Mac mac = Mac.getInstance("HmacMD5");
mac.init(key2);
byte[] result = mac.doFinal("Hi There".getBytes());
System.out.println("Message Authentication code length: " + mac.getMacLength());
System.out.println("Message Authentication code : " + toHexString(result));
verifyMacBody(mac, result);
}
示例10: engineDoPhase
import java.security.Key; //導入方法依賴的package包/類
@Override
protected Key engineDoPhase(Key key, boolean lastPhase)
throws InvalidKeyException, IllegalStateException {
byte[] pubKey = null;
if (debug.DEBUG)
log("engineDoPhase, lastPhase: " + lastPhase);
if (this.state != EngineState.WC_PRIVKEY_DONE)
throw new IllegalStateException(
"KeyAgreement object must be initialized with " +
"private key before calling doPhase");
if (lastPhase == false) {
throw new IllegalStateException(
"wolfJCE KeyAgreement currently only supports " +
"two parties and thus one single doPhase call. " +
"lastPhase must be set to true.");
}
switch (this.type) {
case WC_DH:
if (!(key instanceof DHPublicKey)) {
throw new InvalidKeyException(
"Key must be of type DHPublicKey");
}
pubKey = ((DHPublicKey)key).getY().toByteArray();
if (pubKey == null) {
throw new InvalidKeyException(
"Failed to get DH public key from Key object");
}
this.dh.setPublicKey(pubKey);
break;
case WC_ECDH:
if (!(key instanceof ECPublicKey)) {
throw new InvalidKeyException(
"Key must be of type ECPublicKey");
}
pubKey = key.getEncoded();
if (pubKey == null) {
throw new InvalidKeyException(
"Failed to get ECC public key from Key object");
}
this.ecPublic.publicKeyDecode(pubKey);
break;
};
zeroArray(pubKey);
this.state = EngineState.WC_PUBKEY_DONE;
return null;
}
示例11: engineGetKeySize
import java.security.Key; //導入方法依賴的package包/類
protected int engineGetKeySize(
Key key)
{
return key.getEncoded().length;
}
示例12: engineGetKeySpec
import java.security.Key; //導入方法依賴的package包/類
/**
* Converts a given key into a key specification, if possible. Currently the
* following specs are supported:
* <ul>
* <li>for RainbowPublicKey: X509EncodedKeySpec, RainbowPublicKeySpec
* <li>for RainbowPrivateKey: PKCS8EncodedKeySpec, RainbowPrivateKeySpec
* </ul>
*
* @param key the key
* @param keySpec the key specification
* @return the specification of the CMSS key
* @throws InvalidKeySpecException if the key type or key specification is not supported.
*/
public final KeySpec engineGetKeySpec(Key key, Class keySpec)
throws InvalidKeySpecException
{
if (key instanceof BCRainbowPrivateKey)
{
if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec))
{
return new PKCS8EncodedKeySpec(key.getEncoded());
}
else if (RainbowPrivateKeySpec.class.isAssignableFrom(keySpec))
{
BCRainbowPrivateKey privKey = (BCRainbowPrivateKey)key;
return new RainbowPrivateKeySpec(privKey.getInvA1(), privKey
.getB1(), privKey.getInvA2(), privKey.getB2(), privKey
.getVi(), privKey.getLayers());
}
}
else if (key instanceof BCRainbowPublicKey)
{
if (X509EncodedKeySpec.class.isAssignableFrom(keySpec))
{
return new X509EncodedKeySpec(key.getEncoded());
}
else if (RainbowPublicKeySpec.class.isAssignableFrom(keySpec))
{
BCRainbowPublicKey pubKey = (BCRainbowPublicKey)key;
return new RainbowPublicKeySpec(pubKey.getDocLength(), pubKey
.getCoeffQuadratic(), pubKey.getCoeffSingular(), pubKey
.getCoeffScalar());
}
}
else
{
throw new InvalidKeySpecException("Unsupported key type: "
+ key.getClass() + ".");
}
throw new InvalidKeySpecException("Unknown key specification: "
+ keySpec + ".");
}
示例13: engineGetKeySize
import java.security.Key; //導入方法依賴的package包/類
protected int engineGetKeySize(
Key key)
{
return key.getEncoded().length * 8;
}
示例14: wolfCryptSetKey
import java.security.Key; //導入方法依賴的package包/類
private void wolfCryptSetKey(Key key)
throws InvalidKeyException {
int ret = 0;
long[] idx = {0};
byte[] encodedKey;
/* validate key class type */
if (this.cipherType == CipherType.WC_RSA) {
if (key instanceof RSAPrivateKey) {
this.rsaKeyType = RsaKeyType.WC_RSA_PRIVATE;
} else if (key instanceof RSAPublicKey) {
this.rsaKeyType = RsaKeyType.WC_RSA_PUBLIC;
} else {
throw new InvalidKeyException(
"Cipher key must be of type RSAPrivateKey or " +
"RSAPublicKey when used for RSA encrypt or decrypt");
}
} else if (!(key instanceof SecretKey)) {
throw new InvalidKeyException(
"Cipher key must be of type SecretKey");
}
/* save key for class state resets */
this.storedKey = key;
/* import key */
encodedKey = key.getEncoded();
if (encodedKey == null)
throw new InvalidKeyException("Key does not support encoding");
switch (cipherType) {
case WC_AES:
if (this.direction == OpMode.WC_ENCRYPT) {
this.aes.setKey(encodedKey, iv, Aes.ENCRYPT_MODE);
} else {
this.aes.setKey(encodedKey, iv, Aes.DECRYPT_MODE);
}
break;
case WC_DES3:
if (this.direction == OpMode.WC_ENCRYPT) {
this.des3.setKey(encodedKey, iv, Des3.ENCRYPT_MODE);
} else {
this.des3.setKey(encodedKey, iv, Des3.DECRYPT_MODE);
}
break;
case WC_RSA:
/* reset key struct if needed */
if (this.rsa != null)
this.rsa.releaseNativeStruct();
this.rsa = new Rsa();
this.rsa.setRng(this.rng);
if (this.rsaKeyType == RsaKeyType.WC_RSA_PRIVATE) {
this.rsa.decodePrivateKeyPKCS8(encodedKey);
} else {
this.rsa.decodePublicKey(encodedKey);
}
break;
}
}
示例15: extractPrivateKey
import java.security.Key; //導入方法依賴的package包/類
public static byte[] extractPrivateKey(byte[] keyStoreBytes) throws Exception {
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(new ByteArrayInputStream(keyStoreBytes), keyPass.toCharArray());
Key key = keystore.getKey(alias, keyPass.toCharArray());
if (key instanceof PrivateKey) {
return key.getEncoded();
}
throw new Exception("Private key not found in key store.");
}