javax.crypto.Cipher.wrap()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(145)

本文整理了Java中javax.crypto.Cipher.wrap()方法的一些代码示例,展示了Cipher.wrap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cipher.wrap()方法的具体详情如下:
包路径:javax.crypto.Cipher
类名称:Cipher
方法名:wrap

Cipher.wrap介绍

[英]Wraps a key using this cipher instance.
[中]使用此密码实例包装密钥。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

public static byte[] encodeKeyForTransmission( Key encodingKey, Key keyToEncode ) throws NoSuchAlgorithmException,
 NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException {
 Cipher cipher = Cipher.getInstance( TRANSMISSION_CIPHER_PARAMS );
 cipher.init( Cipher.WRAP_MODE, encodingKey );
 byte[] encodedKey = cipher.wrap( keyToEncode );
 return encodedKey;
}

代码示例来源:origin: apache/incubator-dubbo

keyCipher.init(Cipher.WRAP_MODE, _privateKey);
byte[] encKey = keyCipher.wrap(sharedKey);

代码示例来源:origin: apache/incubator-dubbo

keyCipher.init(Cipher.WRAP_MODE, _cert);
byte[] encKey = keyCipher.wrap(sharedKey);

代码示例来源:origin: aws/aws-sdk-java

keyWrapAlgo, provider);
cipher.init(Cipher.WRAP_MODE, kek, srand);
return new SecuredCEK(cipher.wrap(cek), keyWrapAlgo, matdesc);

代码示例来源:origin: apache/accumulo

@SuppressFBWarnings(value = "CIPHER_INTEGRITY",
  justification = "integrity not needed for key wrap")
public static byte[] wrapKey(Key fek, Key kek) {
 byte[] result = null;
 try {
  Cipher c = Cipher.getInstance(KEY_WRAP_TRANSFORM);
  c.init(Cipher.WRAP_MODE, kek);
  result = c.wrap(fek);
 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException
   | IllegalBlockSizeException e) {
  throw new CryptoException("Unable to wrap file encryption key", e);
 }
 return result;
}

代码示例来源:origin: aws-amplify/aws-sdk-android

keyWrapAlgo, p);
cipher.init(Cipher.WRAP_MODE, kek, srand);
return new SecuredCEK(cipher.wrap(cek), keyWrapAlgo, matdesc);

代码示例来源:origin: stackoverflow.com

Cipher keyEncryptionCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
for (Certificate recipient : recipients) {
 keyEncryptionCipher.init(Cipher.WRAP_MODE, recipient);
 byte[] encryptedKey = keyEncryptionCipher.wrap(contentEncryptionKey);
 /* Store the encryptedKey with an identifier for the recipient... */
}
/* Store the IV... */ 
/* Encrypt the file... */

代码示例来源:origin: stackoverflow.com

Cipher c = Cipher.getInstance("AESWrap", "SunJCE");
c.init(Cipher.WRAP_MODE, secretKey);
byte[] result = c.wrap(someKey);

代码示例来源:origin: 0xbb/otp-authenticator

/**
 * Wrap a {@link SecretKey} using the public key assigned to this wrapper.
 * Use {@link #unwrap(byte[])} to later recover the original
 * {@link SecretKey}.
 *
 * @return a wrapped version of the given {@link SecretKey} that can be
 *         safely stored on untrusted storage.
 */
public byte[] wrap(SecretKey key) throws GeneralSecurityException {
  mCipher.init(Cipher.WRAP_MODE, mPair.getPublic());
  return mCipher.wrap(key);
}

代码示例来源:origin: stackoverflow.com

public static byte[] wrapKey(PublicKey pubKey, SecretKey symKey)
    throws InvalidKeyException, IllegalBlockSizeException {
  try {
    final Cipher cipher = Cipher
        .getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
    cipher.init(Cipher.WRAP_MODE, pubKey);
    final byte[] wrapped = cipher.wrap(symKey);
    return wrapped;
  } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
    throw new IllegalStateException(
        "Java runtime does not support RSA/ECB/OAEPWithSHA1AndMGF1Padding",
        e);
  }
}

代码示例来源:origin: apptentive/apptentive-android

private static String wrapSymmetricKey(KeyPair wrapperKey, SecretKey symmetricKey) throws NoSuchPaddingException,
                                             NoSuchAlgorithmException,
                                             InvalidKeyException,
                                             IllegalBlockSizeException {
  Cipher cipher = Cipher.getInstance(WRAPPER_TRANSFORMATION);
  cipher.init(Cipher.WRAP_MODE, wrapperKey.getPublic());
  byte[] decodedData = cipher.wrap(symmetricKey);
  return Base64.encodeToString(decodedData, Base64.DEFAULT);
}

代码示例来源:origin: stackoverflow.com

KeyFactory factory rsa = KeyFactory.getInstance("RSA");
BigInteger n = ... ; /* modulus */
BigInteger e = ... ; /* public exponent */
RSAPublicKeySpec spec = new RSAPublicKeySpec(n, e);
RSAPublicKey pub = (RSAPublicKey) factory.generatePublic(spec);
Cipher enc = Cipher.getInstance("RSA");
enc.init(Cipher.WRAP_MODE, pub);
byte[] encryptedContentKey = enc.wrap(secret);

代码示例来源:origin: apache/cxf

