当前位置:首页 » 编程语言 » javaphpaes

javaphpaes

发布时间: 2024-01-17 16:59:11

php AES加密对不上java的加密,请问如何实现

要注意特定的Padding实现跟算法的blockSize有关,这里php的blocksize是在php的aes加密前先对源字符串进行Padding,问题得到解决。

㈡ 怎么把php AES128的代码转成java

publicclassSimpleCrypto{

publicstaticStringencrypt(Stringseed,Stringcleartext)throwsException{
byte[]rawKey=getRawKey(seed.getBytes());
byte[]result=encrypt(rawKey,cleartext.getBytes());
returntoHex(result);
}

publicstaticStringdecrypt(Stringseed,Stringencrypted)throwsException{
byte[]rawKey=getRawKey(seed.getBytes());
byte[]enc=toByte(encrypted);
byte[]result=decrypt(rawKey,enc);
returnnewString(result);
}

privatestaticbyte[]getRawKey(byte[]seed)throwsException{
KeyGeneratorkgen=KeyGenerator.getInstance("AES");
SecureRandomsr=SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128,sr);//
SecretKeyskey=kgen.generateKey();
byte[]raw=skey.getEncoded();
returnraw;
}


privatestaticbyte[]encrypt(byte[]raw,byte[]clear)throwsException{
SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");
Ciphercipher=Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
byte[]encrypted=cipher.doFinal(clear);
returnencrypted;
}

privatestaticbyte[]decrypt(byte[]raw,byte[]encrypted)throwsException{
SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");
Ciphercipher=Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
byte[]decrypted=cipher.doFinal(encrypted);
returndecrypted;
}

publicstaticStringtoHex(Stringtxt){
returntoHex(txt.getBytes());
}
publicstaticStringfromHex(Stringhex){
returnnewString(toByte(hex));
}

publicstaticbyte[]toByte(StringhexString){
intlen=hexString.length()/2;
byte[]result=newbyte[len];
for(inti=0;i<len;i++)
result[i]=Integer.valueOf(hexString.substring(2*i,2*i+2),16).byteValue();
returnresult;
}

publicstaticStringtoHex(byte[]buf){
if(buf==null)
return"";
StringBufferresult=newStringBuffer(2*buf.length);
for(inti=0;i<buf.length;i++){
appendHex(result,buf[i]);
}
returnresult.toString();
}
privatefinalstaticStringHEX="0123456789ABCDEF";
privatestaticvoidappendHex(StringBuffersb,byteb){
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}

}

热点内容
php办公系统 发布:2025-07-19 03:06:35 浏览:900
奥德赛买什么配置出去改装 发布:2025-07-19 02:53:18 浏览:40
请与网络管理员联系请求访问权限 发布:2025-07-19 02:37:34 浏览:189
ipad上b站缓存视频怎么下载 发布:2025-07-19 02:32:17 浏览:844
phpcgi与phpfpm 发布:2025-07-19 02:05:19 浏览:527
捷达方向机安全登录密码是多少 发布:2025-07-19 00:57:37 浏览:692
夜魔迅雷下载ftp 发布:2025-07-19 00:39:29 浏览:99
增值税票安全接入服务器地址 发布:2025-07-19 00:20:45 浏览:486
solidworkspcb服务器地址 发布:2025-07-18 22:50:35 浏览:822
怎么在堆叠交换机里配置vlan 发布:2025-07-18 22:42:35 浏览:630