当前位置:首页 » 编程语言 » 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));
}

}

热点内容
安卓手机屏幕标识怎么变大 发布:2024-11-30 02:47:07 浏览:974
墙加密区域 发布:2024-11-30 02:33:32 浏览:630
idrac中怎么控制服务器 发布:2024-11-30 02:18:27 浏览:910
蜘蛛矿池服务器地址 发布:2024-11-30 02:13:57 浏览:169
网易云访问记录 发布:2024-11-30 02:13:17 浏览:376
java的数据类型有哪些 发布:2024-11-30 02:12:42 浏览:546
win8访问win7共享 发布:2024-11-30 02:08:33 浏览:340
编程录入错误 发布:2024-11-30 02:01:58 浏览:733
相机存储卡无法读取 发布:2024-11-30 02:00:15 浏览:772
美国访问学者中介 发布:2024-11-30 01:49:47 浏览:491