導航:首頁 > 編程語言 > 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相關的資料

熱點內容
小書亭下載的文件在哪手機文件夾 瀏覽:173
交叉編譯器編譯單個c文件 瀏覽:509
代理伺服器地址列表吧 瀏覽:928
java列出所有文件 瀏覽:866
壓縮包看圖軟體 瀏覽:187
sqlite在android中的應用 瀏覽:657
一本通pdf 瀏覽:911
2021免費的編程軟體 瀏覽:124
項目編譯後瀏覽器不對應刷新 瀏覽:565
三星升級android60 瀏覽:293
粘土的壓縮模量 瀏覽:116
美國程序員生活 瀏覽:222
51單片機摘要 瀏覽:408
英語經典pdf下載 瀏覽:320
大學文件夾怎麼刪除 瀏覽:671
linux科研軟體 瀏覽:556
ue4打包編譯著色器 瀏覽:778
雲伺服器可以在手機上登錄嗎 瀏覽:678
網游腳本為什麼要連接伺服器 瀏覽:11
程序員發展路線圖 瀏覽:320