/// <summary>復号化</summary> /// <param name="input">AeadResult</param> /// <returns>平文(plaintext)</returns> public override byte[] Decrypt(AeadResult input) { // 初期化 this.InitAesCbc(false); //認証タグ(MAC)を取得 // Concatenate the [AAD], the [Initialization Vector], the [ciphertext], and the [AL value]. byte[] temp = ArrayOperator.CombineArray(this.AAD, this.IV); temp = ArrayOperator.CombineArray(temp, input.Ciphert); temp = ArrayOperator.CombineArray(temp, this.AL); byte[] tag = this._hmac.ComputeHash(temp); Array.Resize(ref tag, TAG_LEN); // タグの確認 if (!tag.SequenceEqual(input.Tag)) { return(null); } // CCM操作の実行 byte[] plaint = new byte[this._aesCBC.GetOutputSize(input.Ciphert.Length)]; int len = this._aesCBC.ProcessBytes(input.Ciphert, 0, input.Ciphert.Length, plaint, 0); len += this._aesCBC.DoFinal(plaint, len); Array.Resize(ref plaint, len); // 平文を返す。 return(plaint); }
/// <summary>暗号化</summary> /// <param name="plaint">平文(plaintext)</param> /// <returns>AEAD実行結果オブジェクト</returns> public override void Encrypt(byte[] plaint) { // 初期化 this.InitAesCbc(true); // CCM操作の実行 byte[] ciphert = new byte[this._aesCBC.GetOutputSize(plaint.Length)]; int len = this._aesCBC.ProcessBytes(plaint, 0, plaint.Length, ciphert, 0); len += this._aesCBC.DoFinal(ciphert, len); // 認証タグ(MAC)を取得 // Concatenate the [AAD], the [IV], the [ciphertext], and the [AL value]. byte[] temp = ArrayOperator.CombineArray(this.AAD, this.IV); temp = ArrayOperator.CombineArray(temp, ciphert); temp = ArrayOperator.CombineArray(temp, this.AL); byte[] tag = this._hmac.ComputeHash(temp); Array.Resize(ref tag, TAG_LEN); // 結果を返す this._result = new AeadResult() { Aead = ArrayOperator.CombineArray(ciphert, tag), Ciphert = ciphert, Tag = tag, }; }
/// <summary>暗号文と認証タグ(MAC)を結合</summary> /// <returns>ciphert + tag</returns> public byte[] CombineByteArrayForDecrypt() { return(ArrayOperator.CombineArray <byte>(this.Ciphert, this.Tag)); }