导航:首页 > 编程语言 > 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相关的资料

热点内容
java列出所有文件 浏览:865
压缩包看图软件 浏览:186
sqlite在android中的应用 浏览:656
一本通pdf 浏览:910
2021免费的编程软件 浏览:123
项目编译后浏览器不对应刷新 浏览:564
三星升级android60 浏览:292
粘土的压缩模量 浏览:115
美国程序员生活 浏览:219
51单片机摘要 浏览:405
英语经典pdf下载 浏览:317
大学文件夹怎么删除 浏览:668
linux科研软件 浏览:553
ue4打包编译着色器 浏览:775
云服务器可以在手机上登录吗 浏览:677
网游脚本为什么要连接服务器 浏览:9
程序员发展路线图 浏览:320
手机语音加密会议 浏览:592
冰与火pdf 浏览:421
为什么叫我买阿里云服务器 浏览:475