gpt4 book ai didi

java - 使用 key iv 数据进行 Android 文本加密

转载 作者:行者123 更新时间:2023-12-01 19:50:20 26 4
gpt4 key购买 nike

数据不加密只显示Null, 如果有错误请更改代码。我认为问题是我的关键和四,但我不知道如何创建它请帮助我

 String text = Java_AES_Cipher.encrypt("123sadsad","123","vishal");
Log.i("encrypt_Text is = ", "" + text);

public static String encrypt(String key, String iv, String data) {
try {
if (key.length() < Java_AES_Cipher.CIPHER_KEY_LEN) {
int numPad = Java_AES_Cipher.CIPHER_KEY_LEN - key.length();

for(int i = 0; i < numPad; i++){
key += "0"; //0 pad to len 16 bytes
}

} else if (key.length() > Java_AES_Cipher.CIPHER_KEY_LEN) {
key = key.substring(0, CIPHER_KEY_LEN); //truncate to 16 bytes
}


IvParameterSpec initVector = new IvParameterSpec(iv.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

Cipher cipher = Cipher.getInstance(Java_AES_Cipher.CIPHER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initVector);

byte[] encryptedData = cipher.doFinal((data.getBytes()));

String base64_EncryptedData = Base64.getEncoder().encodeToString(encryptedData);
String base64_IV = Base64.getEncoder().encodeToString(iv.getBytes("UTF-8"));

return base64_EncryptedData + ":" + base64_IV;

} catch (Exception ex) {
ex.printStackTrace();
}

return null;
}

数据不加密只显示Null,如有错误请修改代码。

最佳答案

import android.util.Base64;

import javax.crypto.Cipher;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;

import javax.crypto.spec.IvParameterSpec;

public class Decrypter {

public static byte[] generateKey() throws GeneralSecurityException, UnsupportedEncodingException {
final String KEY = "com.taba.notes";
byte[] binary = KEY.getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
binary = sha.digest(binary);
// Use only first 128 bit.
binary = Arrays.copyOf(binary, 16);
return binary;
}

public static String encrypt(byte[] key, String value) throws GeneralSecurityException {
// Argument validation.
if (key.length != 16) {
throw new IllegalArgumentException("Invalid key size.");
}

SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));

byte[] original = value.getBytes(Charset.forName("UTF-8"));
byte[] binary = cipher.doFinal(original);
return Base64.encodeToString(binary, Base64.DEFAULT);
}

public static String decrypt(byte[] key, String encrypted) throws GeneralSecurityException {
// Argument validation.
if (key.length != 16) {
throw new IllegalArgumentException("Invalid key size.");
}

// Setup AES tool.
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));

byte[] binary = Base64.decode(encrypted, Base64.DEFAULT);
byte[] original = cipher.doFinal(binary);
return new String(original, Charset.forName("UTF-8"));
}
}

示例:

try{
byte[] key = Decrypter.generateKey();
String title = Decrypter.decrypt( key, note.getTitle() );
deleteFolder( title, position );
} catch(Exception e) {
e.printStackTrace();
}

关于java - 使用 key iv 数据进行 Android 文本加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59092141/

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