Não consegui fazer bater a criptografia.
Agora estamos tentando usar o 3DES mas mesmo assim não está rolando.
código c# Resultado = l2DWkv2JTeM=
public class Criptografia
{
private static byte[] GetKey(string password)
{
string pwd = null;
if (Encoding.UTF8.GetByteCount(password) < 24)
pwd = password.PadRight(24, ' ');
else
pwd = password.Substring(0, 24);
return Encoding.UTF8.GetBytes(pwd);
}
public static string Encrypt(string data, string password)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
DES.Key = GetKey(password);
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
Byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(data);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
public static string Decrypt(string data, string password)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
DES.Key = GetKey(password);
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESEncrypt = DES.CreateDecryptor();
Byte[] Buffer = Convert.FromBase64String(data);
return Encoding.UTF8.GetString(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
}
Código em java com BouncyCastle Resultado = l2DWkv2JTeM=
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
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;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Encoder;
public class TripleDesBouncyCastle {
private static String TRIPLE_DES_TRANSFORMATION = "DESede/ECB/PKCS7Padding";
private static String ALGORITHM = "DESede";
private static String BOUNCY_CASTLE_PROVIDER = "BC";
private static void init() {
Security.addProvider(new BouncyCastleProvider());
}
public static byte[] encode(byte[] input, byte[] key)
throws IllegalBlockSizeException, BadPaddingException,
NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException, InvalidKeyException {
init();
SecretKey keySpec = new SecretKeySpec(key, ALGORITHM);
Cipher encrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION,
BOUNCY_CASTLE_PROVIDER);
encrypter.init(Cipher.ENCRYPT_MODE, keySpec);
return encrypter.doFinal(input);
}
public static byte[] decode(byte[] input, byte[] key)
throws IllegalBlockSizeException, BadPaddingException,
NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException, InvalidKeyException {
init();
SecretKey keySpec = new SecretKeySpec(key, ALGORITHM);
Cipher decrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION,
BOUNCY_CASTLE_PROVIDER);
decrypter.init(Cipher.DECRYPT_MODE, keySpec);
return decrypter.doFinal(input);
}
public static void main(String[] args) throws IOException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchProviderException, NoSuchPaddingException {
String texto = "teste";
String chave = "12345678ABCDEFGHHGFEDCBA";
BASE64Encoder enc = new BASE64Encoder();
byte[] result = encode(texto.getBytes(), chave.getBytes());
System.out.println(enc.encode(result));
}
}
Códogo em Java ( API padrão) Resultado = kQ32PSfsgJ4=
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
public class TripleDESTest {
public static void main(String[] args) throws Exception {
String text = "teste";
byte[] codedtext = new TripleDESTest().encrypt(text);
String decodedtext = new TripleDESTest().decrypt(codedtext);
System.out.println(new BASE64Encoder().encode(codedtext)); // this is a byte array, you'll just see a reference to an array
System.out.println(decodedtext); // This correctly shows "kyle boon"
}
public byte[] encrypt(String message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("12345678ABCDEFGHHGFEDCBA".getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
final byte[] plainTextBytes = message.getBytes("utf-8");
final byte[] cipherText = cipher.doFinal(plainTextBytes);
// final String encodedCipherText = new sun.misc.BASE64Encoder()
// .encode(cipherText);
return cipherText;
}
public String decrypt(byte[] message) throws Exception {
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest("12345678ABCDEFGHHGFEDCBA".getBytes("utf-8"));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher decipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, key);
// final byte[] encData = new
// sun.misc.BASE64Decoder().decodeBuffer(message);
final byte[] plainText = decipher.doFinal(message);
return new String(plainText, "UTF-8");
}
}
Não tenho ideia onde está o erro… ainda mais porque nem os dois códigos java estão batendo.
Alguém tem alguma ideia de onde estou errando… Obrigado