有人可以提供SecurityDriven.Inferno AesCtrCryptoTransfom类的使用示例吗?
图书馆看起来非常有前景,但是我无法找到使用它的任何代码,所提供的细节对我来说还不够加密...
我正在扩展一个与许多密码完美协作的应用程序,只是它不支持AES计数器模式。对于某些SSHv2会话,我需要这个aes-128-ctr密码。 该应用程序有一个非常简单的Cipher接口,适用于所有不同的密码,如下所示:
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创建了一个接口的实现,如下所示,但它不起作用。
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);
}
}
这应该是非常容易的,我必须犯明显的错误才能解决它们。请问有什么想法吗?
答案 0 :(得分:2)
以下是工作解决方案:
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);
}
}
}