September
19th
2008

Java里产生随机数的方法

电脑技术 没有评论

评分: 很差劲不怎样还可以还不错太棒了
Loading ... Loading ...

1. 使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以把他乘以一定的数,比如说乘以100,他就是个100以内的随机。
2. System类中有一个currentTimeMillis()方法,这个方法返回一个从1970年1月1号0点0分0秒到目前的一个毫秒数,返回类型是long,它可以作为一个随机数。拿它对一些数取模,就可以把他限制在一个范围之内了。
3. 在java.util 这个包里面提供了一个Random的类,可以新建一个Random的对象来产生随机数,他可以产生随机整数、随机float、随机double,随机long。

  1. java.util.Random rand=new java.util.Random();
  2. System.out.println(rand.nextInt());

要产生100以内的随机数,可以使用种子,如下代码:

  1. java.util.Random rand=new java.util.Random(100);
  2. System.out.println(rand.nextInt());

如果没有提供种子数,Random实例的种子数将是当前时间的毫秒数,就是方法2里面的System.currentTimeMillis()来获得当前时间的毫秒数作为种子


August
18th
2008

Use DES Encryption Algorithm in Java

电脑技术 没有评论

评分: 很差劲不怎样还可以还不错太棒了
Loading ... Loading ...

Use DES Encryption Algorithm in Java

  1. // :encryption.java
  2. /**
  3.  * @author i4wei
  4.  * @date Aug. 18, 2008
  5.  * @version 1.0
  6.  */
  7.  
  8. import java.security.*;
  9. import javax.crypto.*;
  10. import java.io.*;
  11. import java.util.*;
  12.  
  13. public class encryption {
  14. public static void main(String[] args) {
  15. encryption my = new encryption();
  16. my.run();
  17. }
  18.  
  19. public void run() {
  20. //add new encryption algorithm JCE
  21. Security.addProvider(new com.sun.crypto.provider.SunJCE());
  22. String Algorithm = "DES"; // DES, DESede or Blowfish
  23. String plaintext = "original information";
  24. try {
  25. //generate DES key
  26. KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
  27. SecretKey DESkey = keygen.generateKey();
  28. //save key to DESkey.dat file
  29. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("DESkey.dat"));
  30. out.writeObject(DESkey);
  31. out.close();
  32. System.out.println("DES key saved.");
  33. //encryption
  34. Cipher c1 = Cipher.getInstance(Algorithm);
  35. c1.init(Cipher.ENCRYPT_MODE, DESkey);
  36. byte[] cipherByte = c1.doFinal(plaintext.getBytes());
  37. System.out.println("Encrypte Successful.");
  38. System.out.println("cipher: " + byte2hex(cipherByte));
  39.  
  40. //decryption
  41. c1 = Cipher.getInstance(Algorithm);
  42. c1.init(Cipher.DECRYPT_MODE, DESkey);
  43. byte[] clearByte = c1.doFinal(cipherByte);
  44. if (Arrays.equals(clearByte, plaintext.getBytes())) {
  45. System.out.println("Decrypt Successful.");
  46. } else {
  47. System.out.println("Fail to decrypt.");
  48. }
  49. } catch (NoSuchAlgorithmException e1) {
  50. e1.printStackTrace();
  51. } catch (NoSuchPaddingException e2) {
  52. e2.printStackTrace();
  53. } catch (Exception e3) {
  54. e3.printStackTrace();
  55. }
  56. }
  57.  
  58. public String byte2hex(byte[] b) //binary to string
  59. {
  60. String hs = "";
  61. String stmp = "";
  62. for (int n = 0; n < b.length; n++) {
  63. stmp = (Integer.toHexString(b[n] & 0XFF));
  64. if (stmp.length() == 1) {
  65. hs = hs + "0" + stmp;
  66. } else hs = hs + stmp;
  67. }
  68. return hs.toUpperCase();
  69. }
  70.  
  71. }


August
14th
2008

波兰安全研究员声称发现多个移动版Java漏洞

信息安全 没有评论

评分: 很差劲不怎样还可以还不错太棒了
Loading ... Loading ...

一名波兰安全研究员,宣称已找到移动Java的多个瑕疵,但他要求2万欧元的赏金,才愿意提供完整的细节资料.
Security Explorations创始人兼首席执行官Adam Gowdiak个人网站上写道,他已经制作了两组概念验证码(总长超过1.4万行),足以攻击那些影响Sun与诺基亚在其产品中使用移动Java(简称 J2ME)执行的弱点.他把自己178页报告的前几页公布在网站上,但要求诺基亚或Sun付给他2万欧元后,才提供其余的内容.


April
19th
2007

Use MySQL in Java on Windows

电脑技术 没有评论

评分: 很差劲不怎样还可以还不错太棒了
Loading ... Loading ...

In order to run MySQL through Java’s JDBC, you will need the MySQL installation file and the MySQL driver. Both of these are available from the course web site through these links jdk-6-windows-i586.exe mysql-5.0.27-win32.zip mysql-connector-java-5.0.4.zip All represent the latest release versions at the time of writing. These packages are available (in the truly latest versions) for download from the respective home sites:

Software Download
Java http://java.sun.com/javase/downloads/index.jsp
MySQL http://www.mysql.com/downloads/
MySQL
Connector/J
http://www.mysql.com/downloads/