本文整理匯總了Java中javax.crypto.spec.DESedeKeySpec.isParityAdjusted方法的典型用法代碼示例。如果您正苦於以下問題:Java DESedeKeySpec.isParityAdjusted方法的具體用法?Java DESedeKeySpec.isParityAdjusted怎麽用?Java DESedeKeySpec.isParityAdjusted使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.crypto.spec.DESedeKeySpec
的用法示例。
在下文中一共展示了DESedeKeySpec.isParityAdjusted方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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());
}
}
示例2: EncryptionKey
import javax.crypto.spec.DESedeKeySpec; //導入方法依賴的package包/類
/**
* Generates a sub-sessionkey from a given session key.
*
* Used in AcceptSecContextToken and KrbApReq by acceptor- and initiator-
* side respectively.
*/
public EncryptionKey(EncryptionKey key) throws KrbCryptoException {
// generate random sub-session key
keyValue = Confounder.bytes(key.keyValue.length);
for (int i = 0; i < keyValue.length; i++) {
keyValue[i] ^= key.keyValue[i];
}
keyType = key.keyType;
// check for key parity and weak keys
try {
// check for DES key
if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
(keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
// fix DES key parity
if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
keyValue = Des.set_parity(keyValue);
}
// check for weak key
if (DESKeySpec.isWeak(keyValue, 0)) {
keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
}
}
// check for 3DES key
if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
// fix 3DES key parity
if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
keyValue = Des3.parityFix(keyValue);
}
// check for weak keys
byte[] oneKey = new byte[8];
for (int i=0; i<keyValue.length; i+=8) {
System.arraycopy(keyValue, i, oneKey, 0, 8);
if (DESKeySpec.isWeak(oneKey, 0)) {
keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
}
}
}
} catch (GeneralSecurityException e) {
KrbCryptoException ke = new KrbCryptoException(e.getMessage());
ke.initCause(e);
throw ke;
}
}
示例3: EncryptionKey
import javax.crypto.spec.DESedeKeySpec; //導入方法依賴的package包/類
/**
* Generates a sub-sessionkey from a given session key.
*/
// Used in KrbApRep, KrbApReq
EncryptionKey(EncryptionKey key) throws KrbCryptoException {
// generate random sub-session key
keyValue = Confounder.bytes(key.keyValue.length);
for (int i = 0; i < keyValue.length; i++) {
keyValue[i] ^= key.keyValue[i];
}
keyType = key.keyType;
// check for key parity and weak keys
try {
// check for DES key
if ((keyType == EncryptedData.ETYPE_DES_CBC_MD5) ||
(keyType == EncryptedData.ETYPE_DES_CBC_CRC)) {
// fix DES key parity
if (!DESKeySpec.isParityAdjusted(keyValue, 0)) {
keyValue = Des.set_parity(keyValue);
}
// check for weak key
if (DESKeySpec.isWeak(keyValue, 0)) {
keyValue[7] = (byte)(keyValue[7] ^ 0xF0);
}
}
// check for 3DES key
if (keyType == EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD) {
// fix 3DES key parity
if (!DESedeKeySpec.isParityAdjusted(keyValue, 0)) {
keyValue = Des3.parityFix(keyValue);
}
// check for weak keys
byte[] oneKey = new byte[8];
for (int i=0; i<keyValue.length; i+=8) {
System.arraycopy(keyValue, i, oneKey, 0, 8);
if (DESKeySpec.isWeak(oneKey, 0)) {
keyValue[i+7] = (byte)(keyValue[i+7] ^ 0xF0);
}
}
}
} catch (GeneralSecurityException e) {
KrbCryptoException ke = new KrbCryptoException(e.getMessage());
ke.initCause(e);
throw ke;
}
}
示例4: testDESAndDESede
import javax.crypto.spec.DESedeKeySpec; //導入方法依賴的package包/類
private void testDESAndDESede(BigInteger g, BigInteger p)
throws Exception
{
DHParameterSpec dhParams = new DHParameterSpec(p, g, 256);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC");
keyGen.initialize(dhParams);
KeyPair kp = keyGen.generateKeyPair();
KeyAgreement keyAgreement = KeyAgreement.getInstance("DH", "BC");
keyAgreement.init(kp.getPrivate());
keyAgreement.doPhase(kp.getPublic(), true);
SecretKey key = keyAgreement.generateSecret("DES");
if (key.getEncoded().length != 8)
{
fail("DES length wrong");
}
if (!DESKeySpec.isParityAdjusted(key.getEncoded(), 0))
{
fail("DES parity wrong");
}
key = keyAgreement.generateSecret("DESEDE");
if (key.getEncoded().length != 24)
{
fail("DESEDE length wrong");
}
if (!DESedeKeySpec.isParityAdjusted(key.getEncoded(), 0))
{
fail("DESEDE parity wrong");
}
key = keyAgreement.generateSecret("Blowfish");
if (key.getEncoded().length != 16)
{
fail("Blowfish length wrong");
}
}