Java code
public String encrypt(String key, String value) {
try {
String initVector = "RgUkXp2s5v8y/B?E";
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(hashKey(key), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(1, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private byte[] hashKey(String keyValue) throws NoSuchAlgorithmException, UnsupportedEncodingException {
byte[] key = keyValue.getBytes("UTF-8");
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
key = messageDigest.digest(key);
return Arrays.copyOf(key, 16);
}
public static String decrypt(byte[] key, String encrypted)
{
try
{
String initVector = "RgUkXp2s5v8y/B?E";
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(2, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));
return new String(original);
}
catch (Exception ex)
{
Logger.getLogger("Decryption ").log(Level.SEVERE, "Exception processing decryption " + ex.getMessage());
return null;
}
}
String encryptedLicense = ENCRYPT.encrypt(string_key, stringToEncrypt);
converted c# code
public static byte[] getDefaultKey()
{
return DecryptUtil.hashKey(DecryptUtil.getUniqueIdentifier());
}
private static byte[] hashKey(string keyValue)
{
try
{
var sha1 = SHA1Managed.Create();
byte[] inputBytes = Encoding.UTF8.GetBytes(keyValue);
byte[] outputBytes = sha1.ComputeHash(inputBytes);
return outputBytes;
}
catch (Exception ex)
{
ErrorLogger.WriteToFile("hashKey " + ex.Message.ToString());
}
return null;
}
public static string Decrypt_License(string encryptedText, byte[] key)
{
string initVector = "RgUkXp2s5v8y/B?E";
var ivBytes = Encoding.UTF8.GetBytes(initVector);
var keyBytes = new byte[16];
Array.Copy(key, keyBytes, Math.Min(keyBytes.Length, key.Length));
string plaintext = null;
// Create AesManaged
using (AesManaged aes = new AesManaged())
{
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
aes.IV = ivBytes;
aes.BlockSize = 128;
aes.KeySize = 128;
aes.Mode = CipherMode.CBC;
ICryptoTransform decryptor = aes.CreateDecryptor(keyBytes, ivBytes);
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(encryptedText)))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(cs))
plaintext = reader.ReadToEnd();
}
}
}
return plaintext;
}
String decryptedString = DecryptUtil.Decrypt_License(hashkey(string_key), stringToDecrypt);
I am converting java code to c#, encryption done in java now I am trying to decrypt the data in c# . I read PaddingMode.PKCS7 and PKCS5 both are same. Is it true? or should I change padding type?
I have used all the concepts which is used in java. but it is not working. Am I missing something?
I have doubt in Convert.FromBase64String(encryptedText), because this is the place they(java) have used value.getBytes(). getBytes() equivalent in c# is Convert.FromBase64String()? will they produce same result?
Java hashkey() method and C# hashkey method giving different results. How to compare them as byte[]?
If I change Convert.FromBase64String() to Encoding.UTF8.GetBytes() then it is throwing 'Length of the data to decrypt is invalid AES C#' error.
This is my encrypted text wJZESOEPNb66FbnpB8DqmwLcyosxGOjzgPKa9+7/VUatEFxSs1YqS28HHU26EmCcZDo7otE5PvX/qIpgQgTrhlCHgUJBb0+qd522/1egYoisE48ZL8X3cwCNx1AOVYJ0T2VOLB+J3HDWK4HOJfgm2CqbQpep0ioOTgNKQA0SF3SRmF92MGbf7wjUBA8WGdeUiIKRX9Lm/x6mUUuXHEIrCHbZOqDZvo2xR9zndIzwT/FfgLsQbJQjRjZPg7urfWLpUt6drbYcPFm3KgTmSrsRhzk15P6mw3GNO+3BmqBIndKmzi8blQqG8YFXVW8wiaurCm0jVcX5kwdU1PO1ktHSXQ==
secret key used for encryption BFEBFBFF000306D4
PKCS5Paddingis used by many libraries (e.g. Java) as an identifier for PKCS#7 padding, see here. Why did you comment out the lines inhashKeyof the C# code regarding SHA-1? I suspect that this is the (or at least one) bug. In the Java code SHA-1 is used! - Topaco