public static final PrivateKeyEncryptionContext newPrivateKeyEncryptionContext(PrivateKeyObfuscator o, String password) { return initializeObfuscator(new PrivateKeyEncryptionContext(), o, password); }
public static final PrivateKeyEncryptionContext resolveEffectiveContext(PrivateKeyEncryptionContext encContext) { if (encContext == null) { return null; } String cipherName = encContext.getCipherName(); String cipherType = encContext.getCipherType(); PrivateKeyEncryptionContext effContext = encContext; if ("EDE3".equalsIgnoreCase(cipherType)) { cipherName += "ede"; effContext = encContext.clone(); effContext.setCipherName(cipherName); } return effContext; } }
@Override public int hashCode() { return GenericUtils.hashCode(getCipherName(), Boolean.TRUE) + GenericUtils.hashCode(getCipherType(), Boolean.TRUE) + GenericUtils.hashCode(getCipherMode(), Boolean.TRUE) + Objects.hashCode(getPassword()) + Arrays.hashCode(getInitVector()); }
public PrivateKeyObfuscator resolvePrivateKeyObfuscator() { PrivateKeyObfuscator value = getPrivateKeyObfuscator(); if (value != null) { return value; } return getRegisteredPrivateKeyObfuscator(getCipherName()); }
protected byte[] applyPrivateKeyCipher(byte[] bytes, PrivateKeyEncryptionContext encContext, boolean encryptIt) throws GeneralSecurityException { String cipherName = encContext.getCipherName(); PrivateKeyObfuscator o = encContext.resolvePrivateKeyObfuscator(); if (o == null) { throw new NoSuchAlgorithmException("decryptPrivateKeyData(" + encContext + ")[encrypt=" + encryptIt + "] unknown cipher: " + cipherName); } if (encryptIt) { byte[] initVector = encContext.getInitVector(); if (GenericUtils.isEmpty(initVector)) { initVector = o.generateInitializationVector(encContext); encContext.setInitVector(initVector); } } return o.applyPrivateKeyCipher(bytes, encContext, encryptIt); } }
@Override public String toString() { return GenericUtils.join(new String[]{getCipherName(), getCipherType(), getCipherMode()}, '-'); }
@Override public PrivateKeyEncryptionContext clone() { try { PrivateKeyEncryptionContext copy = getClass().cast(super.clone()); byte[] v = copy.getInitVector(); if (v != null) { v = v.clone(); copy.setInitVector(v); } return copy; } catch (CloneNotSupportedException e) { // unexpected throw new RuntimeException("Failed to clone: " + toString()); } }
PrivateKeyEncryptionContext encContext = new PrivateKeyEncryptionContext(algInfo); encContext.setPassword(password); encContext.setInitVector(initVector); byte[] encryptedData = KeyPairResourceParser.extractDataBytes(dataLines); byte[] decodedData = applyPrivateKeyCipher(encryptedData, encContext, false);
@Override protected int resolveKeyLength(PrivateKeyEncryptionContext encContext) throws GeneralSecurityException { String cipherType = encContext.getCipherType(); try { int keyLength = Integer.parseInt(cipherType); List<Integer> sizes = getSupportedKeySizes(); for (Integer s : sizes) { if (s.intValue() == keyLength) { return keyLength; } } throw new InvalidKeySpecException("Unknown " + getCipherName() + " key length: " + cipherType + " - supported: " + sizes); } catch (NumberFormatException e) { throw new InvalidKeySpecException("Bad " + getCipherName() + " key length (" + cipherType + "): " + e.getMessage(), e); } }
protected byte[] applyPrivateKeyCipher( byte[] bytes, PrivateKeyEncryptionContext encContext, boolean encryptIt) throws GeneralSecurityException, IOException { String cipherName = encContext.getCipherName(); PrivateKeyObfuscator o = encContext.resolvePrivateKeyObfuscator(); if (o == null) { throw new NoSuchAlgorithmException("decryptPrivateKeyData(" + encContext + ")[encrypt=" + encryptIt + "] unknown cipher: " + cipherName); } if (encryptIt) { byte[] initVector = encContext.getInitVector(); if (GenericUtils.isEmpty(initVector)) { initVector = o.generateInitializationVector(encContext); encContext.setInitVector(initVector); } } return o.applyPrivateKeyCipher(bytes, encContext, encryptIt); } }
public PrivateKeyObfuscator resolvePrivateKeyObfuscator() { PrivateKeyObfuscator value = getPrivateKeyObfuscator(); if (value != null) { return value; } return getRegisteredPrivateKeyObfuscator(getCipherName()); }
@Override public String toString() { return GenericUtils.join(new String[]{getCipherName(), getCipherType(), getCipherMode()}, '-'); }
@Override public PrivateKeyEncryptionContext clone() { try { PrivateKeyEncryptionContext copy = getClass().cast(super.clone()); byte[] v = copy.getInitVector(); if (v != null) { v = v.clone(); copy.setInitVector(v); } return copy; } catch (CloneNotSupportedException e) { // unexpected throw new RuntimeException("Failed to clone: " + toString()); } }
PrivateKeyEncryptionContext encContext = new PrivateKeyEncryptionContext(algInfo); encContext.setPassword(password); encContext.setInitVector(initVector);
@Override protected int resolveKeyLength(PrivateKeyEncryptionContext encContext) throws GeneralSecurityException { String cipherType = encContext.getCipherType(); try { int keyLength = Integer.parseInt(cipherType); List<Integer> sizes = getSupportedKeySizes(); for (Integer s : sizes) { if (s.intValue() == keyLength) { return keyLength; } } throw new InvalidKeySpecException("Unknown " + getCipherName() + " key length: " + cipherType + " - supported: " + sizes); } catch (NumberFormatException e) { throw new InvalidKeySpecException("Bad " + getCipherName() + " key length (" + cipherType + "): " + e.getMessage(), e); } }
@Override public int hashCode() { return GenericUtils.hashCode(getCipherName(), Boolean.TRUE) + GenericUtils.hashCode(getCipherType(), Boolean.TRUE) + GenericUtils.hashCode(getCipherMode(), Boolean.TRUE) + Objects.hashCode(getPassword()) + Arrays.hashCode(getInitVector()); }
public static final PrivateKeyEncryptionContext resolveEffectiveContext(PrivateKeyEncryptionContext encContext) { if (encContext == null) { return null; } String cipherName = encContext.getCipherName(); String cipherType = encContext.getCipherType(); PrivateKeyEncryptionContext effContext = encContext; if ("EDE3".equalsIgnoreCase(cipherType)) { cipherName += "ede"; effContext = encContext.clone(); effContext.setCipherName(cipherName); } return effContext; } }
public static final PrivateKeyEncryptionContext newPrivateKeyEncryptionContext(PrivateKeyObfuscator o, String password) { return initializeObfuscator(new PrivateKeyEncryptionContext(), o, password); }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (getClass() != obj.getClass()) { return false; } PrivateKeyEncryptionContext other = (PrivateKeyEncryptionContext) obj; return (GenericUtils.safeCompare(getCipherName(), other.getCipherName(), false) == 0) && (GenericUtils.safeCompare(getCipherType(), other.getCipherType(), false) == 0) && (GenericUtils.safeCompare(getCipherMode(), other.getCipherMode(), false) == 0) && (GenericUtils.safeCompare(getPassword(), other.getPassword(), true) == 0) && Arrays.equals(getInitVector(), other.getInitVector()); }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (getClass() != obj.getClass()) { return false; } PrivateKeyEncryptionContext other = (PrivateKeyEncryptionContext) obj; return (GenericUtils.safeCompare(getCipherName(), other.getCipherName(), false) == 0) && (GenericUtils.safeCompare(getCipherType(), other.getCipherType(), false) == 0) && (GenericUtils.safeCompare(getCipherMode(), other.getCipherMode(), false) == 0) && (GenericUtils.safeCompare(getPassword(), other.getPassword(), true) == 0) && Arrays.equals(getInitVector(), other.getInitVector()); }