输出长度与输入长度相同的加密算法 [英] Encryption algorithm with output length identical to the input length

查看:121
本文介绍了输出长度与输入长度相同的加密算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!所以我需要加密一个AOB(字节数组),但问题是从读取的加密...是输出将大于输入。加密的AOB的长度是否可能与未加密的AOB相同?



我尝试过:



我找不到解决这个问题的方法。

解决方案

是的。如果你考虑一下,要加密一个字节数组,你要做的就是翻转这些字节的大约一半的比特,使数字完全不同。你可以通过使用相同大小的精确分布的伪随机数数组对数组进行异或运算来实现这一点,这些伪随机数可以通过使用一些秘密作为种子来重现。同样的过程再次解密它们 - 这是对称加密。



所以作为一个简单的不安全的例子,考虑一下:



随机r = 随机(<你的秘密种子,哈希来自密码或类似>); 
byte [] yourData = .....
byte []翻转= new byte [yourData.Length];
r.NextBytes(翻转);

// 加密/解密
for int i = 0 ; i < yourData.Length; i ++)yourData [i] ^ =翻转[i];





Random类不够随机加密,你需要深入研究加密名称空间以获得正确的加密强度编码。



而且,你可能想插入一些冗余最后的字节使加密表示的长度不同,以避免人们只能通过长度匹配加密和未加密的数据。


Hello ! So i need to encrypt an AOB(array of bytes), but the thing is that from what read about encryption... is that the output will be bigger than the input. Is it possible the length of the encrypted AOB to be the same with the unencrypted AOB ?

What I have tried:

I couldn't find a solution to this particular problem.

解决方案

Yes it is. If you think about it, to encrypt an array of bytes what you want to do is flip roughly half the bits of those bytes to make the numbers completely different. You can do this by XORing your array with an equal size array of nicely distributed pseudo random numbers which are reproducible by using some secret as the seed. The same process decrypts them again - this is symmetric encryption.

So as a simple insecure example, consider this:

Random r = new Random(<your secret seed, hashed from a password or like>);
byte[] yourData = .....
byte[] flips = new byte[yourData.Length];
r.NextBytes(flips);

// encrypt/decrypt
for (int i = 0; i < yourData.Length; i++) yourData[i] ^= flips[i];



The Random class is not random enough for encryption, you need to delve into the crypto namespace for proper encryption strength encoding.

And, you might want to insert some redundant bytes on the end to make the encrypted representation a different length to avoid people being able to match encrypted and unencrypted data by their lengths alone.


这篇关于输出长度与输入长度相同的加密算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