日期:2014-05-18  浏览次数:20867 次

如何控制RSA加密密文的长度?
C# code

        const string PUBLIC_KEY = "<RSAKeyValue><Modulus>vh4fRUXbE/SATMIqawzPhJWJiXnR1aDgeAZix
pRuNyyLTSm+TjlXymWLKH4VdloUWTC1vqxR28clhOX2mdvJ...........
OQhP+v55jvHxjRh8zZtt5t/DF1ZSWHuW1vZrg/9M=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

        const string PRIVATE_KEY = "<RSAKeyValue><Modulus>vh4fRUXbE/SATMIqawzPhJWJiXnR1aDgeAZ
ixpRuNyyLTSm+TjlXymWLKH4VdloUWT
C1vqxR28clhOX2mdvJ6CYcXq7nq2gyyzcrNhmYRpjWwHN4MKdDKoxGdnGgp30Jy1FOQhP+v55jvHxjRh8zZtt5t/DF1
ZSWHuW1vZrg/9M=</Modulus><Exponent>AQAB<.............=</D></RSAKeyValue>";

            rsa.FromXmlString(PRIVATE_KEY);
            System.Security.Cryptography.RSAPKCS1SignatureFormatter f=
                 new System.Security.Cryptography.RSAPKCS1SignatureFormatter(rsa);
            f.SetHashAlgorithm("SHA1");
            Byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(txtUser.Text);
            System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed();
            Byte[] result = sha.ComputeHash(source);
            Byte[] regkey = f.CreateSignature(result);
            txtCode.Text = Convert.ToBase64String(regkey);
            //加密ding结果为STEcJv8JNr77Ta62C+IqKR5zVhMwtpJd8CEKv6Ne6CTcJcI+Z1nRv+iTbYG+qmSr
             tuQJWpZFBxslRZB8grJfhT0WxEspVKJVtnzn7FRElg+Rmr1UXg8Mo7xNqLOPlTalrN3ewxnXmr6WExoJ76KW1tO2ON
              NoLRU2sBR0jGfUC/0=



如何让加密结果变短啊,这个太长不能做注册码

------解决方案--------------------
你可以只取一部分嘛。因为相同的字符串经过加密,肯定结果是一样的,所以某一部分也是一样的。

或者用md5,32个字符。
------解决方案--------------------
没必要用全部嘛,用一部分就够了