将 Int32 转换为 char(s) 表示
Convert Int32 to char(s) representation
我有一些加密(和解密)函数可以接受任何 int
值和 return 也可以接受任何 int
值(它在幕后使用一些带有大质数的异或运算):
public interface IIntEncryption {
public int Encrypt(int value);
public int Decrypt(int encryptedValue);
}
现在我想用它来加密字符串。我的想法是从字符串中取出每个字符,将其转换为 int,用上面的函数加密,然后再转换回 char。问题是并非每个 int 值都是有效的字符值 afaik。所以将 int 转换为 char 是行不通的。所以我想我需要以某种方式将 int 转换为两个有效字符,然后在解密时转换每对两个字符。所以基本上我在寻找以下功能:
public interface IStringEncryption {
public string Encrypt(string str, IIntEncryption intEncryption);
public string Decrypt(string encryptedStr, IIntEncryption intEncryption);
}
我尝试了很多方法,但我无法弄清楚如何将整数 encrypt/decrypt array if 转换为字符串表示形式。最后我希望它是 base64 编码的字符串。
I have some encrypting (and decrypting) functions that take any int value and return also any int value (it uses some xor operations with large prime numbers under the hood):
这很好,假设您只为 fun/education 这样做。为任何需要安全性的地方创建自己的加密算法或实现通常不受欢迎。
My idea is to take each char from the string, convert it to int, encrypt with the function above and then convert back to char
这不会很好,或者至少是一种非常麻烦的方法。正如您推测的那样,您可以在 32 位 int 中容纳两个 utf16 字符。但是你仍然会遇到同样的问题再次转换为字符串,因为并非所有 16 位值都是有效的 utf16 字符。
更好的解决方案是 convert your string to a byte-array using some kind of encoding. You can then convert pairs of 4 bytes to int32s, encrypt, convert back to a byte array, and use something like base64 to convert the bytes back to a string。
这听起来可能有点复杂,确实如此。所以大多数实际实现只是直接使用字节数组,通常在内部将它们拆分成一些更大的块大小。
我有一些加密(和解密)函数可以接受任何 int
值和 return 也可以接受任何 int
值(它在幕后使用一些带有大质数的异或运算):
public interface IIntEncryption {
public int Encrypt(int value);
public int Decrypt(int encryptedValue);
}
现在我想用它来加密字符串。我的想法是从字符串中取出每个字符,将其转换为 int,用上面的函数加密,然后再转换回 char。问题是并非每个 int 值都是有效的字符值 afaik。所以将 int 转换为 char 是行不通的。所以我想我需要以某种方式将 int 转换为两个有效字符,然后在解密时转换每对两个字符。所以基本上我在寻找以下功能:
public interface IStringEncryption {
public string Encrypt(string str, IIntEncryption intEncryption);
public string Decrypt(string encryptedStr, IIntEncryption intEncryption);
}
我尝试了很多方法,但我无法弄清楚如何将整数 encrypt/decrypt array if 转换为字符串表示形式。最后我希望它是 base64 编码的字符串。
I have some encrypting (and decrypting) functions that take any int value and return also any int value (it uses some xor operations with large prime numbers under the hood):
这很好,假设您只为 fun/education 这样做。为任何需要安全性的地方创建自己的加密算法或实现通常不受欢迎。
My idea is to take each char from the string, convert it to int, encrypt with the function above and then convert back to char
这不会很好,或者至少是一种非常麻烦的方法。正如您推测的那样,您可以在 32 位 int 中容纳两个 utf16 字符。但是你仍然会遇到同样的问题再次转换为字符串,因为并非所有 16 位值都是有效的 utf16 字符。
更好的解决方案是 convert your string to a byte-array using some kind of encoding. You can then convert pairs of 4 bytes to int32s, encrypt, convert back to a byte array, and use something like base64 to convert the bytes back to a string。
这听起来可能有点复杂,确实如此。所以大多数实际实现只是直接使用字节数组,通常在内部将它们拆分成一些更大的块大小。