使用 BouncyCastle 实现 TEA 算法

发布于 2024-11-30 11:46:43 字数 1138 浏览 1 评论 0原文

我试图将使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似最初 2024-12-07 11:46:43

TEA 使用 128 位密钥,即 16 个字节。使用 16 字节长的密码(或使用密钥派生函数从较短的密码获取 16 字节密钥):

e.Init(true, new KeyParameter(Encoding.UTF8.GetBytes("MyPassword123456")));

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):

e.Init(true, new KeyParameter(Encoding.UTF8.GetBytes("MyPassword123456")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文