Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication3;
- import java.io.*;
- import java.security.InvalidKeyException;
- import java.security.*;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.spec.SecretKeySpec;
- /**
- * A utility class that encrypts or decrypts a file.
- * @author www.codejava.net
- *
- */
- public class CryptoUtils {
- public static byte[] encrypt(String ALGORITHM, String key, byte[] inputBytes)
- throws CryptoException {
- if(ALGORITHM.equals("AES") || ALGORITHM.equals("Blowfish")){
- if(key.length()>16)
- key = key.substring(0,16);
- else if(key.length()<16)
- key = key + key.substring(0, 16-key.length() );
- }
- else if(ALGORITHM.equals("DES")){
- if(key.length()>8)
- key = key.substring(0,8);
- else if(key.length()<8)
- key = key + key.substring(0, 8-key.length() );
- }
- return doCrypto(ALGORITHM, Cipher.ENCRYPT_MODE, key, inputBytes);
- }
- public static byte[] decrypt(String ALGORITHM, String key, byte[] inputBytes)
- throws CryptoException {
- if(ALGORITHM.equals("AES") || ALGORITHM.equals("Blowfish")){
- if(key.length()>16)
- key = key.substring(0,16);
- if(key.length()<16)
- key = key + key.substring(0, 16-key.length() );
- }
- else if(ALGORITHM.equals("DES")){
- if(key.length()>8)
- key = key.substring(0,8);
- else if(key.length()<8)
- key = key + key.substring(0, 8-key.length() );
- }
- return doCrypto(ALGORITHM, Cipher.DECRYPT_MODE, key, inputBytes);
- }
- public static byte[] encryptRSA(PublicKey publicKey, String message) throws Exception {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- return cipher.doFinal(message.getBytes());
- }
- public static byte[] decryptRSA(PrivateKey privateKey, byte [] encrypted) throws Exception {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- return cipher.doFinal(encrypted);
- }
- private static byte[] doCrypto(String ALGORITHM, int cipherMode, String key, byte[] inputBytes) throws CryptoException {
- try {
- Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- cipher.init(cipherMode, secretKey);
- byte[] outputBytes = cipher.doFinal(inputBytes);
- return outputBytes;
- } catch (NoSuchPaddingException | NoSuchAlgorithmException
- | InvalidKeyException | BadPaddingException
- | IllegalBlockSizeException ex) {
- }
- return null;
- }
- public class CryptoException extends Exception {
- public CryptoException() {
- }
- public CryptoException(String message, Throwable throwable) {
- super(message, throwable);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement