本文整理汇总了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");
}
}