public void changeCipher(BlockCipher bc) { this.currentCipher = bc; blockSize = bc.getBlockSize(); buffer = new byte[blockSize]; enc = new byte[blockSize]; pos = 0; }
public final void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { bc.transformBlock(X, 0, Xenc, 0); for (int i = 0; i < blockSize; i++) { dst[dstoff + i] = (byte) (src[srcoff + i] ^ Xenc[i]); } for (int i = (blockSize - 1); i >= 0; i--) { X[i]++; if (X[i] != 0) break; } } }
private static byte[] decryptData(byte[] encryptedData, byte[] keyAndIv, SshCipher sshCipher) { byte[] key = new byte[sshCipher.getKeyLength()]; byte[] iv = new byte[sshCipher.getBlockSize()]; System.arraycopy(keyAndIv, 0, key, 0, key.length); System.arraycopy(keyAndIv, key.length, iv, 0, iv.length); BlockCipher cipher = sshCipher.createBlockCipher(key, iv, false); byte[] decrypted = new byte[encryptedData.length]; for (int i = 0; i < encryptedData.length / cipher.getBlockSize(); i++) { cipher.transformBlock(encryptedData, i * cipher.getBlockSize(), decrypted, i * cipher.getBlockSize()); } return decrypted; }
public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) { try { CipherEntry ce = getEntry(type); Class cc = Class.forName(ce.cipherClass); BlockCipher bc = (BlockCipher) cc.newInstance(); if (type.endsWith("-cbc")) { bc.init(encrypt, key); return new CBCMode(bc, iv, encrypt); } else if (type.endsWith("-ctr")) { bc.init(true, key); return new CTRMode(bc, iv, encrypt); } throw new IllegalArgumentException("Cannot instantiate " + type); } catch (Exception e) { throw new IllegalArgumentException("Cannot instantiate " + type); } }
if ((ps.data.length % bc.getBlockSize()) != 0) throw new IOException("Invalid PEM structure, size of encrypted block is not a multiple of " + bc.getBlockSize()); for (int i = 0; i < ps.data.length / bc.getBlockSize(); i++) bc.transformBlock(ps.data, i * bc.getBlockSize(), dz, i * bc.getBlockSize()); dz = removePadding(dz, bc.getBlockSize());
public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) { try { CipherEntry ce = getEntry(type); Class cc = Class.forName(ce.cipherClass); BlockCipher bc = (BlockCipher) cc.newInstance(); if (type.endsWith("-cbc")) { bc.init(encrypt, key); return new CBCMode(bc, iv, encrypt); } else if (type.endsWith("-ctr")) { bc.init(true, key); return new CTRMode(bc, iv, encrypt); } throw new IllegalArgumentException("Cannot instantiate " + type); } catch (Exception e) { throw new IllegalArgumentException("Cannot instantiate " + type); } }
public void changeCipher(BlockCipher bc) { this.currentCipher = bc; blockSize = bc.getBlockSize(); buffer = new byte[blockSize]; enc = new byte[blockSize]; pos = 0; }
if ((ps.data.length % bc.getBlockSize()) != 0) throw new IOException("Invalid PEM structure, size of encrypted block is not a multiple of " + bc.getBlockSize()); for (int i = 0; i < ps.data.length / bc.getBlockSize(); i++) bc.transformBlock(ps.data, i * bc.getBlockSize(), dz, i * bc.getBlockSize()); dz = removePadding(dz, bc.getBlockSize());
public final void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { bc.transformBlock(X, 0, Xenc, 0); for (int i = 0; i < blockSize; i++) { dst[dstoff + i] = (byte) (src[srcoff + i] ^ Xenc[i]); } for (int i = (blockSize - 1); i >= 0; i--) { X[i]++; if (X[i] != 0) break; } } }
public static BlockCipher createCipher(String type, boolean encrypt, byte[] key, byte[] iv) { try { CipherEntry ce = getEntry(type); Class cc = Class.forName(ce.cipherClass); BlockCipher bc = (BlockCipher) cc.newInstance(); if (type.endsWith("-cbc")) { bc.init(encrypt, key); return new CBCMode(bc, iv, encrypt); } else if (type.endsWith("-ctr")) { bc.init(true, key); return new CTRMode(bc, iv, encrypt); } throw new IllegalArgumentException("Cannot instantiate " + type); } catch (Exception e) { throw new IllegalArgumentException("Cannot instantiate " + type); } }
public void changeCipher(BlockCipher bc) { this.currentCipher = bc; blockSize = bc.getBlockSize(); buffer = new byte[blockSize]; enc = new byte[blockSize]; pos = blockSize; }
if ((ps.data.length % bc.getBlockSize()) != 0) throw new IOException("Invalid PEM structure, size of encrypted block is not a multiple of " + bc.getBlockSize()); for (int i = 0; i < ps.data.length / bc.getBlockSize(); i++) bc.transformBlock(ps.data, i * bc.getBlockSize(), dz, i * bc.getBlockSize()); dz = removePadding(dz, bc.getBlockSize());
public final void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { bc.transformBlock(X, 0, Xenc, 0); for (int i = 0; i < blockSize; i++) { dst[dstoff + i] = (byte) (src[srcoff + i] ^ Xenc[i]); } for (int i = (blockSize - 1); i >= 0; i--) { X[i]++; if (X[i] != 0) break; } } }
public void changeCipher(BlockCipher bc) { this.currentCipher = bc; blockSize = bc.getBlockSize(); buffer = new byte[blockSize]; enc = new byte[blockSize]; pos = blockSize; }
private void encryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { for (int i = 0; i < blockSize; i++) cbc_vector[i] ^= src[srcoff + i]; tc.transformBlock(cbc_vector, 0, dst, dstoff); System.arraycopy(dst, dstoff, cbc_vector, 0, blockSize); }
public void changeCipher(BlockCipher bc) { this.currentCipher = bc; blockSize = bc.getBlockSize(); buffer = new byte[blockSize]; enc = new byte[blockSize]; pos = 0; }
private void encryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { for (int i = 0; i < blockSize; i++) cbc_vector[i] ^= src[srcoff + i]; tc.transformBlock(cbc_vector, 0, dst, dstoff); System.arraycopy(dst, dstoff, cbc_vector, 0, blockSize); }
public void changeCipher(BlockCipher bc) { this.currentCipher = bc; blockSize = bc.getBlockSize(); buffer = new byte[blockSize]; enc = new byte[blockSize]; pos = blockSize; }
private void decryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) { /* Assume the worst, src and dst are overlapping... */ System.arraycopy(src, srcoff, tmp_vector, 0, blockSize); tc.transformBlock(src, srcoff, dst, dstoff); for (int i = 0; i < blockSize; i++) dst[dstoff + i] ^= cbc_vector[i]; /* ...that is why we need a tmp buffer. */ byte[] swap = cbc_vector; cbc_vector = tmp_vector; tmp_vector = swap; }
public CTRMode(BlockCipher tc, byte[] iv, boolean doEnc) throws IllegalArgumentException { bc = tc; blockSize = bc.getBlockSize(); doEncrypt = doEnc; if (blockSize != iv.length) throw new IllegalArgumentException("IV must be " + blockSize + " bytes long! (currently " + iv.length + ")"); X = new byte[blockSize]; Xenc = new byte[blockSize]; System.arraycopy(iv, 0, X, 0, blockSize); }