use of org.bouncycastle.crypto.modes.OFBBlockCipher in project robovm by robovm.
the class BaseBlockCipher method engineSetMode.
protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
modeName = Strings.toUpperCase(mode);
if (modeName.equals("ECB")) {
ivLength = 0;
cipher = new BufferedGenericBlockCipher(baseEngine);
} else if (modeName.equals("CBC")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new CBCBlockCipher(baseEngine));
} else if (modeName.startsWith("OFB")) {
ivLength = baseEngine.getBlockSize();
if (modeName.length() != 3) {
int wordSize = Integer.parseInt(modeName.substring(3));
cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, wordSize));
} else {
cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
}
} else if (modeName.startsWith("CFB")) {
ivLength = baseEngine.getBlockSize();
if (modeName.length() != 3) {
int wordSize = Integer.parseInt(modeName.substring(3));
cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, wordSize));
} else {
cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
}
} else // END android-removed
if (modeName.startsWith("SIC")) {
ivLength = baseEngine.getBlockSize();
if (ivLength < 16) {
throw new IllegalArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
}
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(baseEngine)));
} else if (modeName.startsWith("CTR")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(baseEngine)));
} else // END android-removed
if (modeName.startsWith("CTS")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new CTSBlockCipher(new CBCBlockCipher(baseEngine)));
} else if (modeName.startsWith("CCM")) {
ivLength = baseEngine.getBlockSize();
cipher = new AEADGenericBlockCipher(new CCMBlockCipher(baseEngine));
} else // END android-removed
if (modeName.startsWith("GCM")) {
ivLength = baseEngine.getBlockSize();
cipher = new AEADGenericBlockCipher(new GCMBlockCipher(baseEngine));
} else {
throw new NoSuchAlgorithmException("can't support mode " + mode);
}
}
use of org.bouncycastle.crypto.modes.OFBBlockCipher in project samourai-wallet-android by Samourai-Wallet.
the class AESUtil method decryptWithSetMode.
public static String decryptWithSetMode(String ciphertext, CharSequenceX password, int iterations, int mode, @Nullable BlockCipherPadding padding) throws InvalidCipherTextException, UnsupportedEncodingException, DecryptionException {
final int AESBlockSize = 4;
byte[] cipherdata = Base64.decodeBase64(ciphertext.getBytes());
// Separate the IV and cipher data
byte[] iv = copyOfRange(cipherdata, 0, AESBlockSize * 4);
byte[] input = copyOfRange(cipherdata, AESBlockSize * 4, cipherdata.length);
PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
generator.init(PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password.toString().toCharArray()), iv, iterations);
KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(256);
CipherParameters params = new ParametersWithIV(keyParam, iv);
BlockCipher cipherMode;
if (mode == MODE_CBC) {
cipherMode = new CBCBlockCipher(new AESEngine());
} else {
// mode == MODE_OFB
cipherMode = new OFBBlockCipher(new AESEngine(), 128);
}
BufferedBlockCipher cipher;
if (padding != null) {
cipher = new PaddedBufferedBlockCipher(cipherMode, padding);
} else {
cipher = new BufferedBlockCipher(cipherMode);
}
cipher.reset();
cipher.init(false, params);
// create a temporary buffer to decode into (includes padding)
byte[] buf = new byte[cipher.getOutputSize(input.length)];
int len = cipher.processBytes(input, 0, input.length, buf, 0);
len += cipher.doFinal(buf, len);
// remove padding
byte[] out = new byte[len];
System.arraycopy(buf, 0, out, 0, len);
// return string representation of decoded bytes
String result = new String(out, "UTF-8");
if (result.isEmpty()) {
throw new DecryptionException("Decrypted string is empty.");
}
return result;
}
use of org.bouncycastle.crypto.modes.OFBBlockCipher in project rxlib by RockyLOMO.
the class AesCrypt method getCipher.
@Override
protected StreamBlockCipher getCipher(boolean isEncrypted) throws InvalidAlgorithmParameterException {
AESFastEngine engine = new AESFastEngine();
StreamBlockCipher cipher;
if (_name.equals(CIPHER_AES_128_CFB)) {
cipher = new CFBBlockCipher(engine, getIVLength() * 8);
} else if (_name.equals(CIPHER_AES_192_CFB)) {
cipher = new CFBBlockCipher(engine, getIVLength() * 8);
} else if (_name.equals(CIPHER_AES_256_CFB)) {
cipher = new CFBBlockCipher(engine, getIVLength() * 8);
} else if (_name.equals(CIPHER_AES_128_OFB)) {
cipher = new OFBBlockCipher(engine, getIVLength() * 8);
} else if (_name.equals(CIPHER_AES_192_OFB)) {
cipher = new OFBBlockCipher(engine, getIVLength() * 8);
} else if (_name.equals(CIPHER_AES_256_OFB)) {
cipher = new OFBBlockCipher(engine, getIVLength() * 8);
} else {
throw new InvalidAlgorithmParameterException(_name);
}
return cipher;
}
use of org.bouncycastle.crypto.modes.OFBBlockCipher in project keepass2android by PhilippC.
the class JCEBlockCipher method engineSetMode.
protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
modeName = Strings.toUpperCase(mode);
if (modeName.equals("ECB")) {
ivLength = 0;
cipher = new BufferedGenericBlockCipher(baseEngine);
} else if (modeName.equals("CBC")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new CBCBlockCipher(baseEngine));
} else if (modeName.startsWith("OFB")) {
ivLength = baseEngine.getBlockSize();
if (modeName.length() != 3) {
int wordSize = Integer.parseInt(modeName.substring(3));
cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, wordSize));
} else {
cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
}
} else if (modeName.startsWith("CFB")) {
ivLength = baseEngine.getBlockSize();
if (modeName.length() != 3) {
int wordSize = Integer.parseInt(modeName.substring(3));
cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, wordSize));
} else {
cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
}
} else /*
else if (modeName.startsWith("PGP"))
{
boolean inlineIV = modeName.equalsIgnoreCase("PGPCFBwithIV");
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(
new PGPCFBBlockCipher(baseEngine, inlineIV));
}
else if (modeName.equalsIgnoreCase("OpenPGPCFB"))
{
ivLength = 0;
cipher = new BufferedGenericBlockCipher(
new OpenPGPCFBBlockCipher(baseEngine));
}
else if (modeName.startsWith("SIC"))
{
ivLength = baseEngine.getBlockSize();
if (ivLength < 16)
{
throw new IllegalArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
}
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(
new SICBlockCipher(baseEngine)));
}
else if (modeName.startsWith("CTR"))
{
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(
new SICBlockCipher(baseEngine)));
}
else if (modeName.startsWith("GOFB"))
{
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(
new GOFBBlockCipher(baseEngine)));
}
else if (modeName.startsWith("CTS"))
{
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new CTSBlockCipher(new CBCBlockCipher(baseEngine)));
}
else if (modeName.startsWith("CCM"))
{
ivLength = baseEngine.getBlockSize();
cipher = new AEADGenericBlockCipher(new CCMBlockCipher(baseEngine));
}
else if (modeName.startsWith("EAX"))
{
ivLength = baseEngine.getBlockSize();
cipher = new AEADGenericBlockCipher(new EAXBlockCipher(baseEngine));
}
else if (modeName.startsWith("GCM"))
{
ivLength = baseEngine.getBlockSize();
cipher = new AEADGenericBlockCipher(new GCMBlockCipher(baseEngine));
}
*/
{
throw new NoSuchAlgorithmException("can't support mode " + mode);
}
}
use of org.bouncycastle.crypto.modes.OFBBlockCipher in project XobotOS by xamarin.
the class JCEBlockCipher method engineSetMode.
protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
modeName = Strings.toUpperCase(mode);
if (modeName.equals("ECB")) {
ivLength = 0;
cipher = new BufferedGenericBlockCipher(baseEngine);
} else if (modeName.equals("CBC")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new CBCBlockCipher(baseEngine));
} else if (modeName.startsWith("OFB")) {
ivLength = baseEngine.getBlockSize();
if (modeName.length() != 3) {
int wordSize = Integer.parseInt(modeName.substring(3));
cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, wordSize));
} else {
cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
}
} else if (modeName.startsWith("CFB")) {
ivLength = baseEngine.getBlockSize();
if (modeName.length() != 3) {
int wordSize = Integer.parseInt(modeName.substring(3));
cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, wordSize));
} else {
cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
}
} else // END android-removed
if (modeName.startsWith("SIC")) {
ivLength = baseEngine.getBlockSize();
if (ivLength < 16) {
throw new IllegalArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
}
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(baseEngine)));
} else if (modeName.startsWith("CTR")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(baseEngine)));
} else if (modeName.startsWith("GOFB")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new GOFBBlockCipher(baseEngine)));
} else if (modeName.startsWith("CTS")) {
ivLength = baseEngine.getBlockSize();
cipher = new BufferedGenericBlockCipher(new CTSBlockCipher(new CBCBlockCipher(baseEngine)));
} else if (modeName.startsWith("CCM")) {
ivLength = baseEngine.getBlockSize();
cipher = new AEADGenericBlockCipher(new CCMBlockCipher(baseEngine));
} else // END android-removed
if (modeName.startsWith("GCM")) {
ivLength = baseEngine.getBlockSize();
cipher = new AEADGenericBlockCipher(new GCMBlockCipher(baseEngine));
} else {
throw new NoSuchAlgorithmException("can't support mode " + mode);
}
}
Aggregations