private byte[] AESDecrypt_New(string encrypted) { Cipher c = Cipher.GetInstance(_AESMode); IKey key = ((KeyStore.SecretKeyEntry)_keyStore.GetEntry(_secureStoredKeyAlias, null)).SecretKey; GCMParameterSpec param = new GCMParameterSpec(128, _fixedIV); c.Init(CipherMode.DecryptMode, key, param); return(c.DoFinal(Base64.Decode(encrypted, Base64Flags.Default))); }
private byte[] AesDecrypt(byte[] iv, byte[] encData) { using (var entry = _keyStore.GetKey(KeyAlias, null)) using (var cipher = Cipher.GetInstance(AesMode)) { var spec = new GCMParameterSpec(128, iv); cipher.Init(CipherMode.DecryptMode, entry, spec); var decBytes = cipher.DoFinal(encData); return(decBytes); } }
private string AESEncrypt_New(byte[] decrypted) { Cipher c = Cipher.GetInstance(_AESMode); KeyStore.IEntry storeEntry = _keyStore.GetEntry(_secureStoredKeyAlias, null); IKey key = ((KeyStore.SecretKeyEntry)storeEntry).SecretKey; GCMParameterSpec param = new GCMParameterSpec(128, _fixedIV); c.Init(CipherMode.EncryptMode, key, param); byte[] encodedBytes = c.DoFinal(decrypted); return(Base64.EncodeToString(encodedBytes, Base64Flags.Default)); }
/// <summary> /// AES-GCM encryption. /// </summary> /// <param name="plainData">the data to be encrypted.</param> /// <param name="secretKeyStr"> encryption secret key.</param> /// <param name="iv">encryption random iv.</param> /// <returns>the encrypted string.</returns> public static string EncryptByGcm(string plainData, string secretKeyStr, sbyte[] iv) { try { sbyte[] secretKeyByte = (sbyte[])(Array)Encoding.UTF8.GetBytes(secretKeyStr); sbyte[] plainByte = (sbyte[])(Array)Encoding.UTF8.GetBytes(plainData); SecretKeySpec secretKey = new SecretKeySpec((byte[])(Array)secretKeyByte, "AES"); Cipher cipher = Cipher.GetInstance("AES/GCM/NoPadding"); IAlgorithmParameterSpec spec = new GCMParameterSpec(128, (byte[])(Array)iv); cipher.Init((CipherMode)1, secretKey, spec); byte[] fBytes = cipher.DoFinal((byte[])(Array)plainByte); sbyte[] fSBytes = (sbyte[])(Array)fBytes; return(new string(HwHex.EncodeHexString(fSBytes))); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
public string Decrypt(byte[] data) { if (data.Length < initializationVectorLen) { return(null); } var key = GetKey(); var cipher = Cipher.GetInstance(cipherTransformationSymmetric); // IV will be the first 16 bytes of the encrypted data var iv = new byte[initializationVectorLen]; Buffer.BlockCopy(data, 0, iv, 0, initializationVectorLen); var gcm = new GCMParameterSpec(128, iv); cipher.Init(CipherMode.DecryptMode, key, gcm); // Decrypt starting after the first 16 bytes from the IV var decryptedData = cipher.DoFinal(data, initializationVectorLen, data.Length - initializationVectorLen); return(Encoding.UTF8.GetString(decryptedData)); }