使用 BouncyCastle 实现 TEA 算法
我试图将使用 BouncyCastle 的 RSA 算法的 Håvard Stranden 示例 (http://ox.no/posts/rsa-using-bouncycastle) 改编成 TEA 算法,同时还使用 BouncyCastle 附带的示例,我做了以下内容加密我的字符串的代码:
byte[] data = Encoding.UTF8.GetBytes("This is an encoding test!!!...");
TeaEngine e = new TeaEngine();
e.Init(true, new KeyParameter(Encoding.UTF8.GetBytes("MyPassword")));
int blockSize = e.GetBlockSize();
byte[] outBytes = new byte[data.Length];
List<byte> output = new List<byte>();
for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize));
if (e.IsPartialBlockOkay)
{
e.ProcessBlock(data, chunkPosition, outBytes, chunkPosition);
}
else
{
e.ProcessBlock(data, chunkPosition, outBytes, chunkPosition);
}
}
Console.WriteLine("Encrypted: " + Encoding.UTF8.GetString(output.ToArray()));
但我总是收到错误。关于如何实施它有什么想法吗?
提前致谢
编辑:很抱歉没有发布错误日志,但我在晚些时候发布了此内容并忘记了它
是这样的: 在 e.Init(true, new KeyParameter(Encoding.UTF8.GetBytes("MyPassword")));我收到“索引超出数组范围”的消息。
I was trying to adapt Håvard Stranden example (http://ox.no/posts/rsa-using-bouncycastle) of a RSA algorithm using BouncyCastle into a TEA algorithm, using also the examples that come with BouncyCastle, and I made the following code to encrypt my string:
byte[] data = Encoding.UTF8.GetBytes("This is an encoding test!!!...");
TeaEngine e = new TeaEngine();
e.Init(true, new KeyParameter(Encoding.UTF8.GetBytes("MyPassword")));
int blockSize = e.GetBlockSize();
byte[] outBytes = new byte[data.Length];
List<byte> output = new List<byte>();
for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize));
if (e.IsPartialBlockOkay)
{
e.ProcessBlock(data, chunkPosition, outBytes, chunkPosition);
}
else
{
e.ProcessBlock(data, chunkPosition, outBytes, chunkPosition);
}
}
Console.WriteLine("Encrypted: " + Encoding.UTF8.GetString(output.ToArray()));
But I always get an error. Any idea on how to implement it?
Thanks in advance
EDIT: Sorry about not posting the error log, but I posted this at a later hour and forgot about it
Here it goes:
At e.Init(true, new KeyParameter(Encoding.UTF8.GetBytes("MyPassword"))); I get an "Index was outside the bounds of the array."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TEA 使用 128 位密钥,即 16 个字节。使用 16 字节长的密码(或使用密钥派生函数从较短的密码获取 16 字节密钥):
TEA uses a 128-bit key, which is 16 bytes. Use a password that is 16 bytes long (or use a key-derivation function to get a 16-byte key from a shorter password):