gpt4 book ai didi

ssh - hell AesCtrCryptoTransform

转载 作者:行者123 更新时间:2023-12-02 14:19:04 27 4
gpt4 key购买 nike

谁能提供 SecurityDriven.Inferno AesCtrCryptoTransfom 的使用示例类(class) ?

该库似乎很有前途,但我无法找到任何使用它的代码,并且提供的详细信息对我来说还不够加密虚拟......

我正在扩展一个与许多密码完美配合的应用程序,只是它不支持 AES 计数器模式。对于一些 SSHv2 session ,我需要这个 aes-128-ctr 密码。
该应用程序为所有不同的密码提供了一个非常简单的密码接口(interface),如下所示:

  internal interface Cipher
{
void Encrypt(byte[] data, int offset, int len, byte[] result, int result_offset);
void Decrypt(byte[] data, int offset, int len, byte[] result, int result_offset);
int BlockSize { get; }
}

我使用 AesCtrCryptoTransform 创建了接口(interface)的实现,如下所示,但它不起作用。
public class AES128CTR : Cipher
{
private System.Security.Cryptography.ICryptoTransform Encryptor;
private ArraySegment<Byte> _counter;

public AES128CTR(byte[] key, byte[] iv)
{
_counter = new ArraySegment<byte>(iv);
Encryptor = new SecurityDriven.Inferno.Cipher.AesCtrCryptoTransform(key, _counter);
}

public int BlockSize { get { return Encryptor.InputBlockSize; } }

public void Decrypt(byte[] data, int offset, int len, byte[] result, int result_offset)
{
Encryptor.TransformBlock(data, 0, len, result, 0);
}

public void Encrypt(byte[] data, int offset, int len, byte[] result, int result_offset)
{
Encryptor.TransformBlock(data, 0, len, result, 0);
}
}

这应该很容易,我一定犯了明显的错误,只是无法弄清楚。任何想法请如何做到这一点?

最佳答案

这是工作解决方案:

  public class AES128CTR : Cipher
{
private byte[] _key;
private byte[] _iv;

public AES128CTR(byte[] key, byte[] iv, int KeySize = 128)
{
this._key = key;
this._iv = iv;
}

public int BlockSize { get { return _iv.Length; } }

public void Decrypt(byte[] data, int offset, int len, byte[] result, int result_offset)
{
SecurityDriven.Inferno.Cipher.AesCtrCryptoTransform transformation = new SecurityDriven.Inferno.Cipher.AesCtrCryptoTransform(_key, new ArraySegment<byte>(_iv));
using (MemoryStream ms = new MemoryStream())
{
using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, transformation, System.Security.Cryptography.CryptoStreamMode.Write))
{
cs.Write(data, offset, len);
}
var r = ms.ToArray();
Array.Copy(r, result, r.Length);
}
}

public void Encrypt(byte[] data, int offset, int len, byte[] result, int result_offset)
{
SecurityDriven.Inferno.Cipher.AesCtrCryptoTransform transformation = new SecurityDriven.Inferno.Cipher.AesCtrCryptoTransform(_key, new ArraySegment<byte>(_iv));
using (MemoryStream ms = new MemoryStream())
{
using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, transformation, System.Security.Cryptography.CryptoStreamMode.Write))
{
cs.Write(data, offset, len);
}
var r = ms.ToArray();
Array.Copy(r, result, r.Length);
}
}

}

关于ssh - hell AesCtrCryptoTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46202685/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com