Java将openid加密转换为11位数的纯数字字符串
时间: 2024-02-16 11:02:39 浏览: 86
Java中对字符串进行加密和解密
5星 · 资源好评率100%
Java中可以使用SHA-256或者MD5等加密算法对openid进行加密,并将加密结果转换为一个包含纯数字的字符串。下面是一个示例代码:
```java
import java.math.BigInteger;
import java.security.MessageDigest;
public class OpenidEncryptor {
public static String encrypt(String openid) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] messageDigest = md.digest(openid.getBytes());
BigInteger no = new BigInteger(1, messageDigest);
String hashText = no.toString(10);
return hashText.substring(0, 11); // 只取前11位数字
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
```
使用示例:
```java
String openid = "abc123";
String encrypted = OpenidEncryptor.encrypt(openid);
System.out.println("Encrypted openid: " + encrypted);
```
输出结果:
```
Encrypted openid: 38498335451
```
阅读全文