Scooter wrote:
> I have been tasked with writing a small java class that will decrypt a
> string that was encrypted with a C# app. I have the source for the C#
> decryption, its nothing special. I just dont know what the
> counterparts are in java.
>
> Heres the C# code. "m_objDecrypt" is a "ICryptoTransform"
>
> protected internal string DecryptValue(string EncryptedValue)
> {
> byte[] buffer;
> if (EncryptedValue.Length >= 0x800)
> {
> throw new ValueException();
> }
> try
> {
> byte[] inputBuffer = Convert.FromBase64String(EncryptedValue);
> buffer = this.m_objDecrypt.TransformFinalBlock(inputBuffer, 0,
> inputBuffer.Length);
> }
> catch (FormatException)
> {
> throw new ValueException();
> }
> catch (CryptographicException)
> {
> throw new ValueException();
> }
> return new ASCIIEncoding().GetString(buffer);
> }
>
> --------------------------
> in my java class so far I have:
>
> byte[] buffer;
>
> if (EncryptedValue.length() >= 0x800) {
>
>
> }
>
> try
> {
> byte[]inputBuffer=new sun.misc.BASE64Decoder().decodeBuffer
> (EncryptedValue);
> //buffer=
> }
> catch (Exception e)
> {
>
> }
>
>
> I just have no idea about converting that whole "TransformFinalBlock"
> method.
The Java equivalent to C# ICryptoTransform.TransformFinalBlock is
Cipher.doFinal !
Attached below are some C# code and Java code that does the
same thing.
The use Hex not Base64 though.
Arne
===========================================
using System;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
namespace E
{
public class Program
{
public static string ToHex(byte[] ba)
{
StringBuilder sb = new StringBuilder(2 * ba.Length);
for(int i = 0; i < ba.Length; i++)
{
sb.Append(ba[i].ToString("X2"));
}
return sb.ToString();
}
public static byte[] FromHex(string s)
{
byte[] ba = new byte[s.Length/2];
for(int i = 0; i < ba.Length; i++)
{
ba[i] = byte.Parse(s.Substring(2 * i, 2),
NumberStyles.HexNumber);
}
return ba;
}
public static string Encrypt(string s)
{
TripleDESCryptoServiceProvider tdes = new
TripleDESCryptoServiceProvider();
tdes.Key = Encoding.UTF8.GetBytes("MySecretKeyxxxxx");
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform crypt = tdes.CreateEncryptor();
byte[] plain = Encoding.UTF8.GetBytes(s);
byte[] cipher = crypt.TransformFinalBlock(plain, 0,
plain.Length);
return ToHex(cipher);
}
public static string Decrypt(string s)
{
TripleDESCryptoServiceProvider tdes = new
TripleDESCryptoServiceProvider();
tdes.Key = Encoding.UTF8.GetBytes("MySecretKeyxxxxx");
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform crypt = tdes.CreateDecryptor();
byte[] cipher = FromHex(s);
byte[] plain = crypt.TransformFinalBlock(cipher, 0 ,
cipher.Length);
return Encoding.UTF8.GetString(plain);
}
public static void Main(string[] args)
{
string p1 = "Dette er en lille test ABC ÆØÅ 123 !!!!";
Console.WriteLine(p1);
string c = Encrypt(p1);
Console.WriteLine(c);
string p2 = Decrypt(c);
Console.WriteLine(p2);
}
}
}
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class TrippleDES {
static {
Security.addProvider(new
org.bouncycastle.jce.provider.BouncyCastleProvider ());
}
public static String encrypt(String s) throws
NoSuchAlgorithmException, NoSuchPaddingException,
UnsupportedEncodingException, IllegalBlockSizeException,
BadPaddingException, InvalidKeyException {
Cipher crypt = Cipher.getInstance("DESede/ECB/PKCS7Padding");
SecretKey key = new
SecretKeySpec("MySecretKeyxxxxx".getBytes(), "DESede");
crypt.init(Cipher.ENCRYPT_MODE, key);
byte[] plain = s.getBytes("UTF-8");
byte[] cipher = crypt.doFinal(plain, 0, plain.length);
return toHex(cipher);
}
public static String decrypt(String s) throws
NoSuchAlgorithmException, NoSuchPaddingException,
UnsupportedEncodingException, IllegalBlockSizeException,
BadPaddingException, InvalidKeyException {
Cipher crypt = Cipher.getInstance("DESede/ECB/PKCS7Padding");
SecretKey key = new
SecretKeySpec("MySecretKeyxxxxx".getBytes(), "DESede");
crypt.init(Cipher.DECRYPT_MODE, key);
byte[] cipher = fromHex(s);
byte[] plain = crypt.doFinal(cipher, 0, cipher.length);
return new String(plain, "UTF-8");
}
private static String toHex(byte[] ba) {
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < ba.length; i++) {
sb.append(Integer.toHexString((ba[i] >> 4) & 0x0F));
sb.append(Integer.toHexString(ba[i] & 0x0F));
}
return sb.toString();
}
private static byte[] fromHex(String s) {
int n = s.length() / 2;
byte[] res = new byte[n];
for(int i = 0; i < n; i++) {
res[i] = (byte)(Integer.parseInt(s.substring(2 * i, 2 * i +
2), 16));
}
return res;
}
public static void main(String[] args) throws Exception {
String p1 = "Dette er en lille test ABC ÆØÅ 123 !!!!";
System.out.println(p1);
String c = encrypt(p1);
System.out.println(c);
String p2 = decrypt(c);
System.out.println(p2);
}
}
|