Hi everyone, i am trying to use some java code to encrypt passwords entering a database on the system i am building, please could someone explain what the code below is doing and if it would be useful for encrypting passwords. Thanks

import java.util.StringTokenizer;
 
public class Encryption {
 
 
    public static String encrypt(String password) {
        StringBuffer sb = new StringBuffer();
 
        for (int i = 0; i<password.length(); i++) {
 
            char c = password.charAt(i);
            int j = c;
            sb.append(String.valueOf(j) + " ");
        }
        return sb.toString().trim();
    }
 
 
    public static String decrypt(String encryptedPassword) {
        StringTokenizer st = new StringTokenizer(encryptedPassword, " ");
        //A StringTokenizer takes a string as input and breaks it upto tokens, seperated by " "
        StringBuffer sb = new StringBuffer();
        while (st.hasMoreTokens()) {
            int c = Integer.parseInt(st.nextToken());
            char chr = (char) c;
            sb.append(chr);
        }
        return sb.toString();
    }
}

Dani AI

Generated

The code in the first post does not encrypt passwords — it merely converts each character to its numeric code point and back, so anyone who gets the stored text can recover the original password trivially. As observed, that is reversible encoding, not secure storage. Passwords should be protected with a one‑way, slow, memory‑hard hash (a password KDF) and never stored in recoverable form. See the OWASP Password Storage guidance for practical rules. OWASP Password Storage Cheat Sheet. (cheatsheetseries.owasp.org)

Suggestions like MD5 (mentioned by ) or simple fast hashes are not appropriate for authenticators because attackers can brute‑force them very quickly. Database-side reversible encryption (as suggested) keeps passwords recoverable and forces key management; that tradeoff is usually the wrong approach for login credentials. Current authoritative guidance recommends using Argon2id (preferred), or scrypt/bcrypt/PBKDF2 when Argon2 is unavailable, with per‑password salts and a configurable cost factor. See NIST SP 800‑63B and the Argon2 specification. NIST SP 800‑63B, RFC 9106 (Argon2). (pages.nist.gov)

Practical Java guidance: use a well‑tested library instead of rolling your own. Libraries to consider include jBCrypt (bcrypt) or a vetted provider that exposes PBKDF2/Argon2 (for example Bouncy Castle or an Argon2 Java binding). Workflow checklist: generate a per‑password random salt with SecureRandom, pick a strong cost/work factor and store algorithm+cost+salt+hash together, protect transport with TLS, and plan for migration/versioning of the hash when you increase the cost. Example (jBCrypt usage):

String hashed = BCrypt.hashpw(password, BCrypt.gensalt(12));
boolean ok = BCrypt.checkpw(candidate, hashed);

See jBCrypt and Bouncy Castle for Java implementations and integration. jBCrypt on GitHub, Bouncy Castle. (github.com)

Summary: stop using reversible encodings; avoid fast hashes like MD5; adopt a purpose‑built password hashing scheme (Argon2/scrypt/bcrypt/PBKDF2) via a maintained library and store salt+hash+metadata.

Recommended Answers

All 8 Replies

I don't see where you're encrypting it... You get the char value from the int value, which is from the same char value, so you're really doing nothing there. You need to get the ascii value and add least add something to it.

Member Avatar for Member #46692

if it would be useful for encrypting passwords?

What, for real databases? Probably not, the encryption system looks weak.

Isn't there some java API for this anyway?

I don't see where you're encrypting it... You get the char value from the int value, which is from the same char value, so you're really doing nothing there. You need to get the ascii value and add least add something to it.

Do you know any kind of java class i can use to encrypt passwords goin into a Mysql database.:sad:

There's always MD5, if you never have to recover the readable password again.

Java has a crypto and security package. It will do everything for you, but probably the same amount of time will be taken because you'll need to learn it.

I wouldn't recommend using java to do any encryption to the database. DBs have their own encryption built in. Just add the DBs encryption on your jdbc call.


UPDATE users SET password = AES_ENCRYPT(`users password`, `your encryption key` WHERE id=`101`;

This makes it so you don't have to have encryption in each and every java application that touches the database. Much nicer in my opinion.


The above example is for a mysql database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.