gpt4 book ai didi

android - 为 Android 应用程序客户端加密和解密数据

转载 作者:太空狗 更新时间:2023-10-29 15:48:41 24 4
gpt4 key购买 nike

有了这个way我将图像转换为字符串。现在我想在服务器中发送数据之前加密这个字符串。有没有简单的加密和解密字符串的方法?

最佳答案

javax.crypto

此包为实现加密、解密或 key 协议(protocol)算法的加密应用程序提供类和接口(interface)。

支持流密码以及非对称、对称和分组密码。可以使用 SPI(服务提供者接口(interface))抽象类集成来自不同提供者的密码实现。借助 SealedObject 类,程序员可以通过使用密码对其进行加密来保护对象。

身份验证可以基于 MAC(消息身份验证代码),例如 HMAC(哈希 MAC,即具有 SHA-1 哈希函数)。

示例:

使用 AES128 加密和解密字符串的简单帮助程序类。结果是 Ascii 编码的(实际上是十六进制,没有 base64),所以不需要存储 byte[]。 SEED 值用作共享 secret (“主密码”)。只有使用相同的 SEED 才能解密存储的值。

package com.xxx;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
* Usage:
* <pre>
* String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
* ...
* String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
* </pre>
* @author ferenc.hechler
*/
public class SimpleCrypto {

public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}

public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}

private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}


private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}

public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}

public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}

}

有关更多信息,请查看 Android SecurityHow to encrypt and decrypt strings?Encryption on Android & BouncyCastle

关于android - 为 Android 应用程序客户端加密和解密数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8397213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com