☰分类导航

网络安全/安全编码规范
课程进度 12% · 第2/10章第2/10章 · 标签 1/6
— 1 —

Java安全编码规范

1. 输入验证

不安全的输入验证


```java
// 不安全的输入验证示例
public class UnsafeInputValidation {
  public void processUserInput(String input) {
    // 直接使用用户输入,没有验证
    String sql = "SELECT * FROM users WHERE name = '" + input + "'";
    executeQuery(sql);
  }

  public void displayUserInput(String input) {
    // 直接输出用户输入,没有转义
    response.getWriter().write(input);
  }
}
```
java
1
// 不安全的输入验证示例
2
public class UnsafeInputValidation {
3
public void processUserInput(String input) {
4
// 直接使用用户输入,没有验证
5
String sql = "SELECT * FROM users WHERE name = '" + input + "'";
6
executeQuery(sql);
7
}
8
 
9
public void displayUserInput(String input) {
10
// 直接输出用户输入,没有转义
11
response.getWriter().write(input);
12
}
13
}

安全的输入验证


```java
// 安全的输入验证示例
public class SecureInputValidation {
  // 使用预编译语句防止SQL注入
  public void processUserInput(String input) {
    String sql = "SELECT * FROM users WHERE name = ?";
    PreparedStatement stmt = connection.prepareStatement(sql);
    stmt.setString(1, input);
    stmt.executeQuery();
  }

  // 使用OWASP HTML Sanitizer防止XSS
  public void displayUserInput(String input) {
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.BLOCKS);
    String safeInput = policy.sanitize(input);
    response.getWriter().write(safeInput);
  }

  // 使用正则表达式验证输入格式
  public boolean validateEmail(String email) {
    return email.matches("^[A-Za-z0-9+_.-]+@(.+)$");
  }

  // 使用白名单验证输入
  public boolean validateInput(String input, Set<String> whitelist) {
    return whitelist.contains(input);
  }
}
```
java
1
// 安全的输入验证示例
2
public class SecureInputValidation {
3
// 使用预编译语句防止SQL注入
4
public void processUserInput(String input) {
5
String sql = "SELECT * FROM users WHERE name = ?";
6
PreparedStatement stmt = connection.prepareStatement(sql);
7
stmt.setString(1, input);
8
stmt.executeQuery();
9
}
10
 
11
// 使用OWASP HTML Sanitizer防止XSS
12
public void displayUserInput(String input) {
13
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.BLOCKS);
14
String safeInput = policy.sanitize(input);
15
response.getWriter().write(safeInput);
16
}
17
 
18
// 使用正则表达式验证输入格式
19
public boolean validateEmail(String email) {
20
return email.matches("^[A-Za-z0-9+_.-]+@(.+)$");
21
}
22
 
23
// 使用白名单验证输入
24
public boolean validateInput(String input, Set<String> whitelist) {
25
return whitelist.contains(input);
26
}
27
}
— 2 —

2. 密码学使用

不安全的密码学使用


```java
// 不安全的密码学使用示例
public class UnsafeCryptography {
  // 使用不安全的MD5算法
  public String hashPassword(String password) {
    MessageDigest md = MessageDigest.getInstance("MD5");
    return new String(md.digest(password.getBytes()));
  }

  // 使用ECB模式
  public byte[] encryptData(byte[] data, Key key) {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(data);
  }
}
```
java
1
// 不安全的密码学使用示例
2
public class UnsafeCryptography {
3
// 使用不安全的MD5算法
4
public String hashPassword(String password) {
5
MessageDigest md = MessageDigest.getInstance("MD5");
6
return new String(md.digest(password.getBytes()));
7
}
8
 
9
// 使用ECB模式
10
public byte[] encryptData(byte[] data, Key key) {
11
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
12
cipher.init(Cipher.ENCRYPT_MODE, key);
13
return cipher.doFinal(data);
14
}
15
}

安全的密码学使用


```java
// 安全的密码学使用示例
public class SecureCryptography {
  // 使用安全的密码哈希算法
  public String hashPassword(String password) {
    return BCrypt.hashpw(password, BCrypt.gensalt(12));
  }

  // 使用安全的加密模式
  public byte[] encryptData(byte[] data, Key key) {
    // 生成随机IV
    byte[] iv = new byte[16];
    SecureRandom random = new SecureRandom();
    random.nextBytes(iv);

    // 使用CBC模式
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);

    // 组合IV和密文
    byte[] encrypted = cipher.doFinal(data);
    byte[] combined = new byte[iv.length + encrypted.length];
    System.arraycopy(iv, 0, combined, 0, iv.length);
    System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);

    return combined;
  }

  // 安全的密钥生成
  public Key generateKey() {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(256); // 使用256位密钥
    return keyGen.generateKey();
  }
}
```
java
1
// 安全的密码学使用示例
2
public class SecureCryptography {
3
// 使用安全的密码哈希算法
4
public String hashPassword(String password) {
5
return BCrypt.hashpw(password, BCrypt.gensalt(12));
6
}
7
 
8
// 使用安全的加密模式
9
public byte[] encryptData(byte[] data, Key key) {
10
// 生成随机IV
11
byte[] iv = new byte[16];
12
SecureRandom random = new SecureRandom();
13
random.nextBytes(iv);
14
 
15
// 使用CBC模式
16
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
17
IvParameterSpec ivSpec = new IvParameterSpec(iv);
18
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
19
 
20
// 组合IV和密文
21
byte[] encrypted = cipher.doFinal(data);
22
byte[] combined = new byte[iv.length + encrypted.length];
23
System.arraycopy(iv, 0, combined, 0, iv.length);
24
System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);
25
 
26
return combined;
27
}
28
 
29
// 安全的密钥生成
30
public Key generateKey() {
31
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
32
keyGen.init(256); // 使用256位密钥
33
return keyGen.generateKey();
34
}
35
}