public AESObfuscator(byte[] salt, string password) { try { SecretKeyFactory factory = SecretKeyFactory.GetInstance(KEYGEN_ALGORITHM); PBEKeySpec keySpec = new PBEKeySpec(password.ToCharArray(), salt, 1024, 256); ISecretKey tmp = factory.GenerateSecret(keySpec); ISecretKey secret = new SecretKeySpec(tmp.GetEncoded(), "AES"); mEncryptor = Cipher.GetInstance(CIPHER_ALGORITHM); mEncryptor.Init(Cipher.EncryptMode, secret, new IvParameterSpec(IV)); mDecryptor = Cipher.GetInstance(CIPHER_ALGORITHM); mDecryptor.Init(Cipher.DecryptMode, secret, new IvParameterSpec(IV)); } catch (GeneralSecurityException e) { // This can't happen on a compatible Android device. throw new RuntimeException("Invalid environment", e); } }
/// <summary> /// Initializes the cipher if it has not yet been initialized. /// </summary> /// <param name="mode">The mode.</param> /// <param name="iv">The iv.</param> /// <param name="cipher">The cipher.</param> /// <exception cref="System.ArgumentException"> /// Invalid algorithm parameter. /// </exception> /// <exception cref="System.NotSupportedException">Algorithm not supported.</exception> private void InitializeCipher(CipherMode mode, byte[] iv, ref Cipher cipher) { try { bool newCipher = false; if (cipher == null) { cipher = Cipher.GetInstance(this.GetCipherAcquisitionName().ToString()); newCipher = true; } if (this.algorithm.IsBlockCipher() || newCipher) { iv = this.ThisOrDefaultIV(iv); using (var ivspec = iv != null ? new IvParameterSpec(iv) : null) { cipher.Init(mode, this.key, ivspec); } } } catch (InvalidKeyException ex) { throw new ArgumentException(ex.Message, ex); } catch (NoSuchAlgorithmException ex) { throw new NotSupportedException("Algorithm not supported.", ex); } catch (InvalidAlgorithmParameterException ex) { throw new ArgumentException("Invalid algorithm parameter.", ex); } }