Skip to main content
Home
Live to Learn!

Main navigation

  • Home
  • Learn
    • FoxPro
    • MS-DOS
    • C PRG
    • Java
    • ASP
    • Ruby on Rails
    • ASP.NET
    • E-Books
    • Exam Preparation
    • Tools
  • Blog
  • Forums
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Learn by Example - Java Sample Programs

DES - Using Data Encryption Standard in Java

Book navigation

  • Armstrong Number
  • Palindrome Checking
  • Playing Audio Clip using Applet
  • Applet Form
  • Exception Handling
  • Multiple Inheritance and Packages
  • Shopping List
  • Simple Multithread program
  • JDBC Program Select, Insert, Update, Delete records
  • RMI - Example AddServer
  • Simple Bank Account Process
  • TCP Server and Client in Java
  • UDP Server and Client in Java
  • FTP Server and Client in Java
  • Chat Server and Client in Java
  • Echo Server and Client in Java
  • Address Resolution Protocol in Java
  • Ping server and Client in Java
  • Multicast Server and Client in Java
  • Transposition Cipher Method
  • Poly-alphabetic Cipher Method Encryption - Java
  • DES - Using Data Encryption Standard in Java
  • AES - Using Advanced Encryption Standard in Java
  • Bit Stuffing
By Karthikeyan , 16 November, 2012

A simple java program that uses of DES encryption in Java


import java.io.*;
import java.security.*;
import javax.crypto.*;
import java.util.*;
public class DES {
    public static void main(String args[])
    {
        try
        {
            Cipher desCipher = Cipher.getInstance("DES");
            KeyGenerator keygen = KeyGenerator.getInstance("DES");
            SecureRandom randomSeed = new SecureRandom();
            keygen.init(randomSeed);
            Key deskey = keygen.generateKey();
            int mode = Cipher.ENCRYPT_MODE;
            desCipher.init(mode, deskey);
            Scanner console = new Scanner(System.in);
            System.out.println("Enter a text for encryption");
            String plainText = console.nextLine();
            String encryptedText = crypt(desCipher,plainText);
            System.out.println("The encrypted text is " +encryptedText);
            mode = Cipher.DECRYPT_MODE;
            desCipher.init(mode, deskey);
            String decryptedText = crypt(desCipher,encryptedText);
            System.out.println("The decrypted text is "+decryptedText);
            
        }
        catch(GeneralSecurityException e)
                {
                    System.out.println("Security Exception :"+ e.getMessage());
                }
    }
    
    public static String crypt(Cipher desCipher, String in) throws GeneralSecurityException
    {
        int inSize = desCipher.getBlockSize();
        byte[] inBytes = new byte[inSize];
        inBytes = in.getBytes();
        int outSize = desCipher.getOutputSize(inSize);
        byte[] outBytes = new byte[outSize];
        outBytes = desCipher.doFinal(inBytes);
        String out = new String(outBytes);
        return out;
                
    }
}

Comments

Featured Blog Posts

Convert Currency in Number to Words (Indian Rupees) - MS Excel
Foxpro Tutorial and Programs
Convert Currency in Number to Words in MS Word
Convert Currency in Number to Words (Indian Rupees) - Version 2
Best way to Use Rupee Symbol in Windows – Easy steps
Convert Currency in Number to Words - MS Access
Creating All in One Windows XP DVD with all Important Applications
RSS feed

© 2009-2025 Live to Learn.In

Terms of Use | Privacy Policy