How to Encrypt and Decrypt Data In Java Using DES Algorithm

Pubblicato il: 16 febbraio 2017
sul canale di: Inside App
2,631
14

//How to Encrypt and Decrypt Data In Java Using DES Algorithm


Here are the general steps to encrypt/decrypt a file in Java:

Create a Key from a given byte array for a given algorithm.

Get an instance of Cipher class for a given algorithm transformation. See document of the Cipher class for more information regarding supported algorithms and transformations.

Initialize the Cipher with an appropriate mode (encrypt or decrypt) and the given Key.

Invoke doFinal(input_bytes) method of the Cipher class to perform encryption or decryption on the input_bytes, which returns an encrypted or decrypted byte array.

Read an input file to a byte array and write the encrypted/decrypted byte array to an output file accordingly.

Now, let’s see some real examples.

=============================================================
import java.io.*;
import javax.crypto.*;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
class Desal
{
public static void main(String []s)
{

try
{
String st="Hello";
System.out.println("Real String : "+ st);

byte str[]=st.getBytes();
Cipher c=Cipher.getInstance("DES");
KeyGenerator kg=KeyGenerator.getInstance("DES");
SecretKey sk=kg.generateKey();

//ENCRYPT_MODE
c.init(Cipher.ENCRYPT_MODE,sk);
byte ct[]=c.doFinal(str);
System.out.println("ENCRYPT_MODE DATA : "+ new String(ct));


//DECRYPT_MODE
c.init(Cipher.DECRYPT_MODE,sk);
byte ct1[]=c.doFinal(ct);
System.out.println("DECRYPT_MODE DATA : "+ new String(ct1));

}
catch(Exception e)
{
System.out.println(e);
}
}
}

==============================================
encryption and decryption in java example,
string encryption and decryption in java,
password encryption and decryption in java,
java code to encrypt and decrypt a string,
java code for encryption and decryption of files,
simple encryption and decryption in java,
encryption and decryption in java source code,
encryption and decryption in java example md5

www.patelwala.com
www.readranks.com

~-~~-~~~-~~-~
Please watch: "How to online money earn from Flipkart without selling any products"
   • Video  
~-~~-~~~-~~-~


In questa pagina del sito puoi guardare il video online How to Encrypt and Decrypt Data In Java Using DES Algorithm della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Inside App 16 febbraio 2017, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 2,631 volte e gli è piaciuto 14 spettatori. Buona visione!