public static byte[] wrapSecretKey(Key secretKey,
                  Key wrapperKey,
                  KeyProperties keyProps)  throws SecurityException {
  try {
    Cipher c = initCipher(wrapperKey, keyProps, Cipher.WRAP_MODE);
    return c.wrap(secretKey);
  } catch (Exception ex) {
    throw new SecurityException(ex);
  }
}

代码示例来源:origin: org.apache.cxf/cxf-rt-security

public static byte[] wrapSecretKey(Key secretKey,
                  Key wrapperKey,
                  KeyProperties keyProps)  throws SecurityException {
  try {
    Cipher c = initCipher(wrapperKey, keyProps, Cipher.WRAP_MODE);
    return c.wrap(secretKey);
  } catch (Exception ex) {
    throw new SecurityException(ex);
  }
}

代码示例来源:origin: stackoverflow.com

Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
rsa.init(Cipher.WRAP_MODE, keyPair.getPublic());
byte[] wrapped = rsa.wrap(aesKey);

rsa.init(Cipher.UNWRAP_MODE, keyPair.getPrivate());
SecretKey unwrappedAESKey = (SecretKey) rsa.unwrap(wrapped, "RSA", Cipher.SECRET_KEY);

代码示例来源:origin: stackoverflow.com

KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "SunJSSE");
generator.initialize(2048);
KeyPair keyPair = generator.generateKeyPair();

SecretKey sessionKey = new SecretKeySpec(new byte[16], "AES");

Cipher c = Cipher.getInstance("RSA", "SunJCE");
c.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] result1 = c.doFinal(sessionKey.getEncoded());

c.init(Cipher.WRAP_MODE, keyPair.getPublic());
byte[] result2 = c.wrap(sessionKey);

c.init(Cipher.UNWRAP_MODE, keyPair.getPrivate());
SecretKey sessionKey1 = (SecretKey) c.unwrap(result1, "AES",
  Cipher.SECRET_KEY);

c.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
SecretKey sessionKey2 = new SecretKeySpec(c.doFinal(result2), "AES");

System.out.println(Arrays.equals(sessionKey1.getEncoded(),
  sessionKey2.getEncoded()));

代码示例来源:origin: stackoverflow.com

SecretKey sk = new SecretKeySpec(new byte[16], "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, new byte[12]);
cipher.init(Cipher.WRAP_MODE, sk, gcmSpec);
byte[] wrappedKey = cipher.wrap(sk);
System.out.println(Hex.toHexString(wrappedKey));

cipher.init(Cipher.UNWRAP_MODE, sk, gcmSpec);
SecretKey unwrap = (SecretKey) cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
System.out.println(Hex.toHexString(unwrap.getEncoded()));

代码示例来源:origin: aws/aws-dynamodb-encryption-java

public byte[] wrapKey(SecretKey key, String wrappingAlg) throws NoSuchAlgorithmException, NoSuchPaddingException,
    InvalidKeyException, IllegalBlockSizeException {
  if (wrappingKey instanceof DelegatedKey) {
    return ((DelegatedKey)wrappingKey).wrap(key, null, wrappingAlg);
  } else {
    Cipher cipher = Cipher.getInstance(wrappingAlg);
    cipher.init(Cipher.WRAP_MODE, wrappingKey, Utils.getRng());
    byte[] encryptedKey = cipher.wrap(key);
    return encryptedKey;
  }
}

代码示例来源:origin: com.amazonaws/aws-dynamodb-encryption-java

public byte[] wrapKey(SecretKey key, String wrappingAlg) throws NoSuchAlgorithmException, NoSuchPaddingException,
    InvalidKeyException, IllegalBlockSizeException {
  if (wrappingKey instanceof DelegatedKey) {
    return ((DelegatedKey)wrappingKey).wrap(key, null, wrappingAlg);
  } else {
    Cipher cipher = Cipher.getInstance(wrappingAlg);
    cipher.init(Cipher.WRAP_MODE, wrappingKey, Utils.getRng());
    byte[] encryptedKey = cipher.wrap(key);
    return encryptedKey;
  }
}

代码示例来源:origin: com.google.code.jscep/jscep-api

private RecipientInfo toRecipientInfo(X509Certificate cert, SecretKey key) throws CertificateEncodingException, IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException {
    PublicKey pubKey = cert.getPublicKey();
    TBSCertificateStructure tbs = TBSCertificateStructure.getInstance(ASN1Object.fromByteArray(cert.getTBSCertificate()));
    AlgorithmIdentifier keyEncAlg = tbs.getSubjectPublicKeyInfo().getAlgorithmId();

    Cipher keyCipher = Cipher.getInstance("RSA");
    keyCipher.init(Cipher.WRAP_MODE, pubKey);
    ASN1OctetString encKey = new DEROctetString(keyCipher.wrap(key));
    
    ASN1InputStream aIn = new ASN1InputStream(cert.getTBSCertificate());
    tbs = TBSCertificateStructure.getInstance(aIn.readObject());
    IssuerAndSerialNumber encSid = new IssuerAndSerialNumber(tbs.getIssuer(), tbs.getSerialNumber().getValue());
    
    return new RecipientInfo(new KeyTransRecipientInfo(new RecipientIdentifier(encSid), keyEncAlg, encKey));
  }
}

相关文章