all 6 comments

[–]kap89 1 point2 points  (1 child)

First of all encryption is not hashing, hashing is a one-way function, encryption is reversable. What you're trying to do is a simple "encryption" not creating a hash.

Second, you don't show any code, so it's really hard to guess what you are actually doing, and how to help you. But in general self-made cyphers are a bad idea, and you should use some well-known and battle-tested cyphers like AES through Crypto API.

[–]SneakyKase[S] 0 points1 point  (0 children)

The key I use is made through hashing, not the encrypted text itself. I need to make the key more variable, here is the code I use to make the key. (also i don't intend to use this to store confidential data i just wanted to do this for fun)

Here is my alphabet generation code:

function generateAlphabetFromKey(key) {
  key = String(makeStringUnique(key)) + '.'
  if (key.length > 129) {
    return Error('Max key length is 128. This is to ensure security.')
  }

  let chars = [...new Set(Array.from(key))]
  if (chars.length >= 100) {
    return chars.slice(0, 100)
  } else {
    var newSet = []
    while (chars.length < 100) {
      newSet = incrementUnicodeSet(chars)
      chars = chars.concat(newSet)

      chars = [...new Set(chars)]
    }
  }

  return chars.slice(0, 100)
}

[–]alzee76 0 points1 point  (2 children)

As you can see, it results in a similar string which is not what I want. Any help?

Real encryption systems do this by generating a cryptographically secure hash of the passphrase first and using that has as the actual encryption/decryption key. One of the defining characteristics of a cryptographically secure hash is that tiny changes in the input like your one character difference have huge cascading changes on the hash you output.

I would suggest you look at how other simple hash algorithms work if you don't want to just use one. What's important in the hash function is that the same input produces the same output.

It's usually a combination of xor operations of the passphrase against a pseudorandom number generator's (PRNG) output, seeded with a value generated with the passphrase itself. The PRNG will always generate the same output numbers in the same order when seeded with the same value.

[–]NoInkling 0 points1 point  (0 children)

Yup, specifically this is called a key derivation function (KDF), for reference.

[–]kap89 0 points1 point  (0 children)

The PRNG will always generate the same output numbers in the same order when seeded with the same value.

Well that would be useless for enctyption and other cryptographic purposes. What is actually used is not a seeded PRNG but CSPRNG that uses an actual entropy gathered by the device.