Cryptage de données en java
Bonjour à tous,
j'ai un code c# qui est le suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.IO;
usingSystem.Security.Cryptography;
///<summary>
///encrypt/decrypt a string (password) using the input key (must be provided)
///and the IV (initializing vector)
///the key and IV must have the same length
///</summary>
public abstract class TripleDES
{
public static string Encrypt(string data, byte[] Key, byte[] IV)
{
string result = null;
try
{
MemoryStream output = new MemoryStream();
byte[] byteData = new UnicodeEncoding().GetBytes(data);
//Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the
//same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees
//output of two identical plaintext blocks are different.
//byte[] keyy = new byte[100];
//byte[] ivv = new byte[100];
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
CryptoStream crypt = new CryptoStream(output,
new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);
//Assign our crypto-generated key and iv values to our output arguments
//desKey = des.Key; desIV = des.IV;
crypt.Write(byteData, 0, byteData.Length);
crypt.FlushFinalBlock();
crypt.Close();
output.Close();
result = new UnicodeEncoding().GetString(output.ToArray());
}
catch (Exception ex)
{
throw ex;
}
return result;
}
====== Utilisation de ce code... Pour crypter
stringkey = ConfigurationManager.AppSettings["Key"];
string iv = ConfigurationManager.AppSettings["IV"];
string text = TextBox1.Text;
string cryp = TripleDES.Encrypt(text,
new ASCIIEncoding().GetBytes(key),
new ASCIIEncoding().GetBytes(iv));
string Text = HttpUtility.UrlEncode( cryp); |
Ce code permet une authentification sur un site.
Je dois encoder de la même manière mais en java.
Voici mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
byte[] input = new String("pmc").getBytes();
byte[] encryptKey = "****************".getBytes();
byte[] ivBytes = "********".getBytes();
// Créer la clé
SecretKey key = new SecretKeySpec(encryptKey, "DESede");
// Initial Vector
IvParameterSpec ivParameters = new IvParameterSpec(ivBytes);
// Créer un DESede Cipher
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
// Initialise le cipher et le met en mode "encrypt"
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameters);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("Login crypté et encodé en UTF-8: "+ URLEncoder.encode(new String(cipherText),"UTF-8")); |
Or je n'obtiens pas le même résultat:
Avec le code c# :
String d'entrée : pcm
String de sortie : %ea%b4%9e%ee%9e%a6%e4%b7%9f%ed%8f%8c
Avec le code java
String d'entrée : pcm
String de sortie : %1E%C3%A1%C3%A2%C3%89%1A%C3%B6%14%C3%A4
Savez vous d'où cela peut venir ?
Je désespère.
Merci bcp