Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Encryption

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Encryption

    i need,how to encrypt a user name and password in struts application could you help any one and where i need to start this work in struts f/w.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,431
    Thanks
    191
    Thanked 844 Times in 786 Posts
    Blog Entries
    5

    Default Re: Encryption

    Thread moved from 'whats wrong with my code'

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    175
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Encryption

    You're going to need to explain more if you want help.

  4. #4
    Junior Member Motion's Avatar
    Join Date
    Jul 2012
    Posts
    23
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Encryption

    i think you will have to get to the file that contains the username and password first using input stream

    eg:


    [code = java]

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    *
    * @author Harsoft
    */
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;

    import java.io.FileWriter;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import javax.swing.JOptionPane;

    public class JavaCrypt
    {
    public static void main(String[] args) throws Exception {

    File f=new File("D:/a.txt");
    int ch;

    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    try {
    fin = new FileInputStream(f);
    while ((ch = fin.read()) != -1)
    strContent.append((char) ch);
    fin.close();
    }
    catch (Exception e) {
    System.out.println(e);
    }

    System.out.println("Original string: " +strContent.toString()+"\n");
    // Get the KeyGenerator

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128); // 192 and 256 bits may not be available


    // Generate the secret key specs.
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");


    // Instantiate the cipher

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    byte[] encrypted = cipher.doFinal(strContent.toString().getBytes());

    try
    {
    FileWriter fw = new FileWriter(f, true);
    BufferedWriter br = new BufferedWriter(fw);

    fw.write("\n"+encrypted.toString());
    fw.close();
    br.close();
    JOptionPane.showMessageDialog(null, "File successfully written to"+f.getPath());
    }
    catch(Exception e)
    {

    }

    System.out.println("encrypted string: " + encrypted.toString());

    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] original =cipher.doFinal(encrypted);

    String originalString = new String(original);
    System.out.println("Original string: " +originalString);
    }
    }


    [/code]

Similar Threads

  1. [SOLVED] Encryption
    By Kseidel in forum Java Theory & Questions
    Replies: 5
    Last Post: November 22nd, 2012, 10:09
  2. Caesar Encryption
    By andrew_m428 in forum The Cafe
    Replies: 1
    Last Post: October 4th, 2012, 22:59
  3. Replies: 0
    Last Post: September 6th, 2012, 10:57
  4. [SOLVED] Need help with IDEA Encryption
    By finalforce in forum Algorithms & Recursion
    Replies: 1
    Last Post: June 4th, 2012, 03:41