@Override public CryptoAPIEncryptionHeader clone() throws CloneNotSupportedException { return (CryptoAPIEncryptionHeader)super.clone(); } }
protected StandardEncryptionVerifier(LittleEndianInput is, StandardEncryptionHeader header) { int saltSize = is.readInt(); if (saltSize!=16) { throw new RuntimeException("Salt size != 16 !?"); } byte salt[] = new byte[16]; is.readFully(salt); setSalt(salt); byte encryptedVerifier[] = new byte[16]; is.readFully(encryptedVerifier); setEncryptedVerifier(encryptedVerifier); verifierHashSize = is.readInt(); byte encryptedVerifierHash[] = new byte[header.getCipherAlgorithm().encryptedVerifierHashLength]; is.readFully(encryptedVerifierHash); setEncryptedVerifierHash(encryptedVerifierHash); setSpinCount(SPIN_COUNT); setCipherAlgorithm(header.getCipherAlgorithm()); setChainingMode(header.getChainingMode()); setEncryptedKey(null); setHashAlgorithm(header.getHashAlgorithm()); }
/** * serializes the header */ @Override public void write(LittleEndianByteArrayOutputStream bos) { int startIdx = bos.getWriteIndex(); LittleEndianOutput sizeOutput = bos.createDelayedOutput(LittleEndianConsts.INT_SIZE); bos.writeInt(getFlags()); bos.writeInt(0); // size extra bos.writeInt(getCipherAlgorithm().ecmaId); bos.writeInt(getHashAlgorithm().ecmaId); bos.writeInt(getKeySize()); bos.writeInt(getCipherProvider().ecmaId); bos.writeInt(0); // reserved1 bos.writeInt(0); // reserved2 String cspName = getCspName(); if (cspName == null) { cspName = getCipherProvider().cipherProviderName; } bos.write(StringUtil.getToUnicodeLE(cspName)); bos.writeShort(0); int headerSize = bos.getWriteIndex()-startIdx-LittleEndianConsts.INT_SIZE; sizeOutput.writeInt(headerSize); }
protected StandardEncryptionHeader(CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode) { setCipherAlgorithm(cipherAlgorithm); setHashAlgorithm(hashAlgorithm); setKeySize(keyBits); setBlockSize(blockSize); setCipherProvider(cipherAlgorithm.provider); setFlags(flagCryptoAPI.setBoolean(0, true) | flagAES.setBoolean(0, cipherAlgorithm.provider == CipherProvider.aes)); // see http://msdn.microsoft.com/en-us/library/windows/desktop/bb931357(v=vs.85).aspx for a full list // setCspName("Microsoft Enhanced RSA and AES Cryptographic Provider"); }
protected StandardEncryptionHeader(LittleEndianInput is) throws IOException { setFlags(is.readInt()); setSizeExtra(is.readInt()); setCipherAlgorithm(CipherAlgorithm.fromEcmaId(is.readInt())); setHashAlgorithm(HashAlgorithm.fromEcmaId(is.readInt())); int keySize = is.readInt(); if (keySize == 0) { setKeySize(keySize); setBlockSize(getKeySize()); setCipherProvider(CipherProvider.fromEcmaId(is.readInt())); setCspName(""); } else { StringBuilder builder = new StringBuilder(); setCspName(builder.toString()); setChainingMode(ChainingMode.ecb); setKeySalt(null);
/** * initialize the builder from a stream */ @Override public void initialize(EncryptionInfo info, LittleEndianInput dis) throws IOException { /* int hSize = */ dis.readInt(); StandardEncryptionHeader header = new StandardEncryptionHeader(dis); info.setHeader(header); info.setVerifier(new StandardEncryptionVerifier(dis, header)); if (info.getVersionMinor() == 2 && (info.getVersionMajor() == 3 || info.getVersionMajor() == 4)) { StandardDecryptor dec = new StandardDecryptor(); dec.setEncryptionInfo(info); info.setDecryptor(dec); } }
protected StandardEncryptionHeader(LittleEndianInput is) throws IOException { setFlags(is.readInt()); setSizeExtra(is.readInt()); setCipherAlgorithm(CipherAlgorithm.fromEcmaId(is.readInt())); setHashAlgorithm(HashAlgorithm.fromEcmaId(is.readInt())); int keySize = is.readInt(); if (keySize == 0) { setKeySize(keySize); setBlockSize(getKeySize()); setCipherProvider(CipherProvider.fromEcmaId(is.readInt())); setCspName(""); } else { StringBuilder builder = new StringBuilder(); setCspName(builder.toString()); setChainingMode(ChainingMode.ecb); setKeySalt(null);
protected StandardEncryptionHeader(CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode) { setCipherAlgorithm(cipherAlgorithm); setHashAlgorithm(hashAlgorithm); setKeySize(keyBits); setBlockSize(blockSize); setCipherProvider(cipherAlgorithm.provider); setFlags(flagCryptoAPI.setBoolean(0, true) | flagAES.setBoolean(0, cipherAlgorithm.provider == CipherProvider.aes)); // see http://msdn.microsoft.com/en-us/library/windows/desktop/bb931357(v=vs.85).aspx for a full list // setCspName("Microsoft Enhanced RSA and AES Cryptographic Provider"); }
throw new EncryptedDocumentException("KeySize "+keyBits+" not allowed for Cipher "+ cipherAlgorithm); info.setHeader(new StandardEncryptionHeader(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode)); info.setVerifier(new StandardEncryptionVerifier(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode)); StandardDecryptor dec = new StandardDecryptor();
/** * serializes the header */ @Override public void write(LittleEndianByteArrayOutputStream bos) { int startIdx = bos.getWriteIndex(); LittleEndianOutput sizeOutput = bos.createDelayedOutput(LittleEndianConsts.INT_SIZE); bos.writeInt(getFlags()); bos.writeInt(0); // size extra bos.writeInt(getCipherAlgorithm().ecmaId); bos.writeInt(getHashAlgorithm().ecmaId); bos.writeInt(getKeySize()); bos.writeInt(getCipherProvider().ecmaId); bos.writeInt(0); // reserved1 bos.writeInt(0); // reserved2 String cspName = getCspName(); if (cspName == null) { cspName = getCipherProvider().cipherProviderName; } bos.write(StringUtil.getToUnicodeLE(cspName)); bos.writeShort(0); int headerSize = bos.getWriteIndex()-startIdx-LittleEndianConsts.INT_SIZE; sizeOutput.writeInt(headerSize); }
protected StandardEncryptionVerifier(LittleEndianInput is, StandardEncryptionHeader header) { int saltSize = is.readInt(); if (saltSize!=16) { throw new RuntimeException("Salt size != 16 !?"); } byte salt[] = new byte[16]; is.readFully(salt); setSalt(salt); byte encryptedVerifier[] = new byte[16]; is.readFully(encryptedVerifier); setEncryptedVerifier(encryptedVerifier); verifierHashSize = is.readInt(); byte encryptedVerifierHash[] = new byte[header.getCipherAlgorithm().encryptedVerifierHashLength]; is.readFully(encryptedVerifierHash); setEncryptedVerifierHash(encryptedVerifierHash); setSpinCount(SPIN_COUNT); setCipherAlgorithm(header.getCipherAlgorithm()); setChainingMode(header.getChainingMode()); setEncryptedKey(null); setHashAlgorithm(header.getHashAlgorithm()); }
/** * initialize the builder from a stream */ @Override public void initialize(EncryptionInfo info, LittleEndianInput dis) throws IOException { /* int hSize = */ dis.readInt(); StandardEncryptionHeader header = new StandardEncryptionHeader(dis); info.setHeader(header); info.setVerifier(new StandardEncryptionVerifier(dis, header)); if (info.getVersionMinor() == 2 && (info.getVersionMajor() == 3 || info.getVersionMajor() == 4)) { StandardDecryptor dec = new StandardDecryptor(); dec.setEncryptionInfo(info); info.setDecryptor(dec); } }
@Override public CryptoAPIEncryptionHeader clone() throws CloneNotSupportedException { return (CryptoAPIEncryptionHeader)super.clone(); } }
throw new EncryptedDocumentException("KeySize "+keyBits+" not allowed for Cipher "+ cipherAlgorithm); info.setHeader(new StandardEncryptionHeader(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode)); info.setVerifier(new StandardEncryptionVerifier(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode)); StandardDecryptor dec = new StandardDecryptor();