Aniket Narvankar

Aniket Narvankar

  • 833
  • 1.9k
  • 419.9k

AES Decryption in Java and C Sharp

May 1 2020 9:47 PM
Please help on this,which algorithm should I use for decryption in C sharp,below is the decryption code in java and I want to convert in C Sharp,how to do this,please do guide me on this
 

public String decrypt(String ciphertext) {
try {
if(CommonUtils.isEmpty(ciphertext)) {
return null;
}
byte[] saltDecrypt = spiSalt.getBytes();
SecretKeyFactory factoryKeyDecrypt = SecretKeyFactory.getInstance("xyz");
SecretKey tmp2 = factoryKeyDecrypt.generateSecret(new PBEKeySpec(spiKey.toCharArray(), saltDecrypt, iterations, keyLength));
SecretKeySpec decryptKey = new SecretKeySpec(tmp2.getEncoded(), "AES");
Cipher aesCipherDecrypt = Cipher.getInstance("AES/ECB/PKCS5Padding");
aesCipherDecrypt.init(Cipher.DECRYPT_MODE, decryptKey);
byte[] e64bytes = StringUtils.getBytesUtf8(ciphertext);
byte[] eBytes = Base64.decodeBase64(e64bytes);
byte[] cipherDecode = aesCipherDecrypt.doFinal(eBytes);
String decoded = StringUtils.newStringUtf8(cipherDecode);
return decoded;
}catch (Exception e){
e.printStackTrace();
return null;
}
}

java libraries

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;