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

熱點內容
中英文雙字幕的電影 瀏覽:538
pdf可以轉cad嗎 瀏覽:312
超能力念動力的電影 瀏覽:875
李采潭電影名 瀏覽:943
博越智尊型怎麼下載手機APP 瀏覽:453
發送郵件怎麼樣發送文件夾 瀏覽:608
抗日戰爭電影大全免費 瀏覽:571
觀看在線觀看資源視頻 瀏覽:488
廣告的演算法大數據 瀏覽:887
命令方塊技能 瀏覽:502
微轉領袖壓縮視頻 瀏覽:713
java圖形界面swing 瀏覽:977
如何從小度里找到小度app 瀏覽:927
程序員補助 瀏覽:185
postfix命令發郵件 瀏覽:644
php模塊編譯 瀏覽:428
印度十部頂級恐怖電影 瀏覽:69
wifi加密不可上網怎麼解決 瀏覽:90
煤礦退休金的演算法 瀏覽:501
情人相似的電影 瀏覽:536