Files
2026-04-27 12:16:53 +08:00

48 lines
1.3 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/asymmetric/api.dart';
import 'package:stack_chan/util/value_constant.dart';
class RsaUtil {
static final _encrypter = Encrypter(
RSA(
publicKey:
RSAKeyParser().parse(ValueConstant.serverPublicKey) as RSAPublicKey,
privateKey:
RSAKeyParser().parse(ValueConstant.clientPrivateKey) as RSAPrivateKey,
encoding: RSAEncoding.OAEP,
digest: RSADigest.SHA256,
),
);
///RSA EncryptOAEP + SHA-256
static String encrypt(String plainText) {
final encrypted = _encrypter.encrypt(plainText);
return encrypted.base64;
}
///RSA DecryptOAEP + SHA-256
static String decrypt(String cipherText) {
final encrypted = Encrypted.fromBase64(cipherText);
return _encrypter.decrypt(encrypted);
}
static String decryptStackChanBlue(String cipherText) {
final stackChanBlueEncrypter = Encrypter(
RSA(
privateKey:
RSAKeyParser().parse(ValueConstant.stackChanBluePrivateKey)
as RSAPrivateKey,
encoding: RSAEncoding.OAEP,
digest: RSADigest.SHA256,
),
);
final encrypted = Encrypted.fromBase64(cipherText);
return stackChanBlueEncrypter.decrypt(encrypted);
}
}