Encrypt and decrypt messages between two systems - java

I have the following system
Android app -> kafka -> listener service (Java).
I would like to make sure that there is data integrity in this process.
I want to encrypt the message in the Android app, insert it into a topic and then have listener pick it up and decrypt the message.
I would like to know what would be the best way (algorithm and practice) to encrypt and decrypt between the systems. Can this be achieved in a simple way (without certs or keys)?
Assuming there will be 5 mobile apps pushing messages to the broker.

I would like to make sure that there is data integrity in this process.
If you are concerned to ensure the message integrity during transport, using TLS (ssl, https) would do the trick in the most of the cases. If you need end-to-end security (signed, encrypted message) between other components (kafka->Java), you will need to sign the message. In most of the cases you should be ok with the first option (https).
Can this be achieved in a simple way (without certs or keys)?
PKI (certs and keys) enables us to ensure integrity and confidentiality without hardcoding the secrets (which is not very wise for mobile apps). You already have out of box libraries for SSL, so consider it "the simple way". Building custom protocol to sign and validate the messages is only more complex and possibly less secure.
I would like to know what would be the best way (algorithm and practice) to encrypt and decrypt between the systems.
For basic transport security, I'd suggest to rely on TLS, you still need to authenticate the user (username, password?) to ensure client's identity. TLS provides secure channel up to the SSL termination point (https server, kafka listener, ..).
If you need end-to-end security (signed, encrypted message) between other components (kafka->Java), there are some standards to encrypt and sign the messages, such as WS-Security for web services or you can use signed and encrypted JWT to pass messages.
However - you will have to securely exchange the keys between the client and validating backend (with a separate service or some key exchange protocol).

I think the easiest way to implement that is to use and encrypted channel using some secure transport (https, sftp, etc.)
If you are implementing a custom protocol, then you will need to encrypt/decrypt the messages using some of the available libraries.
And yes, I suggest to use an standard encryption algorithm (which will require to use certificates).

Related

Encryption of private key in centralized PKI with bouncycastle

I'm writing a CMP server and I have a problem.
It is used in a centralized PKI where the RA send a private key with the associated certificate to the server, that we have to relay to the CMP client.
The problem is that I can't find how we are supposed to encrypt the private key to send it to the client and for him to be able to decrypt it.
Do we only rely on the security of https, because it sounds wrong ?
Did I miss something in the CRMF RFC ?
A solution would be, I guess, to generate a temporary keypair from the client and use it for encryption/decryption but is sounds more like a hack than the way to do it properly.
It is easy to do when the client is the one generating his keypair, but in this specific case, everything is generated by our OpenTrust RA.
PKCS#12 can be used to distribute private keys from an RA to the various clients. We have used a hardware USB token for this purpose. Data on the USB token can only be decrypted by the software on the client side (using a pre-shared secret).
If you don't want to use hardware tokens, the only option you have is to use a pre-shared secret (communicated via some out of band/out of channel means) and use that symmetric key to distribute the private key and transmit it over HTTPS. Also make sure your HTTPS is configured to use at least AES-256.

Android – Sending/Receiving data to/from server securely

I am developing an Android app. The app communicates with a server through a PHP API. Each user must create an account. So, the app has a login functionality. I am doing further research on how to be able to securely transfer data between client (Android app) and server. For example, a user sends, through a POST, request his username/password in order to login.
Based on what I have read, I can safely assume that in case someone “listens” the transaction between client and server he could steal the username/password combination and use it on order to login to the legitimate user’s account. Is that correct?
The solution to this problem is to encrypt the data (eg username and password) before sending them either from client or server. The data will be then decrypted by the recipient (client or server). I do that by using crypt/decrypt functions both on client (written in Java) and server (written in PHP). Each function has the same IV (initialization vector) and Secret Key (to be honest, I do not know much about the IV’s usage so forgive me if I say something wrong. I google around for information but any useful links would be really appreciated).
From what I read, the problem with this implementation is that the APK file could be decompiled from client side and get the IV and Secret Key. As a result, a listener could decrypt the data sent. Is that correct?
Trying to find a solution to this problem I have a suggestion and I would like your opinion. What if during user’s registration a unique IV and secret key are given to the each user. These values are stored both to a MySQL database (server side) and a SQLite database (client side). Whenever data needs to be sent trough a post request, the user’s id (could be something simple as an integer) and the data to be sent are encrypted using the unique IV/Secret Key for the individual user. These are stored locally so the “listener” has no access to them. Even if he decompile the APK he will just have access to his own IV/Secret Key that he already knows. Then on server side the data are decrypted using the same IV/Secret Key stored on the server. The same procedure is applied when data are sent from server to client.
Is this a correct approach?
Reusing the same symmetric key and same IV is extremely incorrect approach and must not be used ever.
Reusing the same key and IV will enable attacks where the attacker will be able to recover your secret key just be eavesdropping on the encrypted traffic for long enough. And when the attacker has your key he will be able to decrypt all and every past and future communications.
To secure the data transfer you should use HTTPS (or SSL/TLS directly if your data transfer protocol is not HTTP-based).
If your only concern is to securely communicate with the server i suggest you to install a ssl certificate to your server. Doing this way the communication will be secured by the underlying protocol. To facilitate your communication with the server for implementing ssl communication i suggest you to use aquery library, here's a link!.
Also dont forget to see the ca compatibility list for android.
Hope it helps.
This is a rather old question so i'm surprised it didn't have a more complete response. It seems you understand the concepts of symmetric encryption but you are missing knowledge of public/private key encryption. Look up RSA for a method of achieving public/private key encryption. With this, you can generate (via the assistance of a cryptographically secure RNG) new random keys as well as IVs at the start of each session to feed to your symmetric encryption system. This means that anyone listening from start to finish will not be able to make sense of your system short of brute forcing the RSA key or the symetric(AES?) key.

