daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Feb 17th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.security.MessageDigest;
  2.  
  3. import javax.crypto.Cipher;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import javax.xml.bind.DatatypeConverter;
  6.  
  7. public class AES {
  8.     private static byte[] iv = "0000000000000000".getBytes();
  9.  
  10.     private static String decrypt(String encrypted, String seed)
  11.  throws Exception {
  12.         byte[] keyb = seed.getBytes("utf-8");
  13.         MessageDigest md = MessageDigest.getInstance("SHA-256");
  14.         byte[] thedigest = md.digest(keyb);
  15.         SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
  16.         Cipher dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  17.         dcipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv));
  18.  
  19.         byte[] clearbyte = dcipher.doFinal(DatatypeConverter
  20.                 .parseHexBinary(encrypted));
  21.         return new String(clearbyte);
  22.     }
  23.  
  24.     public static String encrypt(String content, String key) throws Exception {
  25.         byte[] input = content.getBytes("utf-8");
  26.  
  27.         MessageDigest md = MessageDigest.getInstance("SHA-256");
  28.         byte[] thedigest = md.digest(key.getBytes("utf-8"));
  29.         SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
  30.         Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  31.         cipher.init(Cipher.ENCRYPT_MODE, skc, new IvParameterSpec(iv));
  32.  
  33.         byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
  34.         int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
  35.         ctLength += cipher.doFinal(cipherText, ctLength);
  36.         return DatatypeConverter.printHexBinary(cipherText);
  37.     }
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         String data = "hello";
  41.         String key = "hi";
  42.         String cipher = AES.encrypt(data, key);
  43.         String decipher = AES.decrypt(cipher, key);
  44.         System.out.println(cipher);
  45.         System.out.println(decipher);
  46.     }
  47. }
  48.  
  49.  
  50. $ javac AES.java
  51. $ java AES
  52. DD0D07B77C1606C97A1B10AC8D9AC180
  53. hello
  54.  
  55.  
  56. var crypto = require('crypto');
  57.  
  58. var iv = new Buffer('0000000000000000');
  59. var encrypt = function(data, key) {
  60.     var decodeKey = crypto.createHash('sha256').update(key, 'utf-8').digest();
  61.     var cipher = crypto.createCipheriv('aes-256-cbc', decodeKey, iv);
  62.     return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
  63. };
  64.  
  65. var decrypt = function(data, key) {
  66.     var encodeKey = crypto.createHash('sha256').update(key, 'utf-8').digest();
  67.     var cipher = crypto.createDecipheriv('aes-256-cbc', encodeKey, iv);
  68.     return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
  69. };
  70.  
  71. var data = 'hello'
  72. var key = 'hi';
  73. var cipher = encrypt(data, key);
  74. var decipher = decrypt(cipher, key);
  75. console.log(cipher);
  76. console.log(decipher);
  77.  
  78.  
  79.  node aes.js
  80. dd0d07b77c1606c97a1b10ac8d9ac180
  81. hello
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top