0

I trying to implement this same code to encrypt on c# but always i get some different encrypted code :

This is my Java class :

public class AES256 {
  private static final String SECRET_KEY = "my_super_secret_key_ho_ho_ho";
  private static final String SALT = "ssshhhhhhhhhhh!!!!";
 
  public static String encrypt(String strToEncrypt) {
    try {
      byte[] iv = "1234567891234567".getBytes("UTF-8");
      IvParameterSpec ivspec = new IvParameterSpec(iv);
 
      SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
      KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALT.getBytes(), 65536, 256);
      SecretKey tmp = factory.generateSecret(spec);
      SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
 
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
      return Base64.getEncoder()
          .encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
    } catch (Exception e) {
      System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
  }
}

How can i implement the same on c#? i usig this as a model -> c# AES Encrypt but the result is diffetent even using the same secret , salt ,interaction hash count

This what i have on c# :

 var passPhrase = "my_super_secret_key_ho_ho_ho";

            // Initialization Vector (16 bit length)
            var iv = "1234567891234567";

 
            // Encrypt & Decrypt (with advanced settings)
            var opts = new AESCryptOptions()
            {
                PasswordHash = AESPasswordHash.SHA1,
                PasswordHashIterations = 65536,
                PasswordHashSalt = "ssshhhhhhhhhhh!!!!",
                PaddingMode = PaddingMode.PKCS7,
                MinSaltLength = 4,
                MaxSaltLength = 16,
                FixedKeySize=256
            };
             var encryptedText = new AESCrypt(passPhrase, iv, opts).Encrypt(text);

And the encrypt Method, i changed the hash to sha256, the only change made from the implement from gitHub:

PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                           passPhrase,
                                                           saltValueBytes,
                                                           ("SHA256"),
                                                           Options.PasswordHashIterations);

                // Convert key to a byte array adjusting the size from bits to bytes.
                keyBytes = password.GetBytes(keySize / 8);
Ivan Fontalvo
  • 433
  • 4
  • 21
  • The padding mode is different from java to c#. See : https://stackoverflow.com/questions/19698272/encrypt-in-java-and-decrypt-in-c-sharp-for-aes-256-bit – jdweng Feb 22 '21 at 14:11
  • 2
    Please post your most recent C# code. – Topaco Feb 22 '21 at 14:13
  • @Topaco Adde C# Code – Ivan Fontalvo Feb 22 '21 at 16:34
  • @jdweng in java PKCS5PADDING and what i read PCKS7 from c# are compatibles – Ivan Fontalvo Feb 22 '21 at 16:35
  • Yes if you have the same padding mode. The default mode in c# and java are different. So you have to specify the correct padding mode. – jdweng Feb 22 '21 at 16:38
  • 1
    [`PasswordDeriveBytes` uses PBKDF1](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.passwordderivebytes?view=net-5.0#remarks), and the Java code appears to be using PBKDF2. Depending on which version of .NET you're using, you might be able to get away with using [`Rfc2898DeriveBytes`](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.rfc2898derivebytes.-ctor?view=net-5.0#System_Security_Cryptography_Rfc2898DeriveBytes__ctor_System_String_System_Byte___System_Int32_System_Security_Cryptography_HashAlgorithmName_) instead. – Joshua Robinson Feb 22 '21 at 16:47
  • @JoshuaRobinson i will try with Rfc2898DeriveBytes and let you know , thanks in advanced – Ivan Fontalvo Feb 22 '21 at 16:54
  • @JoshuaRobinson the Rfc2898DeriveBytes it works!!, but i need to change in java to use SHA1 that is the default on RFC2898 ... can i change from sha1 to sha256 on rfc2898 ? – Ivan Fontalvo Feb 22 '21 at 18:11
  • 1
    @IvanFontalvo It depends on which version of .NET you're using. From .NET Core 2.0 or .NET Framework 4.7.2 and up (I think), you can specify a hash algorithm in the constructor of `Rfc2898DeriveBytes`. If you can't do that, you'll need an implementation of PBKDF2 that allows you to specify the hash... I'm not sure that there is one built in to the framework. – Joshua Robinson Feb 22 '21 at 19:12

0 Answers0