-1

So far i need to generate 6 length random key from given string I assume given string is always unique. With that i cant to plain so i need to encrypt into some 6 length random

    String plainText = "please random me ";
    String password = "secret Key";            
    String s = someMagicalFunction(plainText,password)
//  s = "a23De1"
  • So a hash or a key derivation function like PBKDF2 rather than encryption. – Kayaman May 06 '22 at 05:58
  • Does this answer your question? [link](https://stackoverflow.com/questions/1205135/how-to-encrypt-string-in-java) – Pawan.Java May 06 '22 at 05:59
  • 16^6 pretty trivial to brute-force – g00se May 06 '22 at 06:32
  • Based on the length of the resulting key, I assume this is a practice / school task? If yes: look at hash algorithms eg. MD5 or SHA256 soo what they are doing and adopt for your purpose. If it's allowed for you to use libraries, just use md5, create a hash and choose 6 characters (same everythime) of the resulting string. Edit: ok, unfortunate. But answer remains the same. Unless performance is super important, then do some irreversible bit operations yourself. – Eskapone May 06 '22 at 06:34
  • customers and my supervisors decision bruhh – Munkhdelger Tumenbayar May 06 '22 at 06:36
  • If that key is used for security purposes, maybe show them how easily and fast that can be brute foreced. Or just use a proper hash in the background. – Eskapone May 06 '22 at 06:42
  • Pleace add your intentions to the question, what do you want to achieve? A random number should not be derrived from other values of course, but this probably isn't what you want. Do you need to verify the plain text with this code, or do you have a lookup somewhere where the original item can be retrieved? Should the code be unguessable, are collisions (same code for different values) a problem? – martinstoeckli May 06 '22 at 07:25
  • sorry went to country to stay away from job at weekends so far its not for login password I am just marking limited registration – Munkhdelger Tumenbayar May 08 '22 at 17:36

1 Answers1

0

A six character alphanumeric string, using both uppercase and lowercase is in effect a 6 'digit' base 62 number (10+26+26=62). That gives an upper limit of 62^7-1, or about 2^43-1.

Use your input string and key to generate a number in the given range, truncating as needed, and convert it to base 62 before printing.

You do not say how secure you need the string generation to be. A non-cryptographic hash, such as the FNV hash, will be faster than a cryptographic hash like SHA256.

rossum
  • 15,344
  • 1
  • 24
  • 38