将字符串加密到固定长度 [英] Encrypt string to fixed length

查看:549
本文介绍了将字符串加密到固定长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加密字符串到base64,字符串输入可以是不同的长度(限制是16个字符),我想得到固定长度的加密字符串。


$ b $有没有人知道在.NET框架中这样做的方式?还是有算法呢?

解决方案

如果您正在寻找单向加密(无需解密),可以使用SHA1

  string secretKey =MySecretKey; 
string salt =123;
System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
byte [] preHash = System.Text.Encoding.UTF32.GetBytes(secretKey + salt);
byte [] hash = sha.ComputeHash(preHash);
string password = System.Convert.ToBase64String(hash);

如果要加密和解密,可以使用RSA。


I want to encrypt string to base64, the string input can be in differential length(the limit is 16 charcters), and I want to get the encrypted string in fixed length.

Does anyone know about the way to do that in .NET framework? or has an algorithm to do that?

解决方案

If you're looking for 1-way encryption (no need to decrypt), you can use SHA1

        string secretKey = "MySecretKey";
        string salt = "123";
        System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
        byte[] preHash = System.Text.Encoding.UTF32.GetBytes(secretKey + salt);
        byte[] hash = sha.ComputeHash(preHash);
        string password = System.Convert.ToBase64String(hash);

If you want to encrypt and decrypt, you could probably use RSA.

这篇关于将字符串加密到固定长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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