用 ANSI C 加密,用 Java 解密

发布于 2024-11-05 07:18:45 字数 106 浏览 0 评论 0原文

我必须通过套接字从 ANSI C 应用程序向 Java 应用程序发送一个短字符串 - 已经完成。因为它的重要数据我必须使用“abc123”之类的密码对其进行加密。我应该如何以最简单的方式做到这一点?

I have to send a short string from ANSI C application to Java application through a socket - already done. Because its important data I have to encrypt it using password like "abc123". How should I do it in the simpliest way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没企图 2024-11-12 07:18:45

我认为“套接字”指的是 TCP/IP 连接。在这种情况下,您应该考虑使用安全套接字层 (SSL)。 SSL 几乎解决了与通过线路发送数据相关的大部分安全问题。您唯一需要解决的是如何将密钥分发到管道的每一端。

我强烈建议您不要推出自己的系统。加密很难正确,因此请使用现有的、经过良好测试的实现。

如果您谈论的是 Unix 域套接字,那么您可能不需要费心加密,因为域套接字只是进程间管道。

By "socket" I assume you mean a TCP/IP connection. In that case you should consider using Secure Sockets Layer (SSL). SSL pretty much solves most of the security problems associated with sending data across the wire. The only thing you need to work out is how to distribute keys to each end of the pipe.

I strongly recommend that you don't roll your own system. Crypto is hard to get right so use an existing, well tested implementation.

If you're talking about a Unix domain socket then you probably don't need to bother with encryption since domain sockets are just inter-process pipes.

累赘 2024-11-12 07:18:45

如前所述,这在很大程度上取决于您希望其安全程度,明智的答案是找到同一密码系统的 Java 和 C 实现并使用它们。

如果您愿意接受家庭酿造通常带来的较低安全性,我假设您在问题中采用“最简单的方式”,并假设两端的源和运行时都是安全的。 IE你只需要担心传输中的数据被拦截。您可以使用您想要的任何密码作为伪随机数生成器的种子(大素数除以字节索引或类似的余数),并将数据字节与生成的随机数进行异或。不是最安全的,但实施起来会非常快。

uint8_t encrypt(uint8_t iData, size_t iPos) {
  // Super large prime, our 'password', best kept super secret
  const uint64_t iSeed = 32416190071; 
  // Mostly to stop divide by zero
  // Also starting in the obvious place gives more info on the prime
  const size_t iOffset = 10;

  uint8_t iPad = iSeed % (iPos + iOffset);

  return iPad^iData;
}

As mentioned it depends very much on how secure you want this to be, the sensible answer is to find a Java and C implementation of the same cryptosystem and use those.

If you are willing to accept the lower security that usually comes with home brewing these things which I assume you are by the "simplest way" in your question and assuming both the source and runtime for both ends are secure. I.E. you only need to worry about the data in transit being intercepted. You could just use whatever password you desire as a seed for a pseudo random number generator (remainder of dividing a large prime by the byte index or similar) and XOR the bytes of data with the random numbers generated. Not the most secure but would be very quick to implement.

uint8_t encrypt(uint8_t iData, size_t iPos) {
  // Super large prime, our 'password', best kept super secret
  const uint64_t iSeed = 32416190071; 
  // Mostly to stop divide by zero
  // Also starting in the obvious place gives more info on the prime
  const size_t iOffset = 10;

  uint8_t iPad = iSeed % (iPos + iOffset);

  return iPad^iData;
}
浪漫之都 2024-11-12 07:18:45
Char encrypt(char ch) {
 Return (ch ^0x55);
}
Char encrypt(char ch) {
 Return (ch ^0x55);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文