Пример #1
0
        protected internal static Cipher InitCipherForBlock(Cipher cipher, int block,
                                                            IEncryptionInfoBuilder builder, ISecretKey skey, int encryptMode)
        {
            EncryptionVerifier ver      = builder.GetVerifier();
            HashAlgorithm      hashAlgo = ver.HashAlgorithm;

            byte[] blockKey = new byte[4];
            LittleEndian.PutUInt(blockKey, 0, block);
            MessageDigest hashAlg = CryptoFunctions.GetMessageDigest(hashAlgo);

            hashAlg.Update(skey.GetEncoded());
            byte[]           encKey = hashAlg.Digest(blockKey);
            EncryptionHeader header = builder.GetHeader();
            int keyBits             = header.KeySize;

            encKey = CryptoFunctions.GetBlock0(encKey, keyBits / 8);
            if (keyBits == 40)
            {
                encKey = CryptoFunctions.GetBlock0(encKey, 16);
            }
            ISecretKey key = new SecretKeySpec(encKey, skey.GetAlgorithm());

            if (cipher == null)
            {
                cipher = CryptoFunctions.GetCipher(key, header.CipherAlgorithm, null, null, encryptMode);
            }
            else
            {
                cipher.Init(encryptMode, key);
            }
            return(cipher);
        }
Пример #2
0
        private async Task <byte[]> GetEncryptionKeyLockedAsync(string keyName)
        {
            byte[] key = null;

            try
            {
                char[]   deviceId = GetDeviceId().ToCharArray();
                KeyStore keyStore = await GetOrCreateKeyStoreAsync(deviceId).ConfigureAwait(false);

                KeyStore.IProtectionParameter protectionParameter = new KeyStore.PasswordProtection(deviceId);
                KeyStore.SecretKeyEntry       secretKeyEntry      = (KeyStore.SecretKeyEntry)keyStore.GetEntry(keyName, protectionParameter);

                if (secretKeyEntry != null)
                {
                    ISecretKey secretKey = secretKeyEntry.SecretKey;
                    if (secretKey != null)
                    {
                        key = secretKey.GetEncoded();
                    }
                }
            }
            catch (FileNotFoundException)
            {
                // If the file isn't found, it's not a big deal and should mean it's just the first run.
                // The caller or the GetOrCreate method above will need to create it if we don't find it here.
            }

            return(key);
        }
Пример #3
0
        protected internal static Cipher InitCipherForBlock(Cipher existing, int block, bool lastChunk,
                                                            IEncryptionInfoBuilder builder, ISecretKey skey, int encryptionMode)
        {
            EncryptionHeader header = builder.GetHeader();

            if (existing == null || lastChunk)
            {
                String pAdding = (lastChunk ? "PKCS5PAdding" : "NoPAdding");
                existing = CryptoFunctions.GetCipher(skey, header.CipherAlgorithm, header.ChainingMode, header.KeySalt, encryptionMode, pAdding);
            }

            byte[] blockKey = new byte[4];
            LittleEndian.PutInt(blockKey, 0, block);
            byte[] iv = CryptoFunctions.GenerateIv(header.HashAlgorithm, header.KeySalt, blockKey, header.BlockSize);

            AlgorithmParameterSpec aps;

            if (header.CipherAlgorithm == CipherAlgorithm.rc2)
            {
                aps = new RC2ParameterSpec(skey.GetEncoded().Length * 8, iv);
            }
            else
            {
                aps = new IvParameterSpec(iv);
            }

            existing.Init(encryptionMode, skey, aps);
            return(existing);
        }
Пример #4
0
        private async Task <byte[]> CreateAndStoreEncryptionKeyLockedAsync(string keyName)
        {
            byte[] keyBytes = null;

            KeyGenerator keyGenerator = KeyGenerator.GetInstance("AES");

            keyGenerator.Init(256, new SecureRandom());
            ISecretKey key = keyGenerator.GenerateKey();

            await StoreKeyAsync(keyName, key).ConfigureAwait(false);

            keyBytes = key.GetEncoded();

            return(keyBytes);
        }
Пример #5
0
 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);
     }
 }
Пример #6
0
 /// <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);
     }
 }
Пример #7
0
        protected internal static Cipher InitCipherForBlock(Cipher cipher, int block,
                                                            IEncryptionInfoBuilder builder, ISecretKey skey, int encryptMode)
        {
            EncryptionVerifier ver      = builder.GetVerifier();
            HashAlgorithm      hashAlgo = ver.HashAlgorithm;

            byte[] blockKey = new byte[4];
            LittleEndian.PutUInt(blockKey, 0, block);
            byte[]     encKey = CryptoFunctions.GenerateKey(skey.GetEncoded(), hashAlgo, blockKey, 16);
            ISecretKey key    = new SecretKeySpec(encKey, skey.GetAlgorithm());

            if (cipher == null)
            {
                EncryptionHeader em = builder.GetHeader();
                cipher = CryptoFunctions.GetCipher(key, em.CipherAlgorithm, null, null, encryptMode);
            }
            else
            {
                cipher.Init(encryptMode, key);
            }
            return(cipher);
        }
Пример #8
0
        private static byte[] Mac_3des(ISecretKey key, byte[] text, int offset, int length, byte[] iv)
        {
            if (length == -1)
            {
                length = text.Length - offset;
            }

            try
            {
                //Cipher cipher = Cipher.getInstance(DES3_CBC_CIPHER);
                //cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
                //byte[] res = cipher.doFinal(text, offset, length);
                byte[] res = GPCrypto.DoEncrypt_DES3_CBC(key.GetEncoded(), text, offset, length, iv);

                byte[] result = new byte[8];
                Array.Copy(res, res.Length - 8, result, 0, 8);
                return(result);
            }
            catch (Exception e)
            {
                throw new Exception("MAC computation failed.", e);
            }
        }