How do I encrypt/decrypt a string with another string as a password?

前端 未结 2 819
猫巷女王i
猫巷女王i 2021-02-02 04:03

I\'m making a simple program that takes text entered in a text box, and takes a password that\'s in another text box, then does some sort of simple encryption on it and saves it

2条回答
  •  迷失自我
    2021-02-02 04:13

    What you really need is Symmetric cryptography, i.e., the algorithm uses same key to encrypt and decrypt the data. There are many algorithms available which support symmetric cryptography like DES, AES.

    Have a look at this example: http://www.java2s.com/Code/Java/Security/EncryptionanddecryptionwithAESECBPKCS7Padding.htm

    In the above example, replace

    byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
        0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    

    with

    byte[] keyBytes = yourPassword.getBytes();
    

    It uses the bouncycastle library, which is arguably the best cryptography libraries available.

提交回复
热议问题