Anonymous
Невозможно расшифровать данные, используя PHP -WICH, зашифруется Java [Duplicate]
Сообщение
Anonymous » 01 фев 2025, 10:40
Мы используем следующий код для шифрования данных в Java,
и пытаемся преобразовать логику в PHP. но данные, зашифрованные с одним языком, не могут расшифровать с другим языком. Есть ли какой -либо Dfference?
Код: Выделить всё
public class EncYes {
private static final char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static void main(String[] args) {
try {
String encString=null;
EncYes enc = new EncYes();
switch(args[0]){
case "e":
System.out.println(enc.encrypt(args[1],args[2]));
break;
case "d":
System.out.println(enc.decrypt(args[1],args[2]));
break;
}
} catch (Exception e) {
System.out.println(e);
}
}
public String encrypt(String json, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
Cipher cipher = null;
EncYes enc = new EncYes();
//byte[] keyBytes = new byte[16];
SecretKeySpec skeySpec = new SecretKeySpec(enc.hexfromString(key), "AES");
byte[] ivSrc = new byte[12];
GCMParameterSpec ivSpec = new GCMParameterSpec(128, ivSrc);
cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(1, skeySpec, ivSpec);
byte[] encstr = cipher.doFinal(json.getBytes());
return enc.hextoString(encstr);
}
public String decrypt(String json, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
Cipher cipher = null;
EncYes enc = new EncYes();
//byte[] keyBytes = new byte[16];
SecretKeySpec skeySpec = new SecretKeySpec(enc.hexfromString(key), "AES");
byte[] ivSrc = new byte[12];
GCMParameterSpec ivSpec = new GCMParameterSpec(128, ivSrc);
cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(2, skeySpec, ivSpec);
byte[] encstr = cipher.doFinal(enc.hexfromString(json));
return new String(encstr);
}
public static byte[] hexfromString(String s) {
int i = s.length();
byte[] abyte0 = new byte[(i + 1) / 2];
int j = 0;
int k = 0;
if (i % 2 == 1) {
abyte0[k++] = (byte)HexfromDigit(s.charAt(j++));
}
while(j < i) {
abyte0[k++] = (byte)(HexfromDigit(s.charAt(j++)) = '0' && c = 'A' && c = 'a' && c >> 4 & 0xf];
ac[k++] = hexDigits[byte0 & 0xf];
}
return new String(ac);
}
public static String hextoString(byte[] abyte0) {
return HextoString(abyte0, 0, abyte0.length);
}
public static String generateIv() {
UUID uId = UUID.randomUUID();
return uId.toString().replace("-", "");
}
}
< /code>
my php -код < /p>
class EncYes {
private static $hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
public static function main($args) {
try {
$enc = new EncYes();
switch ($args[0]) {
case "e":
return $enc->encrypt($args[1], $args[2]) . "\n";
break;
case "d":
return $enc->decrypt($args[1], $args[2]) . "\n";
break;
}
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
}
public function encrypt($json, $key) {
$iv = str_repeat("\0", 12);
$cipher = "aes-128-gcm";
$tag = "";
$encrypted = openssl_encrypt($json, $cipher, $this->hexfromString($key), OPENSSL_RAW_DATA, $iv, $tag,"",12);
if ($encrypted === false) {
throw new Exception("Encryption failed");
}
return $this->hextoString($encrypted . $tag);
}
public function decrypt($json, $key) {
$iv = str_repeat("\0", 12); // 12-byte IV filled with zeros
//$iv = base64_decode('AAAAAAAAAAAAAAAA');
$cipher = "aes-128-gcm";
$data = $this->hexfromString($json);
$encrypted = substr($data, 0, -12);
$tag = substr($data, -12);
//echo base64_encode($tag);
$decrypted = openssl_decrypt($encrypted, $cipher, $this->hexfromString($key), OPENSSL_RAW_DATA, $iv, $tag);
if ($decrypted === false) {
throw new Exception("Decryption failed");
}
return $decrypted;
}
public static function hexfromString($s) {
$i = strlen($s);
$abyte0 = array_fill(0, (int)(($i + 1) / 2), 0);
$j = 0;
$k = 0;
if ($i % 2 == 1) {
$abyte0[$k++] = self::HexfromDigit($s[$j++]);
}
while ($j < $i) {
$abyte0[$k++] = (self::HexfromDigit($s[$j++]) = '0' && $c = 'A' && $c = 'a' && $c > 4 & 0xf];
$ac[$k++] = self::$hexDigits[$byte0 & 0xf];
}
return implode("", $ac);
}
public static function hextoString($abyte0) {
return self::HextoString2($abyte0, 0, strlen($abyte0));
}
public static function generateIv() {
return str_replace("-", "", uuid_create());
}
}
Подробнее здесь:
https://stackoverflow.com/questions/794 ... -with-java
1738395621
Anonymous
Мы используем следующий код для шифрования данных в Java, и пытаемся преобразовать логику в PHP. но данные, зашифрованные с одним языком, не могут расшифровать с другим языком. Есть ли какой -либо Dfference?[code] public class EncYes { private static final char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; public static void main(String[] args) { try { String encString=null; EncYes enc = new EncYes(); switch(args[0]){ case "e": System.out.println(enc.encrypt(args[1],args[2])); break; case "d": System.out.println(enc.decrypt(args[1],args[2])); break; } } catch (Exception e) { System.out.println(e); } } public String encrypt(String json, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { Cipher cipher = null; EncYes enc = new EncYes(); //byte[] keyBytes = new byte[16]; SecretKeySpec skeySpec = new SecretKeySpec(enc.hexfromString(key), "AES"); byte[] ivSrc = new byte[12]; GCMParameterSpec ivSpec = new GCMParameterSpec(128, ivSrc); cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(1, skeySpec, ivSpec); byte[] encstr = cipher.doFinal(json.getBytes()); return enc.hextoString(encstr); } public String decrypt(String json, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { Cipher cipher = null; EncYes enc = new EncYes(); //byte[] keyBytes = new byte[16]; SecretKeySpec skeySpec = new SecretKeySpec(enc.hexfromString(key), "AES"); byte[] ivSrc = new byte[12]; GCMParameterSpec ivSpec = new GCMParameterSpec(128, ivSrc); cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(2, skeySpec, ivSpec); byte[] encstr = cipher.doFinal(enc.hexfromString(json)); return new String(encstr); } public static byte[] hexfromString(String s) { int i = s.length(); byte[] abyte0 = new byte[(i + 1) / 2]; int j = 0; int k = 0; if (i % 2 == 1) { abyte0[k++] = (byte)HexfromDigit(s.charAt(j++)); } while(j < i) { abyte0[k++] = (byte)(HexfromDigit(s.charAt(j++)) = '0' && c = 'A' && c = 'a' && c >> 4 & 0xf]; ac[k++] = hexDigits[byte0 & 0xf]; } return new String(ac); } public static String hextoString(byte[] abyte0) { return HextoString(abyte0, 0, abyte0.length); } public static String generateIv() { UUID uId = UUID.randomUUID(); return uId.toString().replace("-", ""); } } < /code> my php -код < /p> class EncYes { private static $hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; public static function main($args) { try { $enc = new EncYes(); switch ($args[0]) { case "e": return $enc->encrypt($args[1], $args[2]) . "\n"; break; case "d": return $enc->decrypt($args[1], $args[2]) . "\n"; break; } } catch (Exception $e) { echo $e->getMessage() . "\n"; } } public function encrypt($json, $key) { $iv = str_repeat("\0", 12); $cipher = "aes-128-gcm"; $tag = ""; $encrypted = openssl_encrypt($json, $cipher, $this->hexfromString($key), OPENSSL_RAW_DATA, $iv, $tag,"",12); if ($encrypted === false) { throw new Exception("Encryption failed"); } return $this->hextoString($encrypted . $tag); } public function decrypt($json, $key) { $iv = str_repeat("\0", 12); // 12-byte IV filled with zeros //$iv = base64_decode('AAAAAAAAAAAAAAAA'); $cipher = "aes-128-gcm"; $data = $this->hexfromString($json); $encrypted = substr($data, 0, -12); $tag = substr($data, -12); //echo base64_encode($tag); $decrypted = openssl_decrypt($encrypted, $cipher, $this->hexfromString($key), OPENSSL_RAW_DATA, $iv, $tag); if ($decrypted === false) { throw new Exception("Decryption failed"); } return $decrypted; } public static function hexfromString($s) { $i = strlen($s); $abyte0 = array_fill(0, (int)(($i + 1) / 2), 0); $j = 0; $k = 0; if ($i % 2 == 1) { $abyte0[$k++] = self::HexfromDigit($s[$j++]); } while ($j < $i) { $abyte0[$k++] = (self::HexfromDigit($s[$j++]) = '0' && $c = 'A' && $c = 'a' && $c > 4 & 0xf]; $ac[$k++] = self::$hexDigits[$byte0 & 0xf]; } return implode("", $ac); } public static function hextoString($abyte0) { return self::HextoString2($abyte0, 0, strlen($abyte0)); } public static function generateIv() { return str_replace("-", "", uuid_create()); } [/code] } Подробнее здесь: [url]https://stackoverflow.com/questions/79404596/unable-to-decrypt-data-using-php-wich-is-encrypted-with-java[/url]