public String testEncryptRijndael(String value,String key) throws DataLengthException, IllegalStateException, InvalidCipherTextException { BlockCipher engine = new RijndaelEngine(256); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine), new ZeroBytePadding()); byte[] keyBytes = key.getBytes(); cipher.init(true, new KeyParameter(keyBytes)); byte[] input = value.getBytes(); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int cipherLength = cipher.processBytes(input, 0, input.length, cipherText, 0); cipher.doFinal(cipherText, cipherLength); String result = new String(Base64.encode(cipherText)); //Log.e("testEncryptRijndael : " , result); return result; }
public void encrypt(byte[] passiveCheckBytes, byte[] initVector, String password) { RijndaelEngine engine = new RijndaelEngine(_keyByteLength * 8); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CFBBlockCipher(engine, 8), new ZeroBytePadding()); try { byte[] sessionKey = new byte[_keyByteLength]; byte[] passwordBytes = password.getBytes("US-ASCII"); System.arraycopy(passwordBytes, 0, sessionKey, 0, Math.min(_keyByteLength, passwordBytes.length)); byte[] iv = new byte[_keyByteLength]; System.arraycopy(initVector, 0, iv, 0, Math.min(_keyByteLength, initVector.length)); cipher.init(true, new ParametersWithIV(new KeyParameter(sessionKey), iv)); byte[] cipherText = new byte[cipher.getOutputSize(passiveCheckBytes.length)]; int cipherLength = cipher.processBytes(passiveCheckBytes, 0, passiveCheckBytes.length, cipherText, 0); cipherLength = cipherLength + cipher.doFinal(cipherText, cipherLength); int bytesToCopy = Math.min(passiveCheckBytes.length, cipherLength); System.arraycopy(cipherText, 0, passiveCheckBytes, 0, bytesToCopy); } catch (Exception e) { throw new RuntimeException(e); } }
public void setParameters(int KeySize, int alg) throws NoSuchAlgorithmException, InvalidKeySpecException { switch (alg) { case 7: case 1: this.blockSize = KeySize; break; case 8: this.blockSize = 256; break; default: this.blockSize = 128; break; } switch (KeySize) { case 128: case 192: Algs[1] = new RijndaelEngine(KeySize); break; case 256: Algs[1] = new RijndaelEngine(KeySize); Algs[7] = new ThreefishEngine(KeySize); break; default: Algs[7] = new ThreefishEngine(KeySize); break; } padding = new PKCS7Padding(); }
public ECB() { super(new BlockCipherProvider() { public BlockCipher get() { return new RijndaelEngine(); } }); }
public String testDecryptRijndael(String value,String key) throws DataLengthException, IllegalStateException, InvalidCipherTextException { BlockCipher engine = new RijndaelEngine(256); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine), new ZeroBytePadding()); byte[] keyBytes = key.getBytes(); cipher.init(false, new KeyParameter(keyBytes)); byte[] output = Base64.decode(value.getBytes()); byte[] cipherText = new byte[cipher.getOutputSize(output.length)]; int cipherLength = cipher.processBytes(output, 0, output.length, cipherText, 0); int outputLength = cipher.doFinal(cipherText, cipherLength); outputLength += cipherLength; byte[] resultBytes = cipherText; if (outputLength != output.length) { resultBytes = new byte[outputLength]; System.arraycopy( cipherText, 0, resultBytes, 0, outputLength ); } String result = new String(resultBytes); return result; }
RijndaelTest() { super(tests, new RijndaelEngine(128), new KeyParameter(new byte[16])); }
private BufferedBlockCipher createCipher( boolean encryptor, byte[] key, byte[] iv ) { BufferedBlockCipher cipher = new BufferedBlockCipher( new CFBBlockCipher( new RijndaelEngine( 128 ), 8 ) ); cipher.init( encryptor, new ParametersWithIV( new KeyParameter( key ), iv ) ); return cipher; }