Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

encryption

class EncryptDecrypt {

    static String encrypt(String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(Constants.Encryption.INIT_VECTOR.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new
                    SecretKeySpec("PRIVATE_KEY_FOR_ENCRYPTION_OR_DECRYPTION"
                    .getBytes(StandardCharsets.UTF_8), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());
            byte[] original = Base64.getEncoder().encode(encrypted);
            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    static String decrypt(String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(Constants.Encryption.INIT_VECTOR
                    .getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec("PRIVATE_KEY_FOR_ENCRYPTION_OR_DECRYPTION".
                    getBytes(StandardCharsets.UTF_8), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));
            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }
Comment

encryption


package com.javacodegeeks.snippets.core;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
 
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
 
public class Main {
 
    static Cipher ce;
    static Cipher cd;
 
    public static void main(String args[]) throws Exception {
 
  Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
 
  SecretKey skey = KeyGenerator.getInstance("DES").generateKey();
 
  byte[] initializationVector = new byte[]{0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02};
 
  AlgorithmParameterSpec algParameters = new IvParameterSpec(initializationVector);
 
  ce = Cipher.getInstance("DES/CBC/PKCS5Padding");
 
  cd = Cipher.getInstance("DES/CBC/PKCS5Padding");
 
  ce.init(Cipher.ENCRYPT_MODE, skey, algParameters);
 
  cd.init(Cipher.DECRYPT_MODE, skey, algParameters);
 
  FileInputStream is = new FileInputStream("C:/Users/nikos7/Desktop/output.txt");
 
  FileOutputStream os = new FileOutputStream("C:/Users/nikos7/Desktop/output2.txt");
 
  int dataSize = is.available();
 
  byte[] inbytes = new byte[dataSize];
 
  is.read(inbytes);
 
  String str2 = new String(inbytes);
 
  System.out.println("Input file contentn" + str2 + "n");
 
  write_encode(inbytes, os);
 
  os.flush();
 
  is.close();
 
  os.close();
 
  System.out.println("Ecrypted Content to output2.txtn");
 
  is = new FileInputStream("C:/Users/nikos7/Desktop/output2.txt");
 
  byte[] decBytes = new byte[dataSize];
 
  read_decode(decBytes, is);
 
  is.close();
 
  String str = new String(decBytes);
 
  System.out.println("Decrypted file contents:n" + str);
 
    }
 
    public static void write_encode(byte[] bytes, OutputStream output) throws Exception {
 
  CipherOutputStream cOutputStream = new CipherOutputStream(output, ce);
 
  cOutputStream.write(bytes, 0, bytes.length);
 
  cOutputStream.close();
    }
 
    public static void read_decode(byte[] bytes, InputStream input) throws Exception {
 
  CipherInputStream cInputStream = new CipherInputStream(input, cd);
 
  int position = 0, i;
 
  while ((i = cInputStream.read()) != -1) {
 
bytes[position] = (byte) i;
 
position++;
 
  }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: default constructor 
Java :: navigation graph setup android 
Java :: java comment 
Java :: is it possible to declare two conditions in for loop in javascrip 
Java :: Java Hasmap Remove Elements 
Java :: void * to int 
Java :: java check if dates are the same day 
Java :: list of list in java 
Java :: how to initialize a variable in java 
Java :: java returns null 
Java :: Java short Keyword 
Java :: nested class in java 8 
Java :: get the average of an array in java 
Java :: how to use another class in java inside of an action listenrer 
Java :: labeled statement in java ex 
Java :: stream reduce stringbuilder 
Java :: how to change default port in spring boot 
Java :: control flow graph generator 
Java :: android java close keyboard 
Java :: coin flip random java 
Java :: sha-1 key generator 
Java :: java stream list order lambda 
Java :: Java Catching base exception class only 
Java :: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 
Java :: string equlity 
Java :: more parameters java sql @PathVariable 
Java :: how to preset an array java 
Java :: android diagonal gradle 
Java :: set jtextfield max length 
Java :: sfadffocusbutton 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =