List of usage examples for org.bouncycastle.crypto BlockCipher getBlockSize
public int getBlockSize();
From source file:cologne.eck.peafactory.peagen.FileModifier.java
License:Open Source License
/** * Generate the file "settings/PeaSettings.java". * This file will be compiled and stored in the new PEA *//*w ww . java 2 s .co m*/ public final static void generatePeaSettings() { StringBuilder builder = new StringBuilder(); // Fixed content: builder.append("package settings;\n" + "\n/* Automatically produced file */\n" + "/* This class is newly created for each PEA */\n\n" + "import javax.swing.JDialog;\n" + "import org.bouncycastle.crypto.*;\n" //+ "import org.bouncycastle.crypto.Digest;\n" + "import org.bouncycastle.crypto.digests.*;\n" + "import org.bouncycastle.crypto.engines.*;\n" + "import KeyDerivation;\n" + "import cologne.eck.peafactory.crypto.kdf.*;\n"); //+ "import cologne.eck.peafactory.peas.gui.*;\n"); // Variable content: String base = ""; if (pswGenerator == true || ((pswGenerator == true) && ((DataType.getCurrentType() instanceof NotesType) || (DataType.getCurrentType() instanceof EditorType)))) { builder.append("import cologne.eck.peafactory.peas.gui.*;\n"); } builder.append("\npublic class PeaSettings {\n\n"); if (setKeyboard == true) { builder.append("private static JDialog keyboard = new Keyboard(PswDialogView.getView() );\n"); } else { builder.append("private static JDialog keyboard = null;\n"); } base = "private static final JDialog pswGenerator = "; if ((pswGenerator == true) && ((DataType.getCurrentType() instanceof NotesType) || (DataType.getCurrentType() instanceof EditorType))) { builder.append("private static final JDialog pswGenerator = new PasswordGeneratorDialog(null);\n"); } else { builder.append("private static final JDialog pswGenerator = null;\n"); } builder.append("private static final boolean BOUND = " + CipherStuff.isBound() + ";\n"); boolean extern = false; base = "private static final String EXTERNAL_FILE_PATH = "; if (MainView.getOpenedFileName() != null) { extern = true; // Windows: handle backslash \ String fileName = MainView.getOpenedFileName().replace("\\", "\\\\"); builder.append(base + "\"" + fileName + "\";\n"); } else { if (DataType.getCurrentType() instanceof FileType) { extern = true; } else { extern = false; } builder.append(base + "null;\n"); /* if (DataType.getCurrentType() instanceof ImageType && MainView.isBlankPea() == true) { extern = true; }*/ } builder.append("private static final boolean EXTERN_FILE = " + extern + ";\n"); if (JarStuff.getJarFileName() == null) { builder.append("private static final String JAR_FILE_NAME = \"default.jar\";\n"); //JarStuff.setJarFileName("default.jar"); } else { builder.append("private static final String JAR_FILE_NAME = " + "\"" + JarStuff.getJarFileName().substring(0, JarStuff.getJarFileName().length() - 4) + "\";\n"); } if ((JarStuff.getLabelText() != null) && (JarStuff.getLabelText().length() > 0)) { // if set: if (JarStuff.getLabelText().length() > 40) { JOptionPane.showMessageDialog(null, "Warning:\n" + " Text above password-field longer than 40 characters will not be displayed. \n" + " Encryption continues ..."); } builder.append("private static final String LABEL_TEXT = " + "\"" + JarStuff.getLabelText() + "\";\n"); } else { builder.append("private static final String LABEL_TEXT = null;\n"); } String programRandomString = "{"; for (int i = 0; i < Attachments.getProgramRandomBytesSize(); i++) { programRandomString += Attachments.getProgramRandomBytes()[i] + ", "; } programRandomString = programRandomString.substring(0, programRandomString.length() - 2); // delete last , builder.append("private static final byte[] PROGRAM_RANDOM_BYTES = " + programRandomString + "};\n"); String fileIdentifierString = "{"; for (int i = 0; i < Attachments.getFileIdentifier().length; i++) { fileIdentifierString += Attachments.getFileIdentifier()[i] + ", "; } fileIdentifierString = fileIdentifierString.substring(0, fileIdentifierString.length() - 2); // delete last , builder.append("private static final byte[] FILE_IDENTIFIER = " + fileIdentifierString + "};\n"); base = "private static final BlockCipher CIPHER_ALGO = "; BlockCipher cipherAlgo = CipherStuff.getCipherAlgo(); if (cipherAlgo instanceof Shacal2Engine) { builder.append(base + "new Shacal2Engine();\n"); } else if (cipherAlgo instanceof TwofishEngine) { builder.append(base + "new TwofishEngine();\n"); } else if (cipherAlgo instanceof AESEngine) { builder.append(base + "new AESEngine();\n"); } else if (cipherAlgo instanceof AESFastEngine) { builder.append(base + "new AESFastEngine();\n"); } else if (cipherAlgo instanceof SerpentEngine) { builder.append(base + "new SerpentEngine();\n"); } else if (cipherAlgo instanceof ThreefishEngine) { int blockSize = cipherAlgo.getBlockSize(); builder.append(base + "new ThreefishEngine(" + blockSize * 8 + ");\n"); } else { System.err.println("FileModifier: CipherAlgorithm invalid"); } // Hash algorithm base = "private static final Digest HASH_ALGO = "; Digest hashAlgo = HashStuff.getHashAlgo(); if (hashAlgo instanceof WhirlpoolDigest) { builder.append(base + "new WhirlpoolDigest();\n"); } else if (hashAlgo instanceof SHA3Digest) { builder.append(base + "new SHA3Digest();\n"); } else if (hashAlgo instanceof SkeinDigest) { builder.append(base + "new SkeinDigest(512, 512);\n"); } else if (hashAlgo instanceof SHA512Digest) { builder.append(base + "new SHA512Digest();\n"); } else if (hashAlgo instanceof SHA384Digest) { builder.append(base + "new SHA384Digest();\n"); } else if (hashAlgo instanceof Blake2bDigest) { builder.append(base + "new Blake2bDigest();\n"); // } else if (hashAlgo instanceof RIPEMD256Digest) { // builder.append(base + "new RIPEMD256Digest();\n"); } else if (hashAlgo instanceof RIPEMD320Digest) { builder.append(base + "new RIPEMD320Digest();\n"); } else { System.err.println("FileModifier: HashAlgorithm failed"); } // KDF scheme KeyDerivation kdfScheme = KeyDerivation.getKdf(); base = "private static final KeyDerivation KDF_SCHEME = "; if (kdfScheme instanceof BcryptKDF) { builder.append(base + "new BcryptKDF();\n"); } else if (kdfScheme instanceof ScryptKDF) { builder.append(base + "new ScryptKDF();\n"); } else if (kdfScheme instanceof PomeloKDF) { builder.append(base + "new PomeloKDF();\n"); } else if (kdfScheme instanceof CatenaKDF) { builder.append(base + "new CatenaKDF();\n"); } else { System.err.println("FileModifier: Key derivation scheme invalid"); } // if (kdfScheme instanceof BcryptKDF) { builder.append("private static final int ITERATIONS = " + KeyDerivation.gettCost() + ";\n"); builder.append("private static final int MEMORY = " + KeyDerivation.getmCost() + ";\n"); builder.append("private static final int PARALLELIZATION = " + KeyDerivation.getArg3() + ";\n"); if (KeyDerivation.getVersionString().equals("")) { builder.append("private static final String VERSION_STRING = \"\";\n"); } else { builder.append("private static final String VERSION_STRING = " + "\"" + KeyDerivation.getVersionString() + "\";\n"); } // } else if (kdfScheme instanceof ScryptKDF) { // builder.append("private static final int ITERATIONS = " + KeyDerivation.gettCost() + ";\n" ); // builder.append("private static final int MEMORY = " + KeyDerivation.getmCost() + ";\n" ); // builder.append("private static final int PARALLELIZATION = " + KeyDerivation.getArg3() + ";\n" ); // } else { // System.err.println("FileModifier: KDFScheme invalid"); // } // Fixed content: builder.append("public final static JDialog getKeyboard() { return keyboard; }\n" + "public final static JDialog getPswGenerator() { return pswGenerator; }\n" + "public final static Digest getHashAlgo() { return HASH_ALGO; }\n" + "public final static int getIterations() { return ITERATIONS; }\n" + "public final static int getMemory() { return MEMORY; }\n" + "public final static int getParallelization() { return PARALLELIZATION; }\n" + "public final static String getVersionString() { return VERSION_STRING; }\n" + "public final static byte[] getProgramRandomBytes() { return PROGRAM_RANDOM_BYTES; }\n" + "public final static byte[] getFileIdentifier() { return FILE_IDENTIFIER; }\n" + "public final static String getJarFileName() { return JAR_FILE_NAME; }\n" + "public final static String getLabelText() { return LABEL_TEXT; }\n" + "public final static boolean getExternFile() { return EXTERN_FILE; }\n" + "public final static boolean getBound() { return BOUND; }\n" + "public final static String getExternalFilePath() { return EXTERNAL_FILE_PATH; }\n" + "public final static BlockCipher getCipherAlgo() { return CIPHER_ALGO; }\n" + "public final static KeyDerivation getKdfScheme() { return KDF_SCHEME; }\n" + "}\n"); //System.out.println(new String(builder)); BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter("settings" + File.separator + "PeaSettings.java")); writer.write(new String(builder)); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.github.horrorho.inflatabledonkey.crypto.xts.XTSTweak.java
License:Open Source License
XTSTweak(BlockCipher cipher, LongFunction<byte[]> tweakFunction, byte[] tweak) { this.cipher = Objects.requireNonNull(cipher, "cipher"); this.tweakFunction = Objects.requireNonNull(tweakFunction, "tweakFunction"); this.tweak = Objects.requireNonNull(tweak, "tweak"); if (cipher.getBlockSize() != BLOCK_SIZE) { throw new IllegalArgumentException("bad block size: " + cipher.getBlockSize()); }/*from ww w. j a v a 2 s.co m*/ }
From source file:com.github.horrorho.inflatabledonkey.crypto.xts.XTSTweak.java
License:Open Source License
XTSTweak(BlockCipher cipher, LongFunction<byte[]> tweakFunction) { this(cipher, tweakFunction, new byte[cipher.getBlockSize()]); }
From source file:com.hanhuy.keepassj.CompositeKey.java
License:Open Source License
public static long TransformKeyBenchmark(long uMilliseconds, long uStep) { long uRounds; // Try native method // if(NativeLib.TransformKeyBenchmark256(uMilliseconds, out uRounds)) // return uRounds; byte[] pbKey = new byte[32]; byte[] pbNewKey = new byte[32]; for (int i = 0; i < pbKey.length; ++i) { pbKey[i] = (byte) i; pbNewKey[i] = (byte) i; }/*from w ww. ja v a 2s . co m*/ byte[] pbIV = new byte[16]; Arrays.fill(pbIV, (byte) 0); try { BlockCipher engine = AesEngines.createAesEngine(); engine.init(true, new KeyParameter(pbKey)); if (engine.getBlockSize() != (128 / 8)) // AES block size { throw new RuntimeException(); } uRounds = 0; long tStart = System.currentTimeMillis(); while (true) { for (long j = 0; j < uStep; ++j) { engine.processBlock(pbNewKey, 0, pbNewKey, 0); engine.processBlock(pbNewKey, 16, pbNewKey, 16); } uRounds += uStep; if (uRounds < uStep) // Overflow check { uRounds = Long.MAX_VALUE; break; } long tElapsed = System.currentTimeMillis() - tStart; if (tElapsed > uMilliseconds) break; } return uRounds; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.licel.jcardsim.crypto.SymmetricSignatureImpl.java
License:Apache License
public void init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen) throws CryptoException { if (theKey == null) { CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY); }/*from w w w . j a v a2 s. c o m*/ if (!theKey.isInitialized()) { CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY); } if (!(theKey instanceof SymmetricKeyImpl)) { CryptoException.throwIt(CryptoException.ILLEGAL_VALUE); } CipherParameters cipherParams = null; BlockCipher cipher = ((SymmetricKeyImpl) theKey).getCipher(); if (bArray == null) { cipherParams = ((SymmetricKeyImpl) theKey).getParameters(); } else { if (bLen != cipher.getBlockSize()) { CryptoException.throwIt(CryptoException.ILLEGAL_VALUE); } cipherParams = new ParametersWithIV(((SymmetricKeyImpl) theKey).getParameters(), bArray, bOff, bLen); } switch (algorithm) { case ALG_DES_MAC4_NOPAD: engine = new CBCBlockCipherMac(cipher, 32, null); break; case ALG_DES_MAC8_NOPAD: engine = new CBCBlockCipherMac(cipher, 64, null); break; case ALG_DES_MAC4_ISO9797_M1: engine = new CBCBlockCipherMac(cipher, 32, new ZeroBytePadding()); break; case ALG_DES_MAC8_ISO9797_M1: engine = new CBCBlockCipherMac(cipher, 64, new ZeroBytePadding()); break; case ALG_DES_MAC4_ISO9797_M2: engine = new CBCBlockCipherMac(cipher, 32, new ISO7816d4Padding()); break; case ALG_DES_MAC8_ISO9797_M2: engine = new CBCBlockCipherMac(cipher, 64, new ISO7816d4Padding()); break; case ALG_DES_MAC8_ISO9797_1_M2_ALG3: engine = new ISO9797Alg3Mac(new DESEngine(), 64, new ISO7816d4Padding()); break; case ALG_DES_MAC4_PKCS5: engine = new CBCBlockCipherMac(cipher, 32, new PKCS7Padding()); break; case ALG_DES_MAC8_PKCS5: engine = new CBCBlockCipherMac(cipher, 64, new PKCS7Padding()); break; case ALG_AES_MAC_128_NOPAD: engine = new CBCBlockCipherMac(cipher, 128, null); break; case ALG_HMAC_SHA1: engine = new HMac(new SHA1Digest()); break; case ALG_HMAC_SHA_256: engine = new HMac(new SHA256Digest()); break; case ALG_HMAC_SHA_384: engine = new HMac(new SHA384Digest()); break; case ALG_HMAC_SHA_512: engine = new HMac(new SHA512Digest()); break; case ALG_HMAC_MD5: engine = new HMac(new MD5Digest()); break; case ALG_HMAC_RIPEMD160: engine = new HMac(new RIPEMD160Digest()); break; default: CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM); break; } engine.init(cipherParams); isInitialized = true; }
From source file:de.burlov.bouncycastle.io.CryptInputStream.java
License:Apache License
public CryptInputStream(InputStream in, BlockCipher cipher, byte[] key) throws IOException { byte[] iv = new byte[cipher.getBlockSize()]; /*//from w w w . j a v a 2s. c o m * IV aus Stream lesen */ DataInputStream din = new DataInputStream(in); din.readFully(iv); this.in = in; this.cipher = new EAXBlockCipher(cipher); this.cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); processedBuf = new byte[inputBuf.length + cipher.getBlockSize() * 10]; }
From source file:de.burlov.bouncycastle.io.CryptOutputStream.java
License:Apache License
/** * @throws IOException/*from w w w .ja v a 2s .c om*/ */ public CryptOutputStream(OutputStream out, BlockCipher cipher, byte[] key) throws IOException { if (out == null) { throw new NullPointerException("OutputStream is null"); } this.out = out; byte[] iv = new byte[cipher.getBlockSize()]; new Random().nextBytes(iv); this.cipher = new EAXBlockCipher(cipher); this.cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); out.write(iv); }
From source file:de.burlov.bouncycastle.io.test.EncryptOutputStreamTest.java
License:Apache License
@Test public void testEncryptDecrypt() throws IOException { BlockCipher cipher = new SerpentEngine(); Random rnd = new Random(); byte[] key = new byte[256 / 8]; rnd.nextBytes(key);// w w w . j a v a 2s. com byte[] iv = new byte[cipher.getBlockSize()]; rnd.nextBytes(iv); byte[] data = new byte[1230000]; new Random().nextBytes(data); ByteArrayOutputStream bout = new ByteArrayOutputStream(); CryptOutputStream eout = new CryptOutputStream(bout, cipher, key); eout.write(data); eout.close(); byte[] eData = bout.toByteArray(); // eData[1000] = (byte) (eData[1000] & 0x88); ByteArrayInputStream bin = new ByteArrayInputStream(eData); CryptInputStream din = new CryptInputStream(bin, cipher, key); bout = new ByteArrayOutputStream(); IOUtils.copy(din, bout); eData = bout.toByteArray(); Assert.assertTrue(Arrays.areEqual(data, eData)); }
From source file:edu.tamu.tcat.crypto.bouncycastle.SymmetricCipherBuilderImpl.java
License:Apache License
@Override public AEADSymmetricCipher buildAEADCipher(Cipher cipher, Mode mode, boolean encryption, byte[] key, byte[] iv) throws CipherException { ParametersWithIV cipherParameters = new ParametersWithIV(new KeyParameter(key), iv); BlockCipher underlyingCipher = null; int macSize = 0; switch (cipher) { case AES128:/*from ww w. jav a2s . c o m*/ case AES192: case AES256: underlyingCipher = new AESEngine(); break; } AEADBlockCipher aeadCipher = null; switch (mode) { case CBC: throw new CipherException(mode + " is not an authenticating encryption mode; use buildCipher instead"); case GCM: aeadCipher = new GCMBlockCipher(underlyingCipher); macSize = underlyingCipher.getBlockSize(); break; } aeadCipher.init(encryption, cipherParameters); return new BouncyCastleAEADCipher(aeadCipher, macSize, encryption); }
From source file:freenet.crypt.OCBBlockCipher_v149.java
License:Open Source License
public OCBBlockCipher_v149(BlockCipher hashCipher, BlockCipher mainCipher) { if (hashCipher == null) { throw new IllegalArgumentException("'hashCipher' cannot be null"); }/*from w w w. jav a 2 s . c o m*/ if (hashCipher.getBlockSize() != BLOCK_SIZE) { throw new IllegalArgumentException("'hashCipher' must have a block size of " + BLOCK_SIZE); } if (mainCipher == null) { throw new IllegalArgumentException("'mainCipher' cannot be null"); } if (mainCipher.getBlockSize() != BLOCK_SIZE) { throw new IllegalArgumentException("'mainCipher' must have a block size of " + BLOCK_SIZE); } if (!hashCipher.getAlgorithmName().equals(mainCipher.getAlgorithmName())) { throw new IllegalArgumentException("'hashCipher' and 'mainCipher' must be the same algorithm"); } this.hashCipher = hashCipher; this.mainCipher = mainCipher; }