Guest User

Untitled

a guest
Nov 28th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.27 KB | None | 0 0
  1. public static void main(String[] args) throws Exception {
  2. String username = "bob@google.org";
  3. String password = "Password1";
  4. String secretID = "BlahBlahBlah";
  5. String SALT2 = "deliciously salty";
  6.  
  7. // Get the Key
  8. byte[] key = (SALT2 + username + password).getBytes();
  9. System.out.println((SALT2 + username + password).getBytes().length);
  10.  
  11. // Need to pad key for AES
  12. // TODO: Best way?
  13.  
  14. // Generate the secret key specs.
  15. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  16.  
  17. // Instantiate the cipher
  18. Cipher cipher = Cipher.getInstance("AES");
  19. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
  20.  
  21. byte[] encrypted = cipher.doFinal((secrectID).getBytes());
  22. System.out.println("encrypted string: " + asHex(encrypted));
  23.  
  24. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
  25. byte[] original = cipher.doFinal(encrypted);
  26. String originalString = new String(original);
  27. System.out.println("Original string: " + originalString + "nOriginal string (Hex): " + asHex(original));
  28. }
  29.  
  30. byte[] key = (SALT2 + username + password).getBytes("UTF-8");
  31. MessageDigest sha = MessageDigest.getInstance("SHA-1");
  32. key = sha.digest(key);
  33. key = Arrays.copyOf(key, 16); // use only first 128 bit
  34.  
  35. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  36.  
  37. PBEKeySpec pbeKeySpec;
  38. PBEParameterSpec pbeParamSpec;
  39. SecretKeyFactory keyFac;
  40.  
  41. // Salt
  42. byte[] salt = {
  43. (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
  44. (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
  45. };
  46.  
  47. // Iteration count
  48. int count = 20;
  49.  
  50. // Create PBE parameter set
  51. pbeParamSpec = new PBEParameterSpec(salt, count);
  52.  
  53. // Prompt user for encryption password.
  54. // Collect user password as char array (using the
  55. // "readPassword" method from above), and convert
  56. // it into a SecretKey object, using a PBE key
  57. // factory.
  58. System.out.print("Enter encryption password: ");
  59. System.out.flush();
  60. pbeKeySpec = new PBEKeySpec(readPassword(System.in));
  61. keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  62. SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
  63.  
  64. // Create PBE Cipher
  65. Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
  66.  
  67. // Initialize PBE Cipher with key and parameters
  68. pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
  69.  
  70. // Our cleartext
  71. byte[] cleartext = "This is another example".getBytes();
  72.  
  73. // Encrypt the cleartext
  74. byte[] ciphertext = pbeCipher.doFinal(cleartext);
  75.  
  76. import java.security.Key;
  77. import javax.crypto.Cipher;
  78. import javax.crypto.spec.SecretKeySpec;
  79. import sun.misc.*;
  80. import java.io.BufferedReader;
  81. import java.io.FileReader;
  82.  
  83. public class AESFile
  84. {
  85. private static String algorithm = "AES";
  86. private static byte[] keyValue=new byte[] {'0','2','3','4','5','6','7','8','9','1','2','3','4','5','6','7'};// your key
  87.  
  88. // Performs Encryption
  89. public static String encrypt(String plainText) throws Exception
  90. {
  91. Key key = generateKey();
  92. Cipher chiper = Cipher.getInstance(algorithm);
  93. chiper.init(Cipher.ENCRYPT_MODE, key);
  94. byte[] encVal = chiper.doFinal(plainText.getBytes());
  95. String encryptedValue = new BASE64Encoder().encode(encVal);
  96. return encryptedValue;
  97. }
  98.  
  99. // Performs decryption
  100. public static String decrypt(String encryptedText) throws Exception
  101. {
  102. // generate key
  103. Key key = generateKey();
  104. Cipher chiper = Cipher.getInstance(algorithm);
  105. chiper.init(Cipher.DECRYPT_MODE, key);
  106. byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedText);
  107. byte[] decValue = chiper.doFinal(decordedValue);
  108. String decryptedValue = new String(decValue);
  109. return decryptedValue;
  110. }
  111.  
  112. //generateKey() is used to generate a secret key for AES algorithm
  113. private static Key generateKey() throws Exception
  114. {
  115. Key key = new SecretKeySpec(keyValue, algorithm);
  116. return key;
  117. }
  118.  
  119. // performs encryption & decryption
  120. public static void main(String[] args) throws Exception
  121. {
  122. FileReader file = new FileReader("C://myprograms//plaintext.txt");
  123. BufferedReader reader = new BufferedReader(file);
  124. String text = "";
  125. String line = reader.readLine();
  126. while(line!= null)
  127. {
  128. text += line;
  129. line = reader.readLine();
  130. }
  131. reader.close();
  132. System.out.println(text);
  133.  
  134. String plainText = text;
  135. String encryptedText = AESFile.encrypt(plainText);
  136. String decryptedText = AESFile.decrypt(encryptedText);
  137.  
  138. System.out.println("Plain Text : " + plainText);
  139. System.out.println("Encrypted Text : " + encryptedText);
  140. System.out.println("Decrypted Text : " + decryptedText);
  141. }
  142. }
  143.  
  144. public class CryptoUtils {
  145.  
  146. private final String TRANSFORMATION = "AES";
  147. private final String encodekey = "1234543444555666";
  148. public String encrypt(String inputFile)
  149. throws CryptoException {
  150. return doEncrypt(encodekey, inputFile);
  151. }
  152.  
  153.  
  154. public String decrypt(String input)
  155. throws CryptoException {
  156. // return doCrypto(Cipher.DECRYPT_MODE, key, inputFile);
  157. return doDecrypt(encodekey,input);
  158. }
  159.  
  160. private String doEncrypt(String encodekey, String inputStr) throws CryptoException {
  161. try {
  162.  
  163. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  164.  
  165. byte[] key = encodekey.getBytes("UTF-8");
  166. MessageDigest sha = MessageDigest.getInstance("SHA-1");
  167. key = sha.digest(key);
  168. key = Arrays.copyOf(key, 16); // use only first 128 bit
  169.  
  170. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  171.  
  172. cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
  173.  
  174. byte[] inputBytes = inputStr.getBytes();
  175. byte[] outputBytes = cipher.doFinal(inputBytes);
  176.  
  177. return Base64Utils.encodeToString(outputBytes);
  178.  
  179. } catch (NoSuchPaddingException | NoSuchAlgorithmException
  180. | InvalidKeyException | BadPaddingException
  181. | IllegalBlockSizeException | IOException ex) {
  182. throw new CryptoException("Error encrypting/decrypting file", ex);
  183. }
  184. }
  185.  
  186.  
  187. public String doDecrypt(String encodekey,String encrptedStr) {
  188. try {
  189.  
  190. Cipher dcipher = Cipher.getInstance(TRANSFORMATION);
  191. dcipher = Cipher.getInstance("AES");
  192. byte[] key = encodekey.getBytes("UTF-8");
  193. MessageDigest sha = MessageDigest.getInstance("SHA-1");
  194. key = sha.digest(key);
  195. key = Arrays.copyOf(key, 16); // use only first 128 bit
  196.  
  197. SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
  198.  
  199. dcipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
  200. // decode with base64 to get bytes
  201.  
  202. byte[] dec = Base64Utils.decode(encrptedStr.getBytes());
  203. byte[] utf8 = dcipher.doFinal(dec);
  204.  
  205. // create new string based on the specified charset
  206. return new String(utf8, "UTF8");
  207.  
  208. } catch (Exception e) {
  209.  
  210. e.printStackTrace();
  211.  
  212. }
  213. return null;
  214. }
  215. }
  216.  
  217. byte[] seed = (SALT2 + username + password).getBytes();
  218. SecureRandom random = new SecureRandom(seed);
  219. KeyGenerator generator;
  220. generator = KeyGenerator.getInstance("AES");
  221. generator.init(random);
  222. generator.init(256);
  223. Key keyObj = generator.generateKey();
  224.  
  225. import sun.misc.BASE64Decoder;
  226. import sun.misc.BASE64Encoder;
  227. import javax.crypto.Cipher;
  228. import javax.crypto.spec.SecretKeySpec;
  229. import java.security.Key;
  230.  
  231. class AESCrypto {
  232. private static final String ALGORITHM = "AES";
  233. private static final int KEY_LENGTH = 16;
  234. private static final char appender = 'a';
  235.  
  236.  
  237. static String encrypt(String word, String keyWord) throws Exception {
  238. try {
  239. Key key = new SecretKeySpec(to16Bytes(keyWord), ALGORITHM);
  240. Cipher cipher = Cipher.getInstance(ALGORITHM);
  241. cipher.init(Cipher.ENCRYPT_MODE, key);
  242. byte[] encryptedBytes = cipher.doFinal(word.getBytes());
  243. return new BASE64Encoder().encode(encryptedBytes);
  244. } catch (Exception ex) {
  245. throw new RuntimeException(ex);
  246. }
  247. }
  248.  
  249. static String decrypt(String encryptedWord, String keyWord) throws Exception {
  250. try {
  251. Key key = new SecretKeySpec(to16Bytes(keyWord), ALGORITHM);
  252. Cipher cipher = Cipher.getInstance(ALGORITHM);
  253. cipher.init(Cipher.DECRYPT_MODE, key);
  254. byte[] decryptedBytes = new BASE64Decoder().decodeBuffer(encryptedWord);
  255. byte[] decryptedValue = cipher.doFinal(decryptedBytes);
  256. return new String(decryptedValue);
  257. } catch (Exception ex) {
  258. throw new RuntimeException(ex);
  259. }
  260. }
  261.  
  262. private static byte[] to16Bytes(String word) {
  263. if (word.length() == KEY_LENGTH)
  264. return word.getBytes();
  265. String ret;
  266. if (word.length() > KEY_LENGTH)
  267. ret = word.substring(0, KEY_LENGTH);
  268. else {
  269. ret = word;
  270. StringBuilder builder = new StringBuilder();
  271. for (int i = word.length(); i < KEY_LENGTH; i++)
  272. builder.append(appender);
  273. ret += builder.toString();
  274. }
  275. return ret.getBytes();
  276. }
  277.  
  278. public static void main(String[] args) {
  279. String word = "my plain text";
  280. String encryptKey = "some secret word";
  281. try {
  282. String encrypted = AESCrypto.encrypt(word, encryptKey);
  283. String decrypted = AESCrypto.decrypt(encrypted, encryptKey);
  284. assert (decrypted.equals(word));
  285. assert (!encrypted.equals(decrypted));
  286. } catch (Exception ex) {
  287. throw new RuntimeException(ex);
  288. }
  289. encryptKey = "pass";
  290. try {
  291. String encrypted = AESCrypto.encrypt(word, encryptKey);
  292. String decrypted = AESCrypto.decrypt(encrypted, encryptKey);
  293. assert (decrypted.equals(word));
  294. assert (!encrypted.equals(decrypted));
  295. } catch (Exception ex) {
  296. throw new RuntimeException(ex);
  297. }
  298. }
  299. }
Add Comment
Please, Sign In to add comment