/// <exception cref="System.Exception"></exception> public override void Init(int mode, byte[] key, byte[] iv) { string pad = "NoPadding"; //if(padding) pad="PKCS5Padding"; byte[] tmp; if (iv.Length > ivsize) { tmp = new byte[ivsize]; System.Array.Copy(iv, 0, tmp, 0, tmp.Length); iv = tmp; } if (key.Length > bsize) { tmp = new byte[bsize]; System.Array.Copy(key, 0, tmp, 0, tmp.Length); key = tmp; } try { cipher = Sharpen.Cipher.GetInstance("DESede/CTR/" + pad); DESedeKeySpec keyspec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.GetInstance("DESede"); SecretKey _key = keyfactory.GenerateSecret(keyspec); cipher.Init((mode == ENCRYPT_MODE ? Sharpen.Cipher.ENCRYPT_MODE : Sharpen.Cipher. DECRYPT_MODE), _key, new IvParameterSpec(iv)); } catch (Exception e) { cipher = null; throw; } }
public void Add(string key, string value) { PBEKeySpec keyspec = new PBEKeySpec(value.ToCharArray()); SecretKeyFactory fk = SecretKeyFactory.GetInstance("PBEWithMD5andDES"); ISecretKey mysec = fk.GenerateSecret(keyspec); KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(mysec); keyStore.SetEntry(key, entry, Password); Save(); }
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 a new instance of the <see cref="AesObfuscator"/> class. /// The aes obfuscator. /// </summary> /// <param name="salt"> /// an array of random bytes to use for each (un)obfuscation /// </param> /// <param name="applicationId"> /// application identifier, e.g. the package name /// </param> /// <param name="deviceId"> /// device identifier. Use as many sources as possible to /// create this unique identifier. /// </param> public AesObfuscator(byte[] salt, string applicationId, string deviceId) { try { SecretKeyFactory factory = SecretKeyFactory.GetInstance(KeygenAlgorithm); IKeySpec keySpec = new PBEKeySpec((applicationId + deviceId).ToCharArray(), salt, 1024, 256); ISecretKey tmp = factory.GenerateSecret(keySpec); ISecretKey secret = new SecretKeySpec(tmp.GetEncoded(), "AES"); this.encryptor = Cipher.GetInstance(CipherAlgorithm); this.encryptor.Init(CipherMode.EncryptMode, secret, new IvParameterSpec(Iv)); this.decryptor = Cipher.GetInstance(CipherAlgorithm); this.decryptor.Init(CipherMode.DecryptMode, secret, new IvParameterSpec(Iv)); } catch (GeneralSecurityException e) { // This can't happen on a compatible Android device. throw new RuntimeException("Invalid environment", e); } }