Convert Java crypto class code to ASP or .NET class code

Discussion in 'ASP .Net Security' started by Wind, Feb 5, 2007.

  1. Wind

    Wind Guest

    I have a java class used for encryption and decryption using Triple DES but
    was unable to convert it to ASP or .NET class successfully.
    Can anyone help to convert it for me ? Thank you in advance.

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io_OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.security.Security;
    import java.security.spec.KeySpec;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESedeKeySpec;


    public class CryptoUtil {

    //This password length should be greater than or equal to 24 bytes
    String passPhrase = "abcdeabcdeabcdeabcdeabcdeabcdeabcdea";

    //Algorithm Name
    String algorithm = "TripleDES";
    Cipher ecipher;
    Cipher dcipher;

    // Buffer used to transport the bytes from one stream to another
    byte[] buf = new byte[1024];


    public CryptoUtil() {
    try {
    int ret = Security.addProvider(new com.sun.crypto.provider.SunJCE());
    if (ret >= 0)
    System.out.println("(Success) adding SunJCE provider at posotion " +
    ret);
    else
    System.out.println("(Success) adding SunJCE provider ... provider was
    previously added ");
    } catch (Exception ex) {
    System.out.println("(Error) adding SunJCE provider : " +
    ex.getMessage());
    }
    System.out.println("in");
    try {
    KeySpec keySpec = new DESedeKeySpec(passPhrase.getBytes());
    SecretKey key =
    SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec);

    ecipher = Cipher.getInstance(key.getAlgorithm());
    dcipher = Cipher.getInstance(key.getAlgorithm());

    ecipher.init(Cipher.ENCRYPT_MODE, key);
    dcipher.init(Cipher.DECRYPT_MODE, key);

    } catch (java.security.spec.InvalidKeySpecException e) {
    e.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e) {
    e.printStackTrace();
    } catch (java.security.NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (java.security.InvalidKeyException e) {
    e.printStackTrace();
    }
    }


    /**
    * @param str
    * @return
    */

    public String encrypt(String str) {
    try {
    // Encode the string into bytes using utf-8
    byte[] utf8 = str.getBytes("UTF8");

    // Encrypt
    byte[] enc = ecipher.doFinal(utf8);

    // Encode bytes to base64 to get a string
    return new sun.misc.BASE64Encoder().encode(enc);

    } catch (javax.crypto.BadPaddingException e) {
    e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    return null;
    }

    /**
    * @param str
    * @return
    */

    public String decrypt(String str) {
    try {
    // Decode base64 to get bytes
    byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

    // Decrypt
    byte[] utf8 = dcipher.doFinal(dec);

    // Decode using utf-8
    return new String(utf8, "UTF8");

    } catch (javax.crypto.BadPaddingException e) {
    e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (java.io.IOException e) {
    e.printStackTrace();
    }
    return null;
    }
    }
    Wind, Feb 5, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Nitulescu

    DES Crypto not working ???

    Alex Nitulescu, Feb 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    367
    Kevin
    Feb 13, 2005
  2. Yu
    Replies:
    0
    Views:
    383
  3. no
    Replies:
    0
    Views:
    400
  4. Bryan Olson
    Replies:
    2
    Views:
    432
    Paul Rubin
    Aug 26, 2005
  5. Andy Akins

    Ruby & Java Crypto interoperability

    Andy Akins, Jul 6, 2005, in forum: Ruby
    Replies:
    1
    Views:
    194
Loading...

Share This Page