简体   繁体   English

将字符串加密为15个字符

[英]Encrypting string to 15 characters

I am doing this serial key thing where the format is xxxxx-xxxxx-xxxxx (15 characters). 我正在执行此串行密钥操作,其格式为xxxxx-xxxxx-xxxxx(15个字符)。 I can currently encrypt my string but the problems are: 我目前可以加密我的字符串,但是问题是:

  1. 6 Characters become 20+ Characters. 6个字符变为20个以上字符。
  2. Has some underieable characters such as: +, /, = 具有一些可理解的字符,例如:+,/,=

So what would be the best way to encrypt a string to certain number of characters? 那么将字符串加密为一定数量的字符的最佳方法是什么? In my case: 6 characters encrypted to 15 characters. 就我而言:将6个字符加密为15个字符。

I am currently using something like this: 我目前正在使用这样的东西:

    public string EncryptString(string clearText, string Password)
    {
        byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(Password, ByteGetter(UsageMode, "Password")); 
        byte[] encryptedData = EncryptByte(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        return Convert.ToBase64String(encryptedData);
    }


    public static byte[] EncryptByte(byte[] clearData, byte[] Key, byte[] IV)
    {
        MemoryStream ms = new MemoryStream();
        Rijndael alg = Rijndael.Create();
        alg.Key = Key;
        alg.IV = IV;
        CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(clearData, 0, clearData.Length);
        cs.Close();
        byte[] encryptedData = ms.ToArray();
        return encryptedData;
    }

First of all: It is a property of Base64-encoding to contain non-alpha characters. 首先:包含非字母字符是Base64编码的属性。 So you will need to chose another encoding, you might want to start use 5 bit parcels and encode to AZ, 2-9 but leave out O,I. 因此,您将需要选择另一种编码,您可能要开始使用5位数据包并将其编码为AZ,即2-9,但忽略O,I。 This should give you the character set you wish. 这应该给您想要的字符集。

Second: Assuming you do chose 5 bit packets, you need 5*15=75 bits. 第二:假设您确实选择了5位数据包,则需要5 * 15 = 75位。 Most hashes will create 128 bits ore more, so you need to downsample. 大多数散列会产生128位或更多的矿石,因此您需要下采样。 A possible way to approach this is to simply ignore bits. 解决此问题的一种可能方法是简单地忽略位。

Synthetising the comment thread from the question: 从问题综合注释线程:

If you want the serial code to be (reversibly) encrypted data, you can't really constrain its size very well. 如果您希望串行代码是(可逆)加密的数据,则不能真正很好地限制其大小。 You're stuck with whatever the encryption algorithm gives you, and they usually operate on fixed-size blocks. 加密算法为您提供的任何功能都使您无所适从,它们通常在固定大小的块上运行。

The obvious choices for encoding the encrypted data into a bunch of ASCII characters are: 将加密数据编码为一堆ASCII字符的显而易见的选择是:

  • base64 - widely supported, but can't be implemented using only alphanumerics, and is case-sensitive base64-广泛支持,但不能仅使用字母数字实现,并且区分大小写
  • base32 - somewhat obscure - you'll have to write / find an implementation. base32-有点晦涩-您必须编写/查找实现。 The RFC-specified standard requires padding with = s. RFC指定的标准要求使用= s进行填充。 (Just as it does for base64 .) More compact than base16 , and only needs case-insensitive alphanumerics for the data, which is an advantage if you expect people to type the code by hand. (就像对base64所做的一样。)比base16更紧凑,并且仅需要区分大小写的字母数字作为数据,如果您希望人们手动键入代码,则这是一个优点。
  • base16 - also very well supported, never requires padding, but is far less compact than the other two. base16-也得到很好的支持,不需要填充,但是比其他两个紧凑得多。

You can work around the padding issue knowing that the length of the encoded string has to be a multiple of 8 for base32 , and a multiple of 4 for base64. 您可以解决padding问题,知道base32的编码字符串的长度必须是8的倍数 ,而base64必须是4的倍数。 (I'm not 100% sure about this, but it makes sense, since you want the encoded string to represent a multiple of 8 bits.) Knowing this you can add padding characters to make a decoder stop complaining about an invalid input length. (对此我不是100%肯定,但是这很有意义,因为您希望编码的字符串表示8位的倍数。)知道了这一点,您可以添加填充字符,以使解码器停止抱怨无效的输入长度。 ( Convert.FromBase64String() does complain.) Convert.FromBase64String()确实报错。)

If you want to constrain the output size given an input size in some specific way, and aren't willing to discard data (ie hash instead of encrypting), you'll have to roll your own encryption algorithm. 如果您想以某种特定的方式来限制给定输入大小的输出大小,并且不想舍弃数据(即哈希而不是加密),则必须使用自己的加密算法。 This seems mostly counterproductive. 这似乎适得其反。

With your requirements you need to: 根据您的要求,您需要:

  • Encrypt with DES 用DES加密
  • Encode with Base 32 (base 16 will give you 16 bytes instead of 15) 使用Base 32编码(base 16将为您提供16个字节,而不是15个字节)
  • Pad however you like 随便垫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM