Android、PHP 和 Node.js 之间的加密和解密

phpAndroidJS

1个回答

写回答

sususi

2025-06-14 14:30

+ 关注

Android
Android

Androidphp 和 Node.JS 之间的加密和解密

在现代的互联网世界中,保护数据的安全性成为了一项非常重要的任务。无论是在移动应用开发中的 Android 平台,还是在服务器端的 php 和 Node.JS 环境中,加密和解密都是必不可少的技术。本文将介绍在 Androidphp 和 Node.JS 之间进行加密和解密的方法,并提供相应的案例代码。

Android 中的加密和解密

Android 平台上,加密和解密主要通过使用 Java 加密标准库来实现。Android 提供了丰富的加密算法,包括对称加密算法(如 AES、DES)、非对称加密算法(如 RSA)以及哈希算法(如 MD5、SHA-1)。下面是一个使用 AES 对称加密算法进行加密和解密的示例代码:

Java

import Javax.crypto.Cipher;

import Javax.crypto.SecretKey;

import Javax.crypto.spec.SecretKeySpec;

public class AESUtils {

private static final String ALGORITHM = "AES";

private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";

private static final String KEY = "0123456789abcdef";

public static String encrypt(String plAInText) throws Exception {

SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM);

Cipher cipher = Cipher.getInstance(TRANSFORMATION);

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

byte[] encryptedBytes = cipher.doFinal(plAInText.getBytes());

return Base64.encodeToString(encryptedBytes, Base64.DEFAULT);

}

public static String decrypt(String encryptedText) throws Exception {

SecretKey secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM);

Cipher cipher = Cipher.getInstance(TRANSFORMATION);

cipher.init(Cipher.DECRYPT_MODE, secretKey);

byte[] encryptedBytes = Base64.decode(encryptedText, Base64.DEFAULT);

byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

return new String(decryptedBytes);

}

}

php 中的加密和解密

php 中,加密和解密可以使用 OpenSSL 扩展库来实现。php 的 OpenSSL 扩展提供了丰富的加密函数,包括对称加密函数(如 openssl_encrypt、openssl_decrypt)、非对称加密函数(如 openssl_public_encrypt、openssl_private_decrypt)以及哈希函数(如 md5、sha1)。下面是一个使用 AES 对称加密算法进行加密和解密的示例代码:

php

$key = '0123456789abcdef';

$plAInText = 'Hello, world!';

$encryptedText = openssl_encrypt($plAInText, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);

$decryptedText = openssl_decrypt($encryptedText, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);

echo $encryptedText . "\n";

echo $decryptedText . "\n";

Node.JS 中的加密和解密

在 Node.JS 中,加密和解密可以使用内置的 Crypto 模块来实现。Node.JS 的 Crypto 模块提供了丰富的加密函数,包括对称加密函数(如 crypto.createCipheriv、crypto.createDecipheriv)、非对称加密函数(如 crypto.publicEncrypt、crypto.privateDecrypt)以及哈希函数(如 crypto.createHash)。下面是一个使用 AES 对称加密算法进行加密和解密的示例代码:

Javascript

const crypto = require('crypto');

const aLGorithm = 'aes-128-ecb';

const key = '0123456789abcdef';

const plAInText = 'Hello, world!';

const cipher = crypto.createCipheriv(aLGorithm, key, null);

let encryptedText = cipher.update(plAInText, 'utf8', 'hex');

encryptedText += cipher.final('hex');

const decipher = crypto.createDecipheriv(aLGorithm, key, null);

let decryptedText = decipher.update(encryptedText, 'hex', 'utf8');

decryptedText += decipher.final('utf8');

console.log(encryptedText);

console.log(decryptedText);

加密和解密在 Androidphp 和 Node.JS 开发中都起着非常重要的作用。通过使用合适的加密算法和方法,我们可以保护数据的安全性,防止敏感信息被泄露。本文介绍了在 Androidphp 和 Node.JS 中进行加密和解密的方法,并提供了相应的案例代码供读者参考。希望本文对您在加密和解密方面的学习和实践有所帮助。

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号