gpt4 book ai didi

java - 如何在android中解析 "input must be under 256 bytes"?

转载 作者:行者123 更新时间:2023-11-30 01:09:39 28 4
gpt4 key购买 nike

我想使用 keystore 存储我的 secret token 。目前我首先使用加密 token 而不是解密的别名创建 key 。但是 cipherOutputStream.close(); 我的应用程序崩溃并显示以下错误。“输入必须小于 256 字节”。我正在传递长 token “xwejdg3kcbkgkv6858gj69gfldkxserhijhgdfsdffgrjkejbvffdfsdd ......”我谷歌了很多,但我没有找到合适的答案。任何人都可以帮助我如何更改我的代码。

createNewKeys(strAlias, getActivity());
encryptString(strtoken, strAlias, this);

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static void createNewKeys(String strAlis, Context context) {
String alias = strAlis.toString();
try {
// Create new key if needed
if (!keyStore.containsAlias(alias)) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 1);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(alias)
.setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
generator.initialize(spec);
KeyPair keyPair = generator.generateKeyPair();
}
} catch (Exception e) {
}
refreshKeys();
}

public static String encryptString(String token, String alias, Context context) {
try {
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();
String initialText = token;
Log.e("MessageApp=", ""+initialText.toString());
Cipher inCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidOpenSSL");
inCipher.init(Cipher.ENCRYPT_MODE, publicKey);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, inCipher);
cipherOutputStream.write(initialText.getBytes("UTF-8"));
cipherOutputStream.close();//Error in this line

byte[] values = outputStream.toByteArray();
encryptedText = Base64.encodeToString(values, Base64.DEFAULT);
} catch (Exception e) {
Toast.makeText(context, "encryptString Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();

}
return encryptedText;
}


public static void decryptString(String encryptedText, String alias) {
try {
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(alias, null);
Cipher output = Cipher.getInstance("RSA/ECB/PKCS1Padding");
output.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());

String cipherText = encryptedText.toString();
CipherInputStream cipherInputStream = new CipherInputStream(new ByteArrayInputStream(Base64.decode(cipherText, Base64.DEFAULT)), output);
ArrayList<Byte> values = new ArrayList<>();
int nextByte;
while ((nextByte = cipherInputStream.read()) != -1) {
values.add((byte) nextByte);
}

byte[] bytes = new byte[values.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = values.get(i).byteValue();
}

decryptedText = new String(bytes, 0, bytes.length, "UTF-8");

} catch (Exception e) {
}
}

最佳答案

要加密比模数更多的字节(- PKCS#1 v1.5 填充为 11 个字节),则需要使用混合加密系统。因此,您应该加密 AES key 并将其用于流式传输模式。

与 ECB(错误地)指出的不同,RSA 实际上不使用多个 block 。因此,除非您保留一个 block ,否则您不能将其用于流式传输。

关于java - 如何在android中解析 "input must be under 256 bytes"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543631/

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