本文整理匯總了Java中javax.crypto.spec.DESedeKeySpec類的典型用法代碼示例。如果您正苦於以下問題:Java DESedeKeySpec類的具體用法?Java DESedeKeySpec怎麽用?Java DESedeKeySpec使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DESedeKeySpec類屬於javax.crypto.spec包,在下文中一共展示了DESedeKeySpec類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: des3EncodeECB
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
* ECB加密,不要IV
*
* @param key 密鑰
* @param data 明文
* @return Base64編碼的密文
* @throws Exception
*/
public static byte[] des3EncodeECB(byte[] key, byte[] data)
throws Exception {
SecretKey deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
示例2: getSecretKey
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
protected Key getSecretKey(KeyInformation keyInformation) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException
{
byte[] keyData = keyInformation.getKeyData();
if(keyData == null)
{
if(keyInformation.getKeyAlgorithm().equals("DESede"))
{
// no key data provided, generate key data automatically
keyData = generateKeyData();
}
else
{
throw new AlfrescoRuntimeException("Unable to generate secret key: key algorithm is not DESede and no keyData provided");
}
}
DESedeKeySpec keySpec = new DESedeKeySpec(keyData);
SecretKeyFactory kf = SecretKeyFactory.getInstance(keyInformation.getKeyAlgorithm());
SecretKey secretKey = kf.generateSecret(keySpec);
return secretKey;
}
示例3: getCipher
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
protected Cipher getCipher(byte[] key, byte[] ivec, int mode)
throws GeneralSecurityException {
// NoSuchAlgorithException
SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");
// InvalidKeyException
KeySpec spec = new DESedeKeySpec(key, 0);
// InvalidKeySpecException
SecretKey secretKey = factory.generateSecret(spec);
// IV
if (ivec == null) {
ivec = ZERO_IV;
}
// NoSuchAlgorithmException, NoSuchPaddingException
// NoSuchProviderException
Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
// InvalidKeyException, InvalidAlgorithParameterException
cipher.init(mode, secretKey, encIv);
return cipher;
}
示例4: engineGenerateSecret
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
* Generates a <code>SecretKey</code> object from the provided key
* specification (key material).
*
* @param keySpec the specification (key material) of the secret key
*
* @return the secret key
*
* @exception InvalidKeySpecException if the given key specification
* is inappropriate for this key factory to produce a public key.
*/
protected SecretKey engineGenerateSecret(KeySpec keySpec)
throws InvalidKeySpecException {
try {
if (keySpec instanceof DESedeKeySpec) {
return new DESedeKey(((DESedeKeySpec)keySpec).getKey());
}
if (keySpec instanceof SecretKeySpec) {
return new DESedeKey(((SecretKeySpec)keySpec).getEncoded());
}
throw new InvalidKeySpecException
("Inappropriate key specification");
} catch (InvalidKeyException e) {
throw new InvalidKeySpecException(e.getMessage());
}
}
示例5: engineGetKeySpec
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
* Returns a specification (key material) of the given key
* in the requested format.
*
* @param key the key
*
* @param keySpec the requested format in which the key material shall be
* returned
*
* @return the underlying key specification (key material) in the
* requested format
*
* @exception InvalidKeySpecException if the requested key specification is
* inappropriate for the given key, or the given key cannot be processed
* (e.g., the given key has an unrecognized algorithm or format).
*/
protected KeySpec engineGetKeySpec(SecretKey key, Class<?> keySpec)
throws InvalidKeySpecException {
try {
if ((key instanceof SecretKey)
&& (key.getAlgorithm().equalsIgnoreCase("DESede"))
&& (key.getFormat().equalsIgnoreCase("RAW"))) {
// Check if requested key spec is amongst the valid ones
if (DESedeKeySpec.class.isAssignableFrom(keySpec)) {
return new DESedeKeySpec(key.getEncoded());
} else {
throw new InvalidKeySpecException
("Inappropriate key specification");
}
} else {
throw new InvalidKeySpecException
("Inappropriate key format/algorithm");
}
} catch (InvalidKeyException e) {
throw new InvalidKeySpecException("Secret key has wrong size");
}
}
示例6: Encrypt
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
* 3-DES加密
*
* @param byte[]
* src 要進行3-DES加密的byte[]
* @param byte[]
* enKey 3-DES加密密鑰
* @return byte[] 3-DES加密後的byte[]
*/
public byte[] Encrypt(byte[] src, byte[] enKey) {
byte[] encryptedData = null;
try {
DESedeKeySpec dks = new DESedeKeySpec(enKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedData = cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedData;
}
示例7: deCrypt
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
*
* 進行3-DES解密(密鑰匙等同於加密的密鑰匙)。
*
* @param byte[]
* src 要進行3-DES解密byte[]
* @param String
* spkey分配的SPKEY
* @return String 3-DES解密後的String
*/
public static String deCrypt(byte[] debase64, String spKey) {
String strDe = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DESede");
byte[] key = getEnKey(spKey);
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey sKey = keyFactory.generateSecret(dks);
cipher.init(Cipher.DECRYPT_MODE, sKey);
byte ciphertext[] = cipher.doFinal(debase64);
strDe = new String(ciphertext, "UTF-16LE");
} catch (Exception ex) {
strDe = "";
ex.printStackTrace();
}
return strDe;
}
示例8: engineGenerateSecret
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
@Override
protected SecretKey engineGenerateSecret(String algorithm)
throws IllegalStateException, NoSuchAlgorithmException,
InvalidKeyException {
byte secret[] = engineGenerateSecret();
if (debug.DEBUG)
log("generating SecretKey for " + algorithm);
if (algorithm.equals("DES")) {
return (SecretKey)new DESKeySpec(secret);
} else if (algorithm.equals("DESede")) {
return (SecretKey)new DESedeKeySpec(secret);
} else {
/* AES and default */
return new SecretKeySpec(secret, algorithm);
}
}
示例9: DESedeKey
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
* Uses the first 24 bytes in <code>key</code>, beginning at
* <code>offset</code>, as the DES-EDE key
*
* @param key the buffer with the DES-EDE key
* @param offset the offset in <code>key</code>, where the DES-EDE key
* starts
*
* @exception InvalidKeyException if the given key has a wrong size
*/
DESedeKey(byte[] key, int offset) throws InvalidKeyException {
if (key==null || ((key.length-offset)<DESedeKeySpec.DES_EDE_KEY_LEN)) {
throw new InvalidKeyException("Wrong key size");
}
this.key = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
System.arraycopy(key, offset, this.key, 0,
DESedeKeySpec.DES_EDE_KEY_LEN);
DESKeyGenerator.setParityBit(this.key, 0);
DESKeyGenerator.setParityBit(this.key, 8);
DESKeyGenerator.setParityBit(this.key, 16);
// Use the cleaner to zero the key when no longer referenced
final byte[] k = this.key;
CleanerFactory.cleaner().register(this,
() -> java.util.Arrays.fill(k, (byte)0x00));
}
示例10: getInstance
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
public static MeviusTransferPacket getInstance(PublicKey publickey, MeviusPacket packet)
throws MeviusCipherException {
try {
DESedeKeySpec desKeySpec = new DESedeKeySpec(((String) MeviusCipherKey.randomDESKey().getKey()).getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
Key key = keyFactory.generateSecret(desKeySpec);
Cipher c = Cipher.getInstance("RSA/ECB/PKCS1PADDING", "SunJCE");
c.init(Cipher.ENCRYPT_MODE, publickey);
byte[] bkey = convertObj(key, c);
c = Cipher.getInstance("DESede/ECB/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] bobj = convertObj(packet, c);
return new MeviusTransferPacket(bkey, bobj);
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException
| IOException | InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
throw new MeviusCipherException(e.getLocalizedMessage());
}
}
示例11: des3EncodeCBC
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
* CBC加密
*
* @param key 密鑰
* @param keyiv IV
* @param data 明文
* @return Base64編碼的密文
* @throws Exception
*/
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
throws Exception {
SecretKey deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
示例12: engineGenerateSecret
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
@Override
protected SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException {
if (keySpec == null) {
throw new InvalidKeySpecException("Null KeySpec");
}
if (keySpec instanceof SecretKeySpec) {
SecretKeySpec key = (SecretKeySpec) keySpec;
try {
if (!DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0)) {
throw new InvalidKeySpecException(
"SecretKeySpec is not a parity-adjusted DESEDE key");
}
} catch (InvalidKeyException e) {
throw new InvalidKeySpecException(e);
}
return key;
} else if (keySpec instanceof DESedeKeySpec) {
DESedeKeySpec desKeySpec = (DESedeKeySpec) keySpec;
return new SecretKeySpec(desKeySpec.getKey(), "DESEDE");
} else {
throw new InvalidKeySpecException(
"Unsupported KeySpec class: " + keySpec.getClass().getName());
}
}
示例13: doKeyGeneration
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
@Override
protected byte[] doKeyGeneration(int keyBytes) {
byte[] keyData = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];
secureRandom.nextBytes(keyData);
// Set the parity bit for each byte
for (int i = 0; i < keyData.length; i++) {
if (Integer.bitCount(keyData[i]) % 2 == 0) {
keyData[i] = (byte) (keyData[i] ^ 1);
}
}
if (keyBytes == 14) {
// The user requested an A-B-A key
System.arraycopy(keyData, 0, keyData, 16, 8);
}
return keyData;
}
示例14: engineLookupAndResolveSecretKey
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
/**
* Method engineResolveSecretKey
*
* @param element
* @param BaseURI
* @param storage
*
* @throws KeyResolverException
*/
public SecretKey engineLookupAndResolveSecretKey(
Element element, String BaseURI, StorageResolver storage
) throws KeyResolverException {
if (engineCanResolve(element, BaseURI, storage)) {
try {
DESedeKeySpec keySpec =
new DESedeKeySpec("abcdefghijklmnopqrstuvwx".getBytes("ASCII"));
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(keySpec);
return key;
}
catch (Exception e) {
throw new KeyResolverException("Something badly wrong in creation of bob's key");
}
}
return null;
}
示例15: loadKeyEncryptionKey
import javax.crypto.spec.DESedeKeySpec; //導入依賴的package包/類
private static SecretKey loadKeyEncryptionKey() throws Exception {
String fileName = "build/kek";
String jceAlgorithmName = "DESede";
File kekFile = new File(fileName);
DESedeKeySpec keySpec =
new DESedeKeySpec(JavaUtils.getBytesFromFile(fileName));
SecretKeyFactory skf =
SecretKeyFactory.getInstance(jceAlgorithmName);
SecretKey key = skf.generateSecret(keySpec);
System.out.println(
"Key encryption key loaded from " + kekFile.toURI().toURL().toString()
);
return key;
}