Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > Convert Java crypto class code to ASP or .NET class code

Reply
Thread Tools

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

 
 
Wind
Guest
Posts: n/a
 
      02-05-2007
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).generateSe cret(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(utf;

// 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;
}
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any tool to convert java raw code (a la java 1.4) into generics code Royan Java 8 02-15-2008 02:35 PM
Crypto java (JCE api) - feed back no Java 0 12-14-2005 10:56 AM
Better crypto hash functions, long, with code Bryan Olson Python 2 08-26-2005 09:27 AM
Ruby & Java Crypto interoperability Andy Akins Ruby 1 07-11-2005 01:08 PM
How fast can Java run crypto in software based approach? Yu Java 0 09-14-2004 06:46 AM



Advertisments