- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在研究 AES-256-CBC 的实现(如下所示),这将用于客户端到数据库的设置。 key 存储在本地,此方案的目的是在数据库内容泄漏时使某些值不可读。
由于IV是固定长度的,所以我打算将密文设为初始化向量前缀为(IV+C)的加密消息,解密过程取密文,将IV和C分开存储,求出C 与存储的初始化向量。
在多次尝试验证此方法失败后,我注释掉了加密部分以查看会发生什么。事实证明,IV 或密文的长度是错误的,即使这个长度是固定的。起初我假设Rijndael initialization vector with 16 bytes会有所帮助,因此我将所有内容都转换为 UTF8,但它仍然无法按预期工作。更糟糕的是:密文随机丢失第一个字符(对我来说,这比一致错误更严重)。
我没有想法,我在这里错过了什么? (注意:消息框用于调试目的)
public string Encrypt(string input)
{
string cipher = null;
RijndaelManaged Crypto = new RijndaelManaged();
Random r = new Random();
Crypto.KeySize = 256;
Crypto.BlockSize = 128;
Crypto.Mode = CipherMode.CBC;
Crypto.Padding = PaddingMode.PKCS7;
byte[] message = Encoding.UTF8.GetBytes(@input);
byte[] pubkey = Encoding.UTF8.GetBytes(@public_key.PadRight(16, '#').Substring(0, 16));
byte[] iv = new byte[16];
r.NextBytes(iv);
Crypto.Key = pubkey;
Crypto.IV = iv;
try
{
ICryptoTransform Encrypt = Crypto.CreateEncryptor();
cipher = @Encoding.UTF8.GetString(iv) + @Encoding.UTF8.GetString(message);//@Encoding.UTF8.GetString(iv) + @Encoding.UTF8.GetString(Encrypt.TransformFinalBlock(message, 0, message.Length));
if (debug)
MessageBox.Show("ENC\r\ninput (" + input.Length + "): " + @input + "\r\nmessage (" + message.Length + "): " + @System.Text.Encoding.UTF8.GetString(message) + "\r\npubkey (" + pubkey.Length + "):" + @System.Text.Encoding.UTF8.GetString(pubkey) + "\r\ninitialization vector (" + iv.Length + "): " + @System.Text.Encoding.UTF8.GetString(iv) + "\r\ncipher (" + cipher.Length + "): " + @cipher);
}
catch (CryptographicException e)
{
MessageBox.Show("ENC\r\ninput (" + input.Length + "): " + @input + "\r\nmessage (" + message.Length + "): " + @System.Text.Encoding.UTF8.GetString(message) + "\r\npubkey (" + pubkey.Length + "):" + @System.Text.Encoding.UTF8.GetString(pubkey) + "\r\ninitialization vector (" + iv.Length + "): " + @System.Text.Encoding.UTF8.GetString(iv) + "\r\ncipher (" + cipher.Length + "): " + @cipher + "\r\n\r\nException:\r\n" + e.ToString());
cipher = "ENCRYPTION ERROR";
}
return cipher;
}
public string Decrypt(string input)
{
string message = "";
byte[] iv = Encoding.UTF8.GetBytes(@input.Substring(0, 15)); // ლ(ಠ益ಠლ)
byte[] cipherdata = Encoding.UTF8.GetBytes(@input.Substring(16, input.Length - 16)); // ಥ‸ಥ
byte[] pubkey = Encoding.UTF8.GetBytes(public_key.PadRight(16, '#').Substring(0, 16));
RijndaelManaged Crypto = new RijndaelManaged();
Crypto.Key = pubkey;
Crypto.Mode = CipherMode.CBC;
Crypto.Padding = PaddingMode.PKCS7;
Crypto.KeySize = 256;
Crypto.BlockSize = 128;
try
{
ICryptoTransform Decrypt = Crypto.CreateDecryptor();
Decrypt.TransformFinalBlock(cipherdata, 0, cipherdata.Length);
message = System.Text.Encoding.UTF8.GetString(cipherdata);
if (debug)
MessageBox.Show("DEC\r\ninput (" + input.Length + "): " + @input + "\r\ncipherdata (" + cipherdata.Length + "): " + @Encoding.UTF8.GetString(cipherdata) + "\r\ninitialization vector (" + iv.Length + "): " + @Encoding.UTF8.GetString(iv) + "\r\npubkey (" + pubkey.Length + "): " + @Encoding.UTF8.GetString(pubkey) + "\r\nMessage (" + message.Length + "): " + message);
}
catch (CryptographicException e)
{
MessageBox.Show("DEC\r\ninput (" + input.Length + "): " + @input + "\r\ncipherdata (" + cipherdata.Length + "): " + @Encoding.UTF8.GetString(cipherdata) + "\r\ninitialization vector (" + iv.Length + "): " + @Encoding.UTF8.GetString(iv) + "\r\npubkey (" + pubkey.Length + "): " + @Encoding.UTF8.GetString(pubkey) + "\r\nMessage (" + message.Length + "): " + message + "\r\n\r\nException:\r\n" + e.ToString());
message = "DECRYPTION ERROR";
}
return message;
}
最佳答案
在使用像 UTF8 这样的可变长度编码时,将随机字节序列当作字符串连接是一个坏主意。
您应该连接 IV 字节数组和密文输出缓冲区以生成字节数组,然后 Convert.ToBase64String
生成其可移植字符串表示形式。
关于C# rijndael,字符集转换导致麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29279008/
我正在使用使用jetty的Spring-Web应用程序: org.springframework.boot spring-boot-starter-web
我想匹配空格字符( )仅当它们后跟一个哈希( # )。 这是什么( #)下面是尝试做的,这是一个捕获组。 (我尝试转义括号,否则无法在组集中正确识别括号)。但是,这是行不通的。 下面的正则表达式 /#
我正在实现一个词法扫描器。我如何实现字符集?大多数字符集都采用范围形式,即 A-Z、h-L 等。我必须检查字符 ch 是否是字符集的成员。除了数组和位集之外,我可以在 Java 中使用哪种高效的数据结
我今天注意到我们的数据库使用字符集“utf8 -- UTF-8 Unicode”和排序规则“utf8_general_ci”,但里面的大多数表和列都使用 CHARSET=latin1。我会遇到任何问题
我有一个包含多种语言的 Mysql 表,一种语言一个字段。 我的字符集是utf_general_ci 当我用 phpMyAdmin 查看表格时,我有一个保加利亚语页面,如下所示: За наÑ
根据 MSDN vb.net uses this extended character set .根据我的经验,它实际上使用了这个: 我错过了什么?为什么它说它使用一个而使用另一个? 难道我做错了什么
我在 Symfony2 中有一个项目,它在我的本地主机上运行良好,但是在将其移动到外部服务器后问题已经开始。 我没有从包含波兰语字符的数据库中看到任何结果名称 在 Profiler 中,我检查了查询:
我更新了我的网络应用程序以使用 UTF-8 而不是 ANSI。 我做了以下措施来定义字符集: mysql_set_charset("utf8"); // PHP // HTML utf8_gener
typedef unsigned char Set; Set s1,s2; s1 = 0xda; PRINT(s1); printf("%d\n", s1); s2 = -s1; pri
我有一个 PHP/MySQL 应用程序,它需要在幕后处理 UTF-8 字符(UTF-8 字符不会显示在屏幕上)。 UTF-8 字符来自 PHP cURL 请求。 我需要做什么才能使 PHP 和 MyS
我正在使用 utf8_general_ci 作为字符集在 MySQL 中构建一个用户表。 1-) 使用这个字符集,两个用户一个叫 Bob 另一个叫 bob 看起来是同一个,对吧?我不知道这可能会导致一
我知道之前已经回答过这个问题,但我发现的解决方案不适用于我的系统(我已经测试过了)。 我想更改 Mysql 中的默认排序规则。这似乎设置为 latin1_swedish_ci,我想将其更改为 UTF8
我正在开发一个母语学习应用程序。我需要将一些字符存储为“ẽũ”。我的数据库设置为具有默认排序规则的 utf-8 字符集,以及受此字符影响的表。 问题是当我尝试使用常规 SQL 插入添加一行时: INS
有什么方法可以定义字符集,以及属于 的其他信息吗? jsFiddle 上的标签? 我知道他们侧面板的“信息”部分,但不允许 标签。 提前致谢! 最佳答案 是的,把它放在CSS部分: 使用 jQ
我正在创建一个对象运行时: var myObj = {}; myObj[propertyName] = propertyValue; propertyName 是变量,如果它像“a.b”,我的对象就有
假设我有一个值为 42 (*) 的字符。我需要在 n 行中打印这个字符,n 由用户定义。但是,对于每个换行,都必须打印另一个 *。如果用户输入“6”,那么结果将是这样的: * ** *** ****
该标准定义了 基本源字符集 基本执行字符集及其对应的宽字符 它还定义了“执行字符集”及其对应的宽字符,如下所示 $2.2/3- "The execution character set and the
我想 ping 目标 IP 地址并接收响应。为此,我在 Java 中使用带有 runtime.exec 方法和进程类的 Windows 命令行。我正在使用 inputStreamReader 获取响应
是否有任何方法可以更改 MySQL Workbench 字符集?我的架构使用 UTF-8,当我查看表数据(另存为 UTF-8)或手动添加数据时,出现字符集错误,可能 MySQL Workbench 使
我有一个文本选择/规范对话框,用户可以从列表中指定字体,设置粗体、斜体等。然后我使用 LOGFONT 中的信息创建一个 CFont 随后在 CEdit 中用作预览。 但是,如果用户选择像 Wingdi
我是一名优秀的程序员,十分优秀!