List of usage examples for org.bouncycastle.crypto.engines DESEngine DESEngine
public DESEngine()
From source file:com.licel.jcardsim.crypto.SymmetricKeyImpl.java
License:Apache License
/** * Return the BouncyCastle <code>BlockCipher</code> for using with this key * @return <code>BlockCipher</code> for this key, or null for HMACKey * @throws CryptoException if key not initialized * @see BlockCipher/*from www . j a v a 2s.c o m*/ */ public BlockCipher getCipher() throws CryptoException { if (!key.isInitialized()) { CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY); } BlockCipher cipher = null; switch (type) { case KeyBuilder.TYPE_DES: case KeyBuilder.TYPE_DES_TRANSIENT_DESELECT: case KeyBuilder.TYPE_DES_TRANSIENT_RESET: if (size == KeyBuilder.LENGTH_DES) { cipher = new DESEngine(); } if (size == KeyBuilder.LENGTH_DES3_2KEY || size == KeyBuilder.LENGTH_DES3_3KEY) { cipher = new DESedeEngine(); } break; case KeyBuilder.TYPE_AES: case KeyBuilder.TYPE_AES_TRANSIENT_DESELECT: case KeyBuilder.TYPE_AES_TRANSIENT_RESET: cipher = new AESEngine(); break; } return cipher; }
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); }/* w w w . j a v a 2 s. c om*/ 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:com.thoughtworks.go.security.DESEncrypter.java
License:Apache License
private static String decrypt(byte[] key, String cipherText) throws CryptoException { try {//w w w .ja va 2 s. co m PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); cipher.init(false, new KeyParameter(key)); byte[] cipherTextBytes = DECODER.decode(cipherText); byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)]; int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0); cipher.doFinal(plainTextBytes, outputLength); int paddingStarts = plainTextBytes.length - 1; for (; paddingStarts >= 0; paddingStarts--) { if (plainTextBytes[paddingStarts] != 0) { break; } } return new String(plainTextBytes, 0, paddingStarts + 1); } catch (Exception e) { throw new CryptoException(e); } }
From source file:com.thoughtworks.go.security.DESEncrypter.java
License:Apache License
private static String encrypt(byte[] key, String plainText) throws CryptoException { try {/* w ww . j a v a 2 s. co m*/ PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); KeyParameter keyParameter = new KeyParameter(key); cipher.init(true, keyParameter); byte[] plainTextBytes = plainText.getBytes(); byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)]; int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0); cipher.doFinal(cipherTextBytes, outputLength); return ENCODER.encodeToString(cipherTextBytes).trim(); } catch (Exception e) { throw new CryptoException(e); } }
From source file:com.thoughtworks.go.security.GoCipher.java
License:Apache License
String cipher(byte[] key, String plainText) throws InvalidCipherTextException { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); KeyParameter keyParameter = new KeyParameter(Hex.decode(key)); cipher.init(true, keyParameter);// w ww. j a va 2s . c o m byte[] plainTextBytes = plainText.getBytes(); byte[] cipherTextBytes = new byte[cipher.getOutputSize(plainTextBytes.length)]; int outputLength = cipher.processBytes(plainTextBytes, 0, plainTextBytes.length, cipherTextBytes, 0); cipher.doFinal(cipherTextBytes, outputLength); return Base64.encodeBase64String(cipherTextBytes).trim(); }
From source file:com.thoughtworks.go.security.GoCipher.java
License:Apache License
String decipher(byte[] key, String cipherText) throws InvalidCipherTextException { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESEngine())); cipher.init(false, new KeyParameter(Hex.decode(key))); byte[] cipherTextBytes = Base64.decodeBase64(cipherText); byte[] plainTextBytes = new byte[cipher.getOutputSize(cipherTextBytes.length)]; int outputLength = cipher.processBytes(cipherTextBytes, 0, cipherTextBytes.length, plainTextBytes, 0); cipher.doFinal(plainTextBytes, outputLength); int paddingStarts = plainTextBytes.length - 1; for (; paddingStarts >= 0; paddingStarts--) { if (plainTextBytes[paddingStarts] != 0) { break; }//from w w w . ja v a 2s . c om } return new String(plainTextBytes, 0, paddingStarts + 1); }
From source file:de.tsenger.animamea.crypto.AmDESCrypto.java
License:Open Source License
@Override public byte[] getMAC(byte[] data) { byte[] n = new byte[8 + data.length]; System.arraycopy(sscBytes, 0, n, 0, 8); System.arraycopy(data, 0, n, 8, data.length); BlockCipher cipher = new DESEngine(); Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding()); ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV); mac.init(parameterIV);//from w w w. ja va 2s . c om mac.update(n, 0, n.length); byte[] out = new byte[8]; mac.doFinal(out, 0); return out; }
From source file:de.tsenger.animamea.crypto.AmDESCrypto.java
License:Open Source License
@Override public byte[] getMAC(byte[] key, byte[] data) { BlockCipher cipher = new DESEngine(); Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding()); KeyParameter keyP = new KeyParameter(key); mac.init(keyP);/* www.j av a2s.c o m*/ mac.update(data, 0, data.length); byte[] out = new byte[8]; mac.doFinal(out, 0); return out; }
From source file:org.alfresco.jlan.client.J2MEPasswordEncryptor.java
License:Open Source License
/** * P16 encryption// www . j a v a2 s .c o m * * @param pwd java.lang.String * @param s8 byte[] * @return byte[] * @exception NoSuchAlgorithmException If a required encryption algorithm is not available */ public final byte[] P16(String pwd, byte[] s8) throws NoSuchAlgorithmException { // Make a 14 byte string using the password string. Truncate the // password or pad with nulls to 14 characters. StringBuffer p14str = new StringBuffer(); p14str.append(pwd.toUpperCase()); if (p14str.length() > 14) p14str.setLength(14); while (p14str.length() < 14) p14str.append((char) 0x00); // Convert the P14 string to an array of bytes. Allocate the return 16 byte array. byte[] p14 = p14str.toString().getBytes(); byte[] p16 = new byte[16]; try { // DES encrypt the password bytes using the challenge key DESEngine des = new DESEngine(); // Set the encryption seed using the first 7 bytes of the password string. // Generate the first 8 bytes of the return value. byte[] key = generateKey(p14, 0); KeyParameter chKey = new KeyParameter(key); des.init(true, chKey); des.processBlock(s8, 0, p16, 0); // Encrypt the second block key = generateKey(p14, 7); chKey = new KeyParameter(key); des.init(true, chKey); des.processBlock(s8, 0, p16, 8); } catch (Exception ex) { p16 = null; } // Return the 16 byte encrypted value return p16; }
From source file:org.alfresco.jlan.client.J2MEPasswordEncryptor.java
License:Open Source License
/** * P24 DES encryption/*from w ww . java2 s . c om*/ * * @param p21 Plain password or hashed password bytes * @param ch Challenge bytes * @return Encrypted password * @exception NoSuchAlgorithmException If a required encryption algorithm is not available */ protected byte[] P24(byte[] p21, byte[] ch) throws NoSuchAlgorithmException { byte[] enc = null; try { // DES encrypt the password bytes using the challenge key DESEngine des = new DESEngine(); // Allocate the output bytes enc = new byte[24]; // Encrypt the first block byte[] key = generateKey(p21, 0); KeyParameter chKey = new KeyParameter(key); des.init(true, chKey); des.processBlock(ch, 0, enc, 0); // Encrypt the second block key = generateKey(p21, 7); chKey = new KeyParameter(key); des.init(true, chKey); des.processBlock(ch, 0, enc, 8); // Encrypt the last block key = generateKey(p21, 14); chKey = new KeyParameter(key); des.init(true, chKey); des.processBlock(ch, 0, enc, 16); } catch (Exception ex) { ex.printStackTrace(); enc = null; } // Return the encrypted password, or null if an error occurred return enc; }