简体   繁体   English

在ANSI C中加密,在Java中解密

[英]Encrypting in ANSI C, decrypting in Java

I have to send a short string from ANSI C application to Java application through a socket - already done. 我必须通过套接字从ANSI C应用程序向Java应用程序发送一个短字符串 - 已经完成。 Because its important data I have to encrypt it using password like "abc123". 因为它的重要数据我必须使用密码“abc123”加密它。 How should I do it in the simpliest way? 我该如何以最简单的方式做到这一点?

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. 如上所述,它在很大程度上取决于您希望这是多么安全,明智的答案是找到相同密码系统的Java和C实现并使用它们。

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. 如果您愿意接受通常伴随家庭酿造这些东西的较低安全性,我认为您是通过问题中“最简单的方式”并假设两端的源和运行时都是安全的。 IE you only need to worry about the data in transit being intercepted. IE你只需要担心被截获的传输数据。 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;
}

By "socket" I assume you mean a TCP/IP connection. 通过“socket”我假设你的意思是TCP / IP连接。 In that case you should consider using Secure Sockets Layer (SSL). 在这种情况下,您应该考虑使用安全套接字层(SSL)。 SSL pretty much solves most of the security problems associated with sending data across the wire. SSL几乎解决了与通过线路发送数据相关的大多数安全问题。 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. 如果您正在谈论Unix域套接字,那么您可能不需要费心加密,因为域套接字只是进程间管道。

Char encrypt(char ch) {
 Return (ch ^0x55);
}

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

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