How to Encrypt and Decrypt Data In Java Using DES Algorithm

Veröffentlicht am: 16 Februar 2017
auf dem Kanal: 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  
~-~~-~~~-~~-~


Auf dieser Seite können Sie das Online-Video How to Encrypt and Decrypt Data In Java Using DES Algorithm mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Inside App 16 Februar 2017 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2,631 Mal angesehen und es wurde von 14 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!