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

W

Wind

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top