I'm hitting a brick wall, I hope someone can help. I have to encrypt a string that will be decrypted somewhere else (not under my control), as part of an authentication mechanism.
I'm told it uses a cipher "AES/ECB/PKCS5Padding", and I was given a 128 bit encryption key. After implementing this in C#, it doesn't match the expected output.
While poking around a .jar I got my hands on, it seems that the encryption key is used in a different way than I was expecting, and I haven't figured out how, or even if it can, be ported to C#. Java:
String secretKey = "1234567890abcdef" //example
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
sRandom.setSeed(secretKey.getBytes("UTF-8"));
keyGen.init(128, sRandom);
SecretKey generatedKey = keyGen.generateKey();
byte[] encodedKey = generatedKey.getEncoded();
SecretKeySpec realKey = new SecretKeySpec(encodedKey, "AES");
Cipher cipherInstance = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
cipherInstance.init(1, realKey);
String encryptedText bytesToHexString(cipherInstance.doFinal("text to encode".getBytes("UTF-8")));
I think I've got it all down except generating the encodedKey
above.
Can someone shed some light please? :)
EDIT: The example above should produce 4a031f9da16717d442c0d06c415435b5
I believe.