导航:首页 > 编程语言 > sha1prngphp

sha1prngphp

发布时间:2024-06-28 03:53:17

‘壹’ java Aes 类,可否用 php 实现,求助于懂 Java 代码的 php 程序猿

Java Aes 类,用 php 实现方法:
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESSecurityUtil {

private static final String AES ="AES";
private static final String CHARSET_NAME ="utf-8";

private static SecretKeySpec getKey(String password) throws NoSuchAlgorithmException{

KeyGenerator kgen = KeyGenerator.getInstance(AES);
SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kgen.init(128, random);

SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
return key;
}

public static String encode(String str, String password)
{
byte[] arr = encodeToArr(str, password);
return byteArrToString(arr);
}

private static byte[] encodeToArr(String str, String password)
{
try
{
Cipher cipher = Cipher.getInstance(AES);
byte[] byteContent = str.getBytes(CHARSET_NAME);

cipher.init(Cipher.ENCRYPT_MODE, getKey(password));
byte[] result = cipher.doFinal(byteContent);
return result;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}

public static String decode(String hexStr, String password){
byte[] arr = string2ByteArr(hexStr);
return decode(arr, password);
}

private static String decode(byte[] arr, String password) {
try{

Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, getKey(password));

byte[] result = cipher.doFinal(arr);
return new String(result, CHARSET_NAME);
}catch (Exception e){
e.printStackTrace();
}
return null;
}

private static String byteArrToString(byte[] arr) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i <arr.length; i++) {
String s = Integer.toString(arr[i] + 128, 16);
if (s.length() == 1){
s ="0"+ s;
}
sb.append(s);
}

return sb.toString().toUpperCase();
}

阅读全文

与sha1prngphp相关的资料

热点内容
python自动化运维之路 浏览:398
eclipsejava教程下载 浏览:985
tita搜索app怎么配置 浏览:261
oracle的连接命令 浏览:1000
基于单片机的恒温水壶 浏览:878
鸿蒙系统文件夹怎么换背景 浏览:294
b站动画算法 浏览:710
程序员每月还房贷 浏览:353
cad墙闭合命令 浏览:168
udp广播可以找到本地服务器地址 浏览:676
加密门卡手机如何复制门禁卡 浏览:266
夜莺的PDF 浏览:707
地方资讯app如何推广 浏览:756
金蝶网络加密连不上 浏览:262
压缩垃圾车的配置部件 浏览:920
视频文件能压缩吗 浏览:71
什么叫美国服务器 浏览:232
阿里云udp服务器源码 浏览:921
小陈程序员理发 浏览:552
白狐问答系统源码下载 浏览:365