Transmitting and storing passwords securely

I am only a beginner developer and am doing this more to practise my skills.
I am developing a client server application in which the server will host a game and the clients will log in using a username and password.
Firstly:
Once the user enters their id and password, what would be the simplest way to have this sent to the client securely? Do I have to right my own encryption method or are there predetermined protocols that can easily be accessed using the java built in libraries? I have heard of TLS and SSL but am unsure how to make use of these.
Secondly, for security purposes I of course do not want the passwords stored on the server to be saved as plain text. Instead it would be great if instead of storing the password, a hash digest of the password is stored. In a text file for easy comparison. Is there a simple way of producing a hash digest?
Thanks a lot for any help
Encryption:
If you are going to use HTTP protocol for communication, you should use HTTP over SSL.
It provides encryption/decryption to the data transferred between client and server. It
will also help you avoid attacks such as tampering, eavesdropping, man-in-the-middle
attack.
If you have your own protocol for communication, you can always use existing algorithms
like RSA to generate a key pair. When user logs in, let the application encrypt the
password with the public portion of the Server's key, and server will decrypt it using
its own private key.
Hashing:
For hashing you can use SHA-2. (MD5 and SHA-1 can also be used, but SHA-2 is safer.)
You can store the hash of the password in the database and match it against when the user
logs in.

Encryption and decryption between PHP and Java

I am trying to make a PHP script to interact a with a Java application. They will share some information, so I would like to encrypt the data that is passed between them to make it as secure as possible, on top of having an SSL certificate. However, because my website is only on a shared server at JustHost, as far as I am aware I can not use the 'mcrypt' PHP module, so I'm not sure how to do it so that both my Java application and the PHP script can encrypt data being sent and decrypt data being received!
Your SSL conversation between Java and PHP will protect it your data while it's in transit. Should you properly protect the private key with a strong password (10+ symbols) and make sure your algorithms strong no one will be able to break it by snooping on the conversation.
You won't get any extra protection by encrypting the data before sending it over the SSL conversation. And you actually might be weakening your security because in order for you to encrypt data you'll have to share some key should you choose symmetric encryption. And, by trading secret keys you're undoing much of the protection SSL gives you because the huge benefit of SSL is the fact we can encrypt data without agreeing on a secret key. If I were trying to get at your encrypted text I'd attack your client because it's easier to find your symmetric encryption key than it is to break SSL. And while you could use asymmetric encryption you'll be basically re-inventing SSL.
I would focus on making sure your SSL conversation is strong. Using only the strongest symmetric encryption: TripleDES, IDEA, AES if your server supports it. Take out the weaker algorithms so conversations can't use the weaker encryption. Generate 1024+ public/private key pairs. That might not always be easy on your shared server, but your Java application could only choose to use TripleDES, IDEA, and AES.
Make sure you validate the server's certificate on the client side so you ensure you aren't talking to a false service. That basically means taking the server's certificate and adding it to the keystore used on the client. If that's Java you can use keytool to import a certificate and use that keystore as your TrustManager/KeyManager in your SSL conversation.
If you want to encrypt the data after it's gone over the SSL conversation then you can encrypt/decrypt on the server only. But, you still have a key management problem. If you encrypt/decrypt how do you plan on securing the secret key on the server? That's always the ugly problem that doesn't have a simple answer.

network communication encryption in java

A friend and me are working on a Java Game with a client/server - architecture.
It is working well, but i ran into a problem.
We use TCP Sockets for networking between server and client.
Our network protocol isnt encrypted and can just be read by anone who bothers to watch the stream.
We thought about how we could apply some kind of cryptography to it to hide login information and prevent people to write their own clients. But basic things like adding/substracting bytes seems pretty easy to figure out.
What are the usual methods used to encrypt network communication for games( or at least game login information )? And having written the server and client in java, are there any useful java libraries?
Use public-key encryption (RSA for example) and implement something like the SSL Handshake, or of course use SSL - here you can see an example.
Here's a simplified sequence:
the server sends his public RSA key to the client
the client generates a symmetric key (using AES for example)
the client encrypts the symmetric key with the server's public key and sends it to the server
the server decrypts the received symmetric key
Now both the client and the server have a key which no one eavesdropping can know. Then use that key to encrypt all data.
SSL(Secure Sockets Layer) is popular to handle this kind of problem.
Look at the javax.crypto library or bouncyCastle.
Both provide cryptographic primitives, also for encryption. Depending on how secure you want to have it, you can use symmetric or assymetric crypto. However, also think about key management in advance. Where do you store your private/shared key.
If it is a client-server, the best way would be to use assymetric crypto (i.e. RSA, Elliptic Curve) and give every user a certificate signed with the key of the server (note, this is TLS (formerly called SSL)). This way you can check if the user logging on is authentic. However, you dont prevent custom clients since the user has to have everyone can just copy the certificate.
In practice, it is quite hard to prevent custom clients.
You can use Ciphers. Some more examples here and here

Categories