EncriptDecriptArchivo.TestBouncy.java Source code

Java tutorial

Introduction

Here is the source code for EncriptDecriptArchivo.TestBouncy.java

Source

/*
 * Copyright (C) 2015 Adrin Ledo
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package EncriptDecriptArchivo;

import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.paddings.*;
import org.bouncycastle.crypto.params.*;

public class TestBouncy {

    BlockCipher engine = new DESEngine();

    public byte[] Encrypt(String keys, byte[] plainText) {
        byte[] key = keys.getBytes();
        byte[] ptBytes = plainText;
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
        cipher.init(true, new KeyParameter(key));
        byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)];
        int tam = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0);
        try {
            cipher.doFinal(rv, tam);
        } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ce) {
            ce.printStackTrace();
        }
        return rv;
    }

    public byte[] Decrypt(String key2, byte[] cipherText) {
        byte[] key = key2.getBytes();
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));
        cipher.init(false, new KeyParameter(key));
        byte[] rv = new byte[cipher.getOutputSize(cipherText.length)];
        int tam = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0);
        try {
            cipher.doFinal(rv, tam);
        } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ce) {
            ce.printStackTrace();
        }
        return rv;
    }

}