Advertisement
rraito

//Java Encryption Using Key

Apr 18th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. //Java Encryption Using Key
  2. //Kalo mau paspornya lebih panjang
  3. /*
  4. SecretKey = getSecretKey();
  5. byte[] md5Key = null;
  6. try {
  7. MessageDigest md = MessageDigest.getInstance("MD5");
  8. md.update(SecretKey.getBytes("UTF-8"),0,SecretKey.length());
  9. md5Key = md.digest();
  10. // Log.d("md5Key",md.toString());
  11. } catch (Exception e1) {
  12. // TODO Auto-generated catch block
  13. e1.printStackTrace();
  14. }
  15.  
  16. */
  17.  
  18.  
  19.  
  20. import java.io.UnsupportedEncodingException;
  21. import java.security.NoSuchAlgorithmException;
  22.  
  23. import javax.crypto.Cipher;
  24. import javax.crypto.NoSuchPaddingException;
  25. import javax.crypto.spec.IvParameterSpec;
  26. import javax.crypto.spec.SecretKeySpec;
  27.  
  28. import android.app.Activity;
  29. import android.os.Bundle;
  30. import android.util.Log;
  31.  
  32. public class TestActivity extends Activity {
  33. private String iv = "fedcba9876543210";// Dummy iv (CHANGE IT!)
  34. private IvParameterSpec ivspec;
  35. private SecretKeySpec keyspec;
  36. private Cipher cipher;
  37.  
  38. // private String SecretKey = "0123456789abcdef";// Dummy secretKey (CHANGE
  39. // // IT!)
  40.  
  41.  
  42. private String SecretKey = "0123456789abcdef";// Dummy secretKey (CHANGE
  43. // IT!)
  44.  
  45. /** Called when the activity is first created. */
  46. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.main);
  50.  
  51.  
  52.  
  53.  
  54.  
  55. ivspec = new IvParameterSpec(iv.getBytes());
  56. keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
  57.  
  58. try {
  59. cipher = Cipher.getInstance("AES/CBC/NoPadding");
  60. } catch (NoSuchAlgorithmException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. } catch (NoSuchPaddingException e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. }
  67. Log.d("cipher.toString()", cipher.toString());
  68. Log.d("keyspec.toString()", keyspec.toString());
  69. Log.d("ivspec.toString()", ivspec.toString());
  70. Log.d("SecretKey", SecretKey);
  71. Log.d("iv", iv);
  72. String eiv = "";
  73. try {
  74. eiv = new String(encrypt(iv), "UTF-8");
  75. } catch (UnsupportedEncodingException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. } catch (Exception e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. }
  82. Log.d("eiv", eiv);
  83. try {
  84. eiv = new String(decrypt(eiv), "UTF-8");
  85. } catch (UnsupportedEncodingException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. } catch (Exception e) {
  89. // TODO Auto-generated catch block
  90. e.printStackTrace();
  91. }
  92. Log.d("eiv", eiv);
  93.  
  94.  
  95.  
  96.  
  97. /* Encrypt */
  98. String encrypted = "";
  99. try {
  100. encrypted = bytesToHex( encrypt("Mine Fujiko") );
  101. } catch (Exception e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105. Log.d("encrypted", encrypted);
  106. /* Decrypt */
  107. String decrypted = "";
  108. try {
  109. decrypted = new String( decrypt( encrypted ) );
  110. } catch (Exception e) {
  111. // TODO Auto-generated catch block
  112. e.printStackTrace();
  113. }
  114. Log.d("decrypted", decrypted);
  115. }
  116.  
  117. public byte[] encrypt(String text) throws Exception {
  118. if (text == null || text.length() == 0)
  119. throw new Exception("Empty string");
  120.  
  121. byte[] encrypted = null;
  122.  
  123. try {
  124. cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  125.  
  126. encrypted = cipher.doFinal(padString(text).getBytes());
  127. } catch (Exception e) {
  128. throw new Exception("[encrypt] " + e.getMessage());
  129. }
  130.  
  131. return encrypted;
  132. }
  133.  
  134. public byte[] decrypt(String code) throws Exception {
  135. if (code == null || code.length() == 0)
  136. throw new Exception("Empty string");
  137.  
  138. byte[] decrypted = null;
  139.  
  140. try {
  141. cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  142.  
  143. decrypted = cipher.doFinal(hexToBytes(code));
  144. } catch (Exception e) {
  145. throw new Exception("[decrypt] " + e.getMessage());
  146. }
  147. return decrypted;
  148. }
  149.  
  150. public static String bytesToHex(byte[] data) {
  151. if (data == null) {
  152. return null;
  153. }
  154.  
  155. int len = data.length;
  156. String str = "";
  157. for (int i = 0; i < len; i++) {
  158. if ((data[i] & 0xFF) < 16)
  159. str = str + "0" + java.lang.Integer.toHexString(data[i] & 0xFF);
  160. else
  161. str = str + java.lang.Integer.toHexString(data[i] & 0xFF);
  162. }
  163. return str;
  164. }
  165.  
  166. public static byte[] hexToBytes(String str) {
  167. if (str == null) {
  168. return null;
  169. } else if (str.length() < 2) {
  170. return null;
  171. } else {
  172. int len = str.length() / 2;
  173. byte[] buffer = new byte[len];
  174. for (int i = 0; i < len; i++) {
  175. buffer[i] = (byte) Integer.parseInt(
  176. str.substring(i * 2, i * 2 + 2), 16);
  177. }
  178. return buffer;
  179. }
  180. }
  181.  
  182. private static String padString(String source) {
  183. char paddingChar = ' ';
  184. int size = 16;
  185. int x = source.length() % size;
  186. int padLength = size - x;
  187.  
  188. for (int i = 0; i < padLength; i++) {
  189. source += paddingChar;
  190. }
  191.  
  192. return source;
  193. }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement