DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2006
    Posts
    12

    Unhappy how to print encrypted string in this encrypting/decrypting code ?

    Hi all,
    here is full working code for encrypting and decrypting a string. It encrypts entered string and the decrypts it. It doesnt shows the encrypted string.

    http://www.java-tips.org/java-se-ti...etric-keys.html

    I just wanted to know how can i show enrypted string too.
    PLEASE HELP...!!!

    thanks,

  2. #2
    Join Date
    Apr 2006
    Posts
    28
    kindly paste the complete url. (error)

    cheers

  3. #3
    Join Date
    Mar 2006
    Posts
    12
    FILE NAME : LocalEncrypter.java


    import javax.crypto.Cipher;
    import javax.crypto.BadPaddingException;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import java.security.Key;
    import java.security.InvalidKeyException;

    public class LocalEncrypter {

    private static String algorithm = "DESede";
    private static Key key = null;
    private static Cipher cipher = null;

    private static void setUp() throws Exception {
    key = KeyGenerator.getInstance(algorithm).generateKey();
    cipher = Cipher.getInstance(algorithm);
    }

    public static void main(String[] args)
    throws Exception {
    setUp();
    if (args.length !=1) {
    System.out.println(
    "USAGE: java LocalEncrypter " +
    "[String]");
    System.exit(1);
    }
    byte[] encryptionBytes = null;
    String input = args[0];
    System.out.println("Entered: " + input);
    encryptionBytes = encrypt(input);
    System.out.println(
    "Recovered: " + decrypt(encryptionBytes));
    }

    private static byte[] encrypt(String input)
    throws InvalidKeyException,
    BadPaddingException,
    IllegalBlockSizeException {
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = input.getBytes();
    return cipher.doFinal(inputBytes);
    }

    private static String decrypt(byte[] encryptionBytes)
    throws InvalidKeyException,
    BadPaddingException,
    IllegalBlockSizeException {
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] recoveredBytes =
    cipher.doFinal(encryptionBytes);
    String recovered =
    new String(recoveredBytes);
    return recovered;
    }
    }

  4. #4
    Join Date
    Apr 2006
    Posts
    28
    ok i think i have done what u had asked for.

    lemme know if it works for u.

    Code:
    import javax.crypto.Cipher;
    import javax.crypto.BadPaddingException;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.KeyGenerator;
    import java.security.Key;
    import java.security.InvalidKeyException;
    
    public class LocalEncrypter {
    
    private static String algorithm = "DESede";
    private static Key key = null;
    private static Cipher cipher = null;
    
    private static void setUp() throws Exception {
    key = KeyGenerator.getInstance(algorithm).generateKey(); 
    cipher = Cipher.getInstance(algorithm);
    }
    
    public static String asHex (byte buf[]) {
          StringBuffer strbuf = new StringBuffer(buf.length * 2);
          int i;
    
          for (i = 0; i < buf.length; i++) {
           if (((int) buf[i] & 0xff) < 0x10)
    	    strbuf.append("0");
    
           strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
          }
    
          return strbuf.toString();
         }
    
    public static void main(String[] args) 
    throws Exception {
    setUp();
    if (args.length !=1) {
    System.out.println(
    "USAGE: java LocalEncrypter " +
    "[String]");
    System.exit(1);
    }
    byte[] encryptionBytes = null;
    String input = args[0];
    System.out.println("Entered: " + input);
    
    encryptionBytes = encrypt(input);
    System.out.println(
    "Recovered: " + decrypt(encryptionBytes));
    System.out.println("Encrypted String: " + asHex(encryptionBytes));
    }
    
    private static byte[] encrypt(String input)
    throws InvalidKeyException, 
    BadPaddingException,
    IllegalBlockSizeException {
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = input.getBytes();
    return cipher.doFinal(inputBytes);
    }
    
    private static String decrypt(byte[] encryptionBytes)
    throws InvalidKeyException, 
    BadPaddingException,
    IllegalBlockSizeException {
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] recoveredBytes = 
    cipher.doFinal(encryptionBytes);
    String recovered = 
    new String(recoveredBytes);
    return recovered;
    }
    private static String getBytes(String input) {
            return null;
        }
    
    }

Similar Threads

  1. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 02:32 PM
  2. Writing in HKEY_LOCAL_MACHINE...Access is denied
    By Martin in forum VB Classic
    Replies: 22
    Last Post: 12-03-2001, 03:53 AM
  3. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  4. Deadlock error.. how to remove
    By Chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 12:52 PM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links