- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Dynamic C Rabbit 微处理器上的编程语言。我相信 Dynamic C 是基于 ANSI-C89 的,只是做了一些非常细微的改动。他们有一个 AES 加密库,我试图在将数据发送到我的服务器之前使用它来加密数据(但收效甚微)。
这是我正在使用的基于 C 的 AES 函数的文档:
AESinitStream
SYNTAX: void AESinitStream(AESstreamState *state, const char *key, const char *init_vector);
DESCRIPTION: Sets up a stream state structure to begin encrypting or decrypting a stream. A particular stream state can only be used for one direction.
PARAMETER1: state - An AESstreamState structure to be initialized
PARAMETER2: key - the 16-byte cipher key, using a NULL pointer will prevent an existing key from being recalculated.
PARAMETER3: init_vector - a 16-byte array representing the initial state of the feedback registers. Both ends of the stream must begin with the same initialization vector and key.
AESencryptStream
SYNTAX: void AESencryptStream(AESstreamState *state, char *data, int count);
DESCRIPTION: Encrypts an array of bytes
PARAMETER1: state - The AESstreamState structure
PARAMETER2: data - an array of bytes that will be encrypted in place.
PARAMETER3: count - size of data array
这是我的 C 代码:
const char key[] = {'\x41', '\x41', '\x37', '\x44',
'\x44', '\x34', '\x30', '\x33',
'\x30', '\x35', '\x39', '\x4e',
'\x36', '\x37', '\x30', '\x38'};
AESstreamState encrypt_state; //built in Dynamic C type
char init_vector[16];
int i;
int bufLength;
sprintf(Buf, "%s", "testabc");
bufLength = strlen(Buf);
for (i = 0; i < 16; i++)
init_vector[i] = rand() % 255;
printf("Key: ");
for (i = 0; i < sizeof(key); i++)
printf("%d ", key[i]);
printf("\n");
AESinitStream(&encrypt_state, key, init_vector); //built in Dynamic C function
AESencryptStream(&encrypt_state, Buf, bufLength); //built in Dynamic C function
printf("Data: ");
for (i = 0; i < strlen(Buf); i++)
printf("%d ", Buf[i]);
printf("\n");
//set first byte to something that lets the server know it's encrypted
//set 2nd through 16th byte to the IV (initialization vector) so every message will be different even if they have the same contents
for (i = bufLength-1; i >= 0; i--)
Buf[i+17] = Buf[i];
Buf[0] = 237; //φ
printf("IV: ");
for (i = 1; i < 17; i++)
{
printf("%d ", init_vector[i-1]);
Buf[i] = init_vector[i-1];
}
printf("\n");
输出:
Key: 65 65 55 68 68 52 48 51 48 53 57 78 54 55 48 56
Data: 249 78 60 83 130 167 37
IV: 74 121 108 165 83 120 36 27 161 192 84 76 105 239 34 214
这是我的 C# 代码:
private string DecryptAES(byte[] cipherText, byte[] IV)
{
byte[] key = {
0x41, 0x41, 0x37, 0x44,
0x44, 0x34, 0x30, 0x33,
0x30, 0x35, 0x39, 0x4e,
0x36, 0x37, 0x30, 0x38
};
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
{
throw new ArgumentNullException("cipherText");
}
else if (IV == null || IV.Length <= 0 || IV.Length != 16)
{
throw new ArgumentNullException("IV");
}
Console.Write("Key: ");
for (int i = 0; i < key.Length; i++)
Console.Write("{0} ", key[i]);
Console.WriteLine();
Console.Write("Data: ");
for (int i = 0; i < cipherText.Length; i++)
Console.Write("{0} ", cipherText[i]);
Console.WriteLine();
Console.Write("IV: ");
for (int i = 0; i < IV.Length; i++)
Console.Write("{0} ", IV[i]);
Console.WriteLine();
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CFB;
rijAlg.FeedbackSize = 8;
rijAlg.BlockSize = 128;
rijAlg.Padding = PaddingMode.None;
rijAlg.Key = key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
string plaintext = null;
plaintext = srDecrypt.ReadToEnd();
Console.WriteLine("Decrypted: " + plaintext);
return plaintext;
}
}
}
}
}
输出:
Key: 65 65 55 68 68 52 48 51 48 53 57 78 54 55 48 56
Data: 249 78 60 83 130 167 37
IV: 74 121 108 165 83 120 36 27 161 192 84 76 105 239 34 214
Decrypted: t{^^?d2
如您所见,我在解密的文本中得到了一些奇怪的输出。有人能指出我正确的方向吗?
我曾尝试使用 Dynamic C 成功解密它,但如果您认为内置的 Dynamic C AES 库是罪魁祸首,您会推荐什么?
编辑:所以我决定使用 C# 的 AES 库对其进行加密并获得不同的密码:
private byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
{
throw new ArgumentNullException("plainText");
}
if (Key == null || Key.Length <= 0)
{
throw new ArgumentNullException("Key");
}
if (IV == null || IV.Length <= 0)
{
throw new ArgumentNullException("IV");
}
byte[] encrypted = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CFB;
rijAlg.FeedbackSize = 8;
rijAlg.BlockSize = 128;
rijAlg.Padding = PaddingMode.None;
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
使用我最初使用的相同字符串、 key 和 IV 调用上述函数会产生以下密码:
249 (which is correct), 43, 74, 118, 241, 179, 235
我将 FeedbackSize 更改为 16,前两个字节是正确的,但它也给了我一个错误(要加密的数据的长度无效。)并将数组大小减少到 6 个字节。我一无所知。
这是 Rabbit 中使用 AESencryptStream 的一些示例代码的片段(他们选择将 IV 设置为与 key 相同的值)。看来我正确使用了这个功能,但如果我错了请告诉我:
const char key[16] = {
'\x06', '\xa9', '\x21', '\x40', '\x36', '\xb8', '\xa1', '\x5b',
'\x51', '\x2e', '\x03', '\xd5', '\x34', '\x12', '\x00', '\x06'
};
char bblock[8192];
AESstreamState ass;
memset(bblock, 'A', sizeof(bblock));
AESinitStream(&ass, key, key);
AESencryptStream(&ass, bblock, sizeof(bblock));
AESinitStream(&ass, key, key);
AESdecryptStream(&ass, bblock, sizeof(bblock));
最佳答案
在线看AES Encryption作为引用。
Hex values:
plaintext: 74657374616263
key: 41413744443430333035394e36373038
iv: 000102030405060708090A0B0C0D0E0F
I'm not tested follows code, please test it on your pc.
DC(动态 C)
#use AES_CRYPT.LIB
#define AES_CFB_BLOCK_SIZE 16
#define PLAINTEXT_SIZE 7
//41413744443430333035394e36373038
const char key[AES_CFB_BLOCK_SIZE] = {
'\x41', '\x41', '\x37', '\x44', '\x44', '\x34', '\x30', '\x33',
'\x30', '\x35', '\x39', '\x4e', '\x36', '\x37', '\x30', '\x38'
};
//000102030405060708090A0B0C0D0E0F
const char iv[AES_CFB_BLOCK_SIZE] = {
'\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
'\x08', '\x09', '\x0A', '\x0B', '\x0C', '\x0D', '\x0E', '\x0F'
};
//testabc = 74657374616263
const char plntxt[PLAINTEXT_SIZE] =
{
't', 'e', 's', 't', 'a', 'b', 'c'
};
int main(void) {
auto int i;
auto char text[256];
auto AESstreamState encrypt_state, decrypt_state;
printf("Test case 1 - encrypt aes cfb\n");
AESinitStream(&encrypt_state, key, iv);
memcpy(text, plntxt, sizeof(plntxt));
AESencryptStream(&encrypt_state, text, AES_CFB_BLOCK_SIZE);
printf("Encrypted text:\n");
for (i = 0; i < sizeof(plntxt); i++) {
printf("%02x.", (int) text[i]);
if (0 == ((i+1) % PLAINTEXT_SIZE)) printf("\n");
}
printf("\n");
printf("Test case 2 - decrypt aes cfb \n");
AESinitStream(&decrypt_state, key, iv);
//memcpy(text, cyptxt, sizeof(cyptxt));
AESdecryptStream(&decrypt_state, text, AES_CFB_BLOCK_SIZE);
printf("Decrypted text:\n");
for (i = 0; i < sizeof(cyptxt); i++) {
printf("%02x.", (int) text[i]);
if (0 == ((i+1) % PLAINTEXT_SIZE)) printf("\n");
}
printf("\n");
return 0;
}
C#
using System;
using System.IO;
using System.Security.Cryptography;
namespace TestAES_CFB
{
class Program
{
static byte[] AES_CFB_Encrypt(string plainText, byte[] Key, byte[] IV)
{
byte[] encrypted;
using (Aes aes = Aes.Create())
{
aes.KeySize = 128; // 16 bytes
aes.BlockSize = 128; // 16 bytes
aes.Key = Key;
aes.IV = IV;
aes.Padding = PaddingMode.Zeros;
aes.Mode = CipherMode.CFB;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
static string AES_CFB_Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
string plaintext = null;
using (Aes aes = Aes.Create())
{
aes.KeySize = 128; // 16 bytes
aes.BlockSize = 128; // 16 bytes
aes.Key = Key;
aes.IV = IV;
aes.Padding = PaddingMode.Zeros;
aes.Mode = CipherMode.CFB;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
static void Main(string[] args)
{
const int AES_CFB_BLOCK_SIZE = 16;
const int PLAINTEXT_SIZE = 7;
//41413744443430333035394e36373038
byte[] key = new byte[AES_CFB_BLOCK_SIZE] {0x41, 0x41, 0x37, 0x44, 0x44, 0x34, 0x30, 0x33, 0x30, 0x35, 0x39, 0x4e, 0x36, 0x37, 0x30, 0x38};
//000102030405060708090A0B0C0D0E0F
byte[] iv = new byte[AES_CFB_BLOCK_SIZE] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
//testabc = 74657374616263
string plntxt = "testabc";
using (Aes myAes = Aes.Create())
{
byte[] encrypted = AES_CFB_Encrypt(plntxt, key, iv);
string decrypted = AES_CFB_Decrypt(encrypted, key, iv);
Console.WriteLine("Encrypted: {0}", BitConverter.ToString(encrypted));
Console.WriteLine("Decrypted: {0}", decrypted);
Console.ReadLine();
}
}
}
}
关于c# - AES 加密/解密 - 动态 C 到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466430/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!