I have a C # application to call a Java web service to verify a user password. I would like the C # application to encrypt the password, and then the Java web service decrypt the password. I have Java side code (decryption code), but I can not understand C # code to encrypt the code.
Here is my java code ...
public void validateUserPassword(String encryptedPassword) { String algorithm = "AES"; SecretKeySpec keySpec = null; byte[] key = "<==OMGWTFBBQ!==>".getBytes(); Cipher cipher = null; cipher = Cipher.getInstance(algorithm); keySpec = new SecretKeySpec(key, algorithm); byte[] encryptionBytes = new sun.misc.BASE64Decoder().decodeBuffer(encryptedPassword); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] recoveredBytes = cipher.doFinal(encryptionBytes); String recovered = new String(recoveredBytes); log.info("Encrypted password: " + encryptedPassword); log.info("Dencrypted password: " + recovered); }
Here is what I found for encryption using C #, but it does not create the same encrypion string as my Java function, so the Java web service cannot decrypt it.
private void btnEncrypt_Click(object sender, EventArgs e) { string PlainText = "testing"; string Password = "<==OMGWTFBBQ!==>"; string Salt = "Kosher"; string HashAlgorithm = "SHA1"; int PasswordIterations = 2; string InitialVector = "OFRna73m*aze01xY"; int KeySize = 256; string encryptedPassword; byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText); PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); RijndaelManaged SymmetricKey = new RijndaelManaged(); SymmetricKey.Mode = CipherMode.CBC; byte[] CipherTextBytes = null; using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)) { using (MemoryStream MemStream = new MemoryStream()) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)) { CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length); CryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); encryptedPassword = Convert.ToBase64String(CipherTextBytes); MessageBox.Show("Encrypted password: " + encryptedPassword); }
I don't mind changing the way my Java web service is decrypted so that it works with my C # application.
source share