Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

79
LINES

< > BotCompany Repo | #1005117 // Symmetric encryption/decryption test (OK)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1213L/9K/31K).

1  
!7
2  
3  
import java.security.*;
4  
import java.security.spec.*;
5  
import javax.crypto.*;
6  
import javax.crypto.spec.*;
7  
8  
p {
9  
  S original = "hello";
10  
  byte[] data = original.getBytes("UTF-8");
11  
  printHex(data);
12  
13  
  Random ranGen = new SecureRandom();
14  
  byte[] salt = new byte[8]; // 8 grains of salt
15  
  ranGen.nextBytes(salt);
16  
  
17  
  String pw = "pw";
18  
  byte[] enc = encrypt(data, pw, salt);
19  
  printHex(enc);
20  
  System.out.println("enc length: " + enc.length);
21  
  
22  
  byte[] dec = decrypt(enc, pw, salt);
23  
  S decoded = fromUtf8(dec);
24  
  System.out.println("decrypted: " + decoded);
25  
  assertEquals(original, decoded);
26  
  print("OK!!!!");
27  
}
28  
29  
static IvParameterSpec makeIV() {
30  
  ret new IvParameterSpec(new byte[16]);
31  
}
32  
33  
static SecretKey makeKey(S password, byte[] salt) throws Exception {
34  
  /* Derive the key, given password and salt. */
35  
  SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
36  
    
37  
  // only with unlimited strength:
38  
  //KeySpec spec = new PBEKeySpec(password, salt, 65536, 256);
39  
  
40  
  // Let's try this:
41  
  KeySpec spec = new PBEKeySpec(toCharArray(password), salt, 65536, 128);
42  
  
43  
  SecretKey tmp = factory.generateSecret(spec);
44  
  SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
45  
  return secret;
46  
}
47  
48  
public static byte[] encrypt(byte[] data, S password, byte[] salt) {
49  
  try {
50  
    SecretKey secret = makeKey(password, salt);
51  
    
52  
    /* Encrypt the message. */
53  
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
54  
    cipher.init(Cipher.ENCRYPT_MODE, secret, makeIV());
55  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
56  
    baos.write(cipher.update(data));
57  
    baos.write(cipher.doFinal());
58  
    byte[] ciphertext = baos.toByteArray();
59  
    return ciphertext;
60  
  } catch (Exception e) {
61  
    throw new RuntimeException(e);
62  
  }
63  
}
64  
65  
static byte[] decrypt(byte[] ciphertext, S password, byte[] salt) {
66  
  try {
67  
    SecretKey secret = makeKey(password, salt);
68  
   
69  
    /* Decrypt the message, given derived key and initialization vector. */
70  
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
71  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
72  
    cipher.init(Cipher.DECRYPT_MODE, secret, makeIV());
73  
    baos.write(cipher.update(ciphertext));
74  
    baos.write(cipher.doFinal());
75  
    return baos.toByteArray(); 
76  
  } catch (Exception e) {
77  
    throw new RuntimeException(e);
78  
  }
79  
}

Author comment

Began life as a copy of #1000344

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005117
Snippet name: Symmetric encryption/decryption test (OK)
Eternal ID of this version: #1005117/3
Text MD5: 4bef5495c36390d6a1fbaaf8f47e1952
Transpilation MD5: 53eb9e203f9832fcb311cd986d27f624
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-13 23:57:10
Source code size: 2423 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 515 / 666
Version history: 2 change(s)
Referenced in: [show references]