Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Decryption of Cipher encrypted through DESCryptoServiceProvider in ASP.NET

843811Nov 6 2008 — edited Nov 6 2008
Hello Experts,

I am trying to decrypt the ciphertext in java that was encrypted using DESCryptoServiceProvider Class of ASP.Net. I am able to do it till some extent but its not completely decrypting.
Here i am putting the code of both (Encryption through ASP.NET and Decryption through Java)

ASP.NET Code for encryption
private string Encrypt(string strText, string encryptionKey)
{
// Here "strText" is the text to encrypt and "encryptionKey" is the secret key used for encryption
byte[] byKey = new byte[20];
byte[] dv ={ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);
cs.Write(inputArray, 0, inputArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
throw ex;

}

}
Java Class for decryption


import java.security.Security;
import java.io.*;*
*import java.net.*;
import javax.servlet.*;*
*import javax.servlet.http.*;
import javax.crypto.*;*
*import javax.crypto.spec.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class MainClass {

public static String fromBase64(String in) throws IOException {
return new String(new BASE64Decoder().decodeBuffer(in));
}


public static String toBase64(String in) {
return new String(new BASE64Encoder().encodeBuffer(in.getBytes()));
}

public static void main(String[] args) throws Exception { 
MainClass decryptionClass = new MainClass();
decryptionClass.decrypt("EncryptedPassword");
}


public void decrypt(String myPassword){
try{
String key = "xxxxxxxx"; //same encryptionKey that is used in the ASP.NET code

byte[] IVBytes = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF};
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes( "UTF-8" ));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey skey = keyFactory.generateSecret(desKeySpec);

Cipher cipher = Cipher.getInstance( "DES/CBC/NoPadding" );

byte[] mykeybytes = new byte[20];
mykeybytes = key.substring(0,8).getBytes( "UTF-8" );

IvParameterSpec iv = new IvParameterSpec(IVBytes);

cipher.init(Cipher.DECRYPT_MODE, skey, iv);

String baseString = MainClass.fromBase64( myPassword );

byte[] keyByteArray = baseString.getBytes();

byte[] original = cipher.doFinal( keyByteArray );

String pwd = new String( original,"UTF-8");

System.out.println( "Original password : "+pwd );

} catch ( Exception e ) {
e.printStackTrace(); 
} 
}
}
It is decrypting the CipherTexts till some characters but not completely. What i am missing in my java code ??

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 4 2008
Added on Nov 6 2008
7 comments
1,100 views