static Wrapper createRFC3211Wrapper(ASN1ObjectIdentifier algorithm) throws CMSException { if (NISTObjectIdentifiers.id_aes128_CBC.equals(algorithm) || NISTObjectIdentifiers.id_aes192_CBC.equals(algorithm) || NISTObjectIdentifiers.id_aes256_CBC.equals(algorithm)) { return new RFC3211WrapEngine(new AESEngine()); } else if (PKCSObjectIdentifiers.des_EDE3_CBC.equals(algorithm)) { return new RFC3211WrapEngine(new DESedeEngine()); } else if (OIWObjectIdentifiers.desCBC.equals(algorithm)) { return new RFC3211WrapEngine(new DESEngine()); } else if (PKCSObjectIdentifiers.RC2_CBC.equals(algorithm)) { return new RFC3211WrapEngine(new RC2Engine()); } else { throw new CMSException("cannot recognise wrapper: " + algorithm); } }
/** * Encrypt. * * @param instr the instr * @return the string * @throws java.security.GeneralSecurityException the general security exception */ @Override public String encrypt(String instr) throws GeneralSecurityException { long t1 = System.currentTimeMillis(); byte[] in = instr.getBytes(); PaddedBufferedBlockCipher encryptor = new PaddedBufferedBlockCipher( new CBCBlockCipher(new DESedeEngine())); encryptor.init(true, keyParameter); byte[] cipherText = new byte[encryptor.getOutputSize(in.length)]; int outputLen = encryptor.processBytes(in, 0, in.length, cipherText, 0); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { encryptor.doFinal(cipherText, outputLen); Hex.encode(cipherText, os); } catch (Exception e) { e.printStackTrace(); throw new GeneralSecurityException(e); } long t2 = System.currentTimeMillis(); logger.debug("Time taken to encrypt(millis) :" + (t2 - t1)); return ENC_PREFIX + os.toString(); }
private String decryptStr(String instr) throws GeneralSecurityException { if(StringUtils.isEmpty(instr)){ return instr; } long t1 = System.currentTimeMillis(); PaddedBufferedBlockCipher decryptor = new PaddedBufferedBlockCipher( new CBCBlockCipher(new DESedeEngine())); decryptor.init(false, keyParameter); byte[] in = null; byte[] cipherText = null; try { in = Hex.decode(instr); cipherText = new byte[decryptor.getOutputSize(in.length)]; int outputLen = decryptor.processBytes(in, 0, in.length, cipherText, 0); decryptor.doFinal(cipherText, outputLen); } catch (Exception e) { throw new GeneralSecurityException(e); } long t2 = System.currentTimeMillis(); logger.debug("Time taken to decrypt(millis) : " + (t2 - t1)); return (new String(cipherText)).replaceAll("\\u0000+$", ""); }
private static void initBlockCipherEngines() { blockCipherEngines.put("MARS", MarsEngine.class); blockCipherEngines.put("AES", AESEngine.class); blockCipherEngines.put("Blowfish", BlowfishEngine.class); blockCipherEngines.put("Camellia", CamelliaEngine.class); blockCipherEngines.put("CAST5", CAST5Engine.class); blockCipherEngines.put("CAST6", CAST6Engine.class); blockCipherEngines.put("DESede", DESedeEngine.class); blockCipherEngines.put("DES", DESEngine.class); blockCipherEngines.put("GOST28147", GOST28147Engine.class); blockCipherEngines.put("IDEA", IDEAEngine.class); blockCipherEngines.put("Noekeon", NoekeonEngine.class); blockCipherEngines.put("RC2", RC2Engine.class); blockCipherEngines.put("RC5", RC532Engine.class); blockCipherEngines.put("RC6", RC6Engine.class); blockCipherEngines.put("SEED", SEEDEngine.class); blockCipherEngines.put("Serpent", SerpentEngine.class); blockCipherEngines.put("Shacal2", Shacal2Engine.class); blockCipherEngines.put("Skipjack", SkipjackEngine.class); blockCipherEngines.put("SM4", SM4Engine.class); blockCipherEngines.put("TEA", TEAEngine.class); blockCipherEngines.put("Twofish", TwofishEngine.class); blockCipherEngines.put("XTEA", XTEAEngine.class); blockCipherEngines.put("Threefish", ThreefishEngine.class); }
public PKCS12PfxPdu generatePKCS12(X509Certificate caCert, Cert clientCert, String password) throws IOException, CertificateException, PKCSException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { PKCS12SafeBagBuilder caCertBagBuilder = new JcaPKCS12SafeBagBuilder(caCert); X509CertificateHolder clientCertHolder =new X509CertificateHolder(clientCert.getClientCert()); PKCS12SafeBagBuilder clientCertBagBuilder = new JcaPKCS12SafeBagBuilder(new JcaX509CertificateConverter().setProvider("BC").getCertificate(clientCertHolder)); PKCS12SafeBagBuilder keyBagBuilder = new JcaPKCS12SafeBagBuilder( KeyFactory.getInstance("RSA", "BC").generatePrivate(new PKCS8EncodedKeySpec(clientCert.getPrivateKey())), new BcPKCS12PBEOutputEncryptorBuilder( PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC, new CBCBlockCipher((new DESedeEngine()))).build(password.toCharArray())); PKCS12PfxPduBuilder pfxPduBuilder = new PKCS12PfxPduBuilder(); PKCS12SafeBag[] certs = new PKCS12SafeBag[2]; certs[0] = clientCertBagBuilder.build(); certs[1] = caCertBagBuilder.build(); pfxPduBuilder.addData(keyBagBuilder.build()); return pfxPduBuilder.build(new BcPKCS12MacCalculatorBuilder(), password.toCharArray()); }
private void initCiphers(byte[] key, byte[] iv) { // get the keyBytes keyBytes = new byte[key.length]; System.arraycopy(key, 0, keyBytes, 0, key.length); // get the IV IV = new byte[blockSize]; System.arraycopy(iv, 0, IV, 0, iv.length); keyP = new KeyParameter(keyBytes); encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher( new DESedeEngine()), new ISO7816d4Padding()); decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher( new DESedeEngine()), new ISO7816d4Padding()); // create the IV parameter ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV); encryptCipher.init(true, parameterIV); decryptCipher.init(false, parameterIV); }
@Test public void test() throws DataLengthException, IllegalStateException, InvalidCipherTextException { PaddedBufferedBlockCipher encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine()); PaddedBufferedBlockCipher decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine()); byte inBuff[] = "Hello Wd".getBytes(); byte[] outBuff = new byte[512]; byte[] keyBytes = "TestTestTestTest".getBytes(); byte[] uncipherData = new byte[8]; encryptCipher.init(true, new KeyParameter(keyBytes)); decryptCipher.init(false, new KeyParameter(keyBytes)); encryptCipher.processBytes(inBuff, 0, inBuff.length, outBuff, 0); encryptCipher.doFinal(outBuff, 0); decryptCipher.processBytes(outBuff, 0, 2*inBuff.length, uncipherData, 0); decryptCipher.doFinal(uncipherData, 0); log.debug("Uncipher Data: {}", uncipherData); assertTrue("Hello Wd".equals(new String(uncipherData))); }
private void process() { /* * Setup the DESede cipher engine, create a PaddedBufferedBlockCipher * in CBC mode. */ cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new DESedeEngine())); /* * The input and output streams are currently set up * appropriately, and the key bytes are ready to be * used. * */ if (encrypt) { performEncrypt(key); } else { performDecrypt(key); } // after processing clean up the files try { in.close(); out.flush(); out.close(); } catch (IOException closing) { } }
public IESwithDESede() { super(new IESEngine(new DHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), new PaddedBufferedBlockCipher(new DESedeEngine()))); }
public ECIESwithDESede() { super(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), new PaddedBufferedBlockCipher(new DESedeEngine()))); }
private void testCTRRandom() { DRBGTestVector tv = new DRBGTestVector( new DESedeEngine(), 168, new Bit232EntropyProvider().get(232), false, "20212223242526", 112, new String[] { "ABC88224514D0316EA3D48AEE3C9A2B4", "D3D3F372E43E7ABDC4FA293743EED076" } ); doCTRTest(tv); tv = new DRBGTestVector( new DESedeEngine(), 168, new Bit232EntropyProvider().get(232), true, "20212223242526", 112, new String[] { "64983055D014550B39DE699E43130B64", "035FDDA8582A2214EC722C410A8D95D3" } ) .setPersonalizationString("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C"); doCTRTest(tv); }
private X931TestVector[] createTestVectorData() { return new X931TestVector[] { new X931TestVector( new AESEngine(), new AES128EntropyProvider(), "f7d36762b9915f1ed585eb8e91700eb2", "259e67249288597a4d61e7c0e690afae", false, new String[] { "15f013af5a8e9df9a8e37500edaeac43", "a9d74bb1c90a222adc398546d64879cf", "0379e404042d58180764fb9e6c5d94bb", "3c74603e036d28c79947ffb56fee4e51", "e872101a4df81ebbe1e632fc87195d52", "26a6b3d33b8e7e68b75d9630ec036314" }), new X931TestVector( new DESedeEngine(), new TDESEntropyProvider(), "ef16ec643e5db5892cbc6eabba310b3410e6f8759e3e382c", "55df103deaf68dc4", false, new String[] { "9c960bb9662ce6de", "d9d0e527fd0931da", "3e2db9994e9e6995", "0e3868aef8218cf7", "7b0b0ca137f8fd81", "f657df270ad12265" }) }; }
private void performTests() throws Exception { testModes(new BlowfishEngine(), new BlowfishEngine(), 16); testModes(new DESEngine(), new DESEngine(), 8); testModes(new DESedeEngine(), new DESedeEngine(), 24); testModes(new TEAEngine(), new TEAEngine(), 16); testModes(new CAST5Engine(), new CAST5Engine(), 16); testModes(new RC2Engine(), new RC2Engine(), 16); testModes(new XTEAEngine(), new XTEAEngine(), 16); testModes(new AESEngine(), new AESEngine(), 16); testModes(new NoekeonEngine(), new NoekeonEngine(), 16); testModes(new TwofishEngine(), new TwofishEngine(), 16); testModes(new CAST6Engine(), new CAST6Engine(), 16); testModes(new SEEDEngine(), new SEEDEngine(), 16); testModes(new SerpentEngine(), new SerpentEngine(), 16); testModes(new RC6Engine(), new RC6Engine(), 16); testModes(new CamelliaEngine(), new CamelliaEngine(), 16); testModes(new ThreefishEngine(ThreefishEngine.BLOCKSIZE_512), new ThreefishEngine(ThreefishEngine.BLOCKSIZE_512), 64); testMode(new RC4Engine(), new KeyParameter(new byte[16])); testMode(new Salsa20Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testMode(new XSalsa20Engine(), new ParametersWithIV(new KeyParameter(new byte[32]), new byte[24])); testMode(new ChaChaEngine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testMode(new Grainv1Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testMode(new Grain128Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[12])); testMode(new HC128Engine(), new KeyParameter(new byte[16])); testMode(new HC256Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); testSkipping(new Salsa20Engine(), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[8])); testSkipping(new SICBlockCipher(new AESEngine()), new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); }
/** * Dekodiert einen Block mit DES * * @param key * Byte-Array enthält den 3DES-Schlüssel * @param z * verschlüsselter Block * @return entschlüsselter block */ @Override public byte[] decryptBlock(byte[] key, byte[] z) { byte[] s = new byte[16]; KeyParameter encKey = new KeyParameter(key); BlockCipher cipher = new DESedeEngine(); cipher.init(false, encKey); cipher.processBlock(z, 0, s, 0); return s; }
public TDESCrypto(byte[] key) { encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine()); decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine()); encryptCipher.init(true, new KeyParameter(key)); decryptCipher.init(false, new KeyParameter(key)); this.key = key; }
public void testBcEncryptedPrivateKeyInfo() throws Exception { KeyFactory fact = KeyFactory.getInstance("RSA", BC); PrivateKey privKey = fact.generatePrivate(privKeySpec); PKCS8EncryptedPrivateKeyInfoBuilder builder = new JcaPKCS8EncryptedPrivateKeyInfoBuilder(privKey); PKCS8EncryptedPrivateKeyInfo priv = builder.build(new BcPKCS12PBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC, new CBCBlockCipher(new DESedeEngine())).build(passwd)); PrivateKeyInfo info = priv.decryptPrivateKeyInfo(new BcPKCS12PBEInputDecryptorProviderBuilder().build(passwd)); assertTrue(Arrays.areEqual(info.getEncoded(), privKey.getEncoded())); }
public DESede_CFB8() { super(new CFBBlockCipher(new DESedeEngine(), 8), 64); }
public DESede_OFB8() { super(new OFBBlockCipher(new DESedeEngine(), 8), 64); }
public BrokePBEWithSHAAndDES3Key() { super(new CBCBlockCipher(new DESedeEngine()), PKCS12, SHA1, 192, 64); }
public OldPBEWithSHAAndDES3Key() { super(new CBCBlockCipher(new DESedeEngine()), OLD_PKCS12, SHA1, 192, 64); }
public BrokePBEWithSHAAndDES2Key() { super(new CBCBlockCipher(new DESedeEngine()), PKCS12, SHA1, 128, 64); }