AES Decryption in c# compared to Java

Viewed 36

public static String decrypt(String cyphertext) throws Exception { String plaintext; String KeyGen = "UTF8"; String KeyPhrase = "1234@4@$#$"; String KeySalt = "es3456r76f76f"; integer Keyiteration = 65536; integer keysize = 256;

    try {

        IvParameterSpec ivspec = new IvParameterSpec(KeyPhrase.getBytes(KeyGen));

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        
        String keyGenPassword = null;
        if (ServiceConfig.getServiceConfig().getKeygen() != null) {
            keyGenPassword = ServiceConfig.getServiceConfig().getKeygen();  
        }
        KeySpec spec = new PBEKeySpec(keyGenPassword.toCharArray(), KeySalt.getBytes(KeyGen), Keyiteration, keysize);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
        plaintext = new String(cipher.doFinal(Base64.decodeBase64(cyphertext)),KeyGen);

    } catch (Exception e) {
        LOGGER.error("Error while decrypting key" +  e.toString());
        
    }

    return plaintext;
}

Above code is in Java, What would be the equivalent method in c# with same result?

0 Answers
Related