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

熱點內容
iturns下載的app在哪裡 瀏覽:637
雲起書院App在哪裡 瀏覽:612
怎麼對hex文件進行加密 瀏覽:282
pdf少年時 瀏覽:951
兩個同名文件夾同步 瀏覽:628
拍照文件夾哪個好用 瀏覽:87
文件夾和書包 瀏覽:508
解壓歸檔拿登記證 瀏覽:221
去體驗解壓房 瀏覽:611
單片機一鍵中斷響應 瀏覽:644
唐浩明pdf 瀏覽:842
程序員土木 瀏覽:71
android如何與web伺服器交互 瀏覽:607
蘋果小風扇app怎麼下 瀏覽:955
杭州it程序員對身體好不好 瀏覽:535
高新區編程加盟找哪家 瀏覽:31
伺服器什麼樣的好賣 瀏覽:150
單片機原理及應用第二版例題答案 瀏覽:970
base64encoderjava 瀏覽:492
linux視頻轉換 瀏覽:704