标签: encryption

Android和Java中的RSA加密

我想用RSA加密对String进行加密。我的公钥/私钥已生成并存储在DB中。在android中,我使用以下代码:

public static String encryptRSAToString(String text, String strPublicKey) {
    byte[] cipherText = null;
    String strEncryInfoData="";
    try {

    KeyFactory keyFac = KeyFactory.getInstance("RSA");
    KeySpec keySpec = new X509EncodedKeySpec(Base64.decode(strPublicKey.trim().getBytes(), Base64.DEFAULT));
    Key publicKey = keyFac.generatePublic(keySpec);

    // get an RSA cipher object and print the provider
    final Cipher cipher = Cipher.getInstance("RSA");
    // encrypt the plain text using the public key
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    cipherText = cipher.doFinal(text.getBytes());
    strEncryInfoData = new String(Base64.encode(cipherText,Base64.DEFAULT));

    } catch (Exception e) {
    e.printStackTrace();
    }
    return strEncryInfoData.replaceAll("(\\r|\\n)", "");
}
Run Code Online (Sandbox Code Playgroud)

出于调试目的,我尝试使用相同的参数调用此方法两次,并且String结果相似(符合预期)。

我想在Java中生成相同的加密字符串。但是,“ android.util.Base64”类在Java中不可用,因此我尝试使用默认的Base64类:

public static …
Run Code Online (Sandbox Code Playgroud)

java security encryption android rsa

1
推荐指数
1
解决办法
4352
查看次数

使用SynCrypto进行AES加密的问题

我试图使用带有AES 256的SynCrypto.pas加密文件,但如果我尝试加密大小不是16字节倍数的文件,它就会失败.解密的数据包含垃圾.

例:

  • txt文件中的原始字符串

    我们正在测试该文件

  • 加密字符串

    [ù["|wáî} f*!4ìÙw-•ü¨s

  • 解密字符串

    我们正在测试t?p?J

这是我的加密代码

procedure TForm1.Button1Click(Sender: TObject);
var
  A: TAES;
  Key: TSHA256Digest;
  s, B: TAESBlock;
  ks: integer;
  st: RawByteString;
  InStream, OutStream: TFileStream;
  SuperNo, TheSize, StreamSize: Int64;

begin
  InStream := TFileStream.Create('test.txt', fmOpenRead);
  OutStream := TFileStream.Create('out.txt', fmCreate);

  InStream.Position := 0;
  OutStream.Position := 0;
  st := '1234essai';
  ks := 256;
  SHA256Weak(st, Key);
  A.EncryptInit(Key, ks);
  StreamSize := InStream.Size;
  while InStream.Position < StreamSize do
  begin
    TheSize := StreamSize - InStream.Position;
    if TheSize < 16 then
    begin …
Run Code Online (Sandbox Code Playgroud)

delphi encryption cryptography aes

1
推荐指数
1
解决办法
1819
查看次数

反向^运算符用于解密

我试图反转以下代码,以提供一个获取缓冲区并解密它的函数.

void crypt_buffer(unsigned char *buffer, size_t size, char *key) {
    size_t i;
    int j;

    j = 0;
    for(i = 0; i < size; i++) {
        if(j >= KEY_SIZE)
            j = 0;
        buffer[i] ^= key[j];
        j++;
    }
}
Run Code Online (Sandbox Code Playgroud)

我担心如果简单buffer[i] ^= (1/key[j]);就足以解密加密文件了.但是会有一些截断问题,不是吗?

c security encryption algorithm math

1
推荐指数
1
解决办法
108
查看次数

PHP:为确认电子邮件创建和存储令牌的最佳方式(3个选项)

我想在PHP中创建一个令牌,可以用作确认电子邮件,作为密码重置请求的一部分.

在网上搜索时,我在这里发现了一个帖子,我发现了以下内容:

$token = md5(uniqid(mt_rand(), true));
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我,与下面的内容相比,这是否应该首选,让我知道这里有什么不同之处?

$token = bin2hex(openssl_random_pseudo_bytes(16));
Run Code Online (Sandbox Code Playgroud)

要么

$token = bin2hex(mcrypt_create_iv(128, MCRYPT_DEV_RANDOM));
Run Code Online (Sandbox Code Playgroud)

另外,您能告诉我应该使用哪种数据类型将其存储在db中(使用MySQL)吗?

非常感谢,迈克

php encryption token

1
推荐指数
1
解决办法
1442
查看次数

AES文件解密"给定最终块未正确填充"

我想加密然后解密文件使用AES.我读过很多关于错误的话题"Given final block not properly padded".但我没有为我找到解决方案.

抱歉指定我的代码语言,我不懂java语言

这是我的代码:

变量

// IV, secret, salt in the same time
private byte[] salt = { 'h', 'u', 'n', 'g', 'd', 'h', '9', '4' };
public byte[] iv;
public SecretKey secret;
Run Code Online (Sandbox Code Playgroud)

createSecretKey

public void createSecretKey(String password){
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256);
    SecretKey tmp = factory.generateSecret(spec);
    secret = new SecretKeySpec(tmp.getEncoded(), "AES");
}
Run Code Online (Sandbox Code Playgroud)

方法加密

public void encrypt(String inputFile){
    FileInputStream fis = new FileInputStream(inputFile);
    // Save file: inputFile.enc …
Run Code Online (Sandbox Code Playgroud)

java encryption aes badpaddingexception

1
推荐指数
1
解决办法
1222
查看次数

如何在python电子邮件中使用加密密码

我希望Python在某些事件发生后自动发送邮件.在我的脚本中,我必须输入密码.有没有办法加密我的密码并在此脚本中使用它?

请举个例子,因为我不是python的专家.我在这个主题上看到的答案很少,但是没有完全讨论,只给出了一些提示.

python email encryption sendmail

1
推荐指数
1
解决办法
3738
查看次数

用PyCrypto解密AES和HMAC

在获取AES密码文本以解密时遇到一些麻烦.

在这个特定的场景中,我使用Crypto-JS在客户端加密数据,并使用PyCrypto在python服务器上解密.

encrypt.js:

  var password = 'BJhtfRjKnTDTtPXUBnErKDxfkiMCOLyP';
  var data = 'mytext';

  var masterKey = CryptoJS.SHA256(password).toString();

  // Derive keys for AES and HMAC
  var length = masterKey.toString().length / 2
  var encryptionKey = masterKey.substr(0, length);
  var hmacKey = masterKey.substr(length);

  var iv = CryptoJS.lib.WordArray.random(64/8);

  var encrypted = CryptoJS.AES.encrypt(
    data,
    encryptionKey,
    {
      iv: iv,
      mode: CryptoJS.mode.CFB
    }
  );

  var concat = iv + encrypted;

  // Calculate HMAC using iv and cipher text
  var hash = CryptoJS.HmacSHA256(concat, hmacKey);

  // Put it all together
  var registrationKey …
Run Code Online (Sandbox Code Playgroud)

python encryption hmac pycrypto cryptojs

1
推荐指数
1
解决办法
3021
查看次数

将AES解密从CryptoJS移植到Php

我正在尝试将AES解密函数从JavaScript转换为PHP脚本.忽略缩进以便于阅读.

var enc = 'EK/tvL3RsjOY1j82ILXv7W10bEU83JeaiBhlLmcZIrk=';

var key = 'FSHcT+sfRO/siok2ooweuA==' ;

var y = CryptoJS.AES.decrypt({ciphertext:     CryptoJS.enc.Base64.parse(enc)}, 
CryptoJS.enc.Base64.parse(key), 
{iv: CryptoJS.enc.Hex.parse("2323232323232323")});

var dec = y.toString(CryptoJS.enc.Utf8);
Run Code Online (Sandbox Code Playgroud)

在PHP中我试过了

$iv = mcrypt_create_iv(16, '2323232323232323'); 

$enc = 'EK/tvL3RsjOY1j82ILXv7W10bEU83JeaiBhlLmcZIrk=';

$key = 'FSHcT+sfRO/siok2ooweuA==' ;

$dec = rtrim((mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $enc, MCRYPT_MODE_CBC, $iv)), "\0\3");
Run Code Online (Sandbox Code Playgroud)

在Javascript解密工作正常,但当我执行PHP它给我奇怪的字符.

javascript php encryption aes encryption-symmetric

1
推荐指数
1
解决办法
196
查看次数

使用CryptoJS解密openssl AES

我正在尝试使用CryptoJS 3.1.5解密使用openssl加密的文件.

如果我使用CryptoJS进行加密和解密,一切正常,对于shell中的OpenSSL也是如此,但是当我尝试将CryptoJS与OpenSSL混合时,一切都会出错.

使用以下命令创建文件:

openssl enc -aes-256-cbc -in file.txt -out file.enc -k password
Run Code Online (Sandbox Code Playgroud)

我试着像这样解密:

fs.readFile('file.enc', function(err, data) {
  var decrypted = CryptoJS.AES.decrypt(
                    data.toString(),
                    "password",
                    { mode : CryptoJS.mode.CBC }
                  );

  console.log(decrypted.toString(CryptoJS.enc.Utf8));
});

// Give me this err: Uncaught Error: Malformed UTF-8 data
Run Code Online (Sandbox Code Playgroud)

而在另一方面,我做:

fs.readFile('file.txt', function(err, data) {
  var encrypted = CryptoJS.AES.encrypt(
                    data.toString(),
                    "password",
                    { mode : CryptoJS.mode.CBC });

  fs.writeFile('file.enc', encrypted);
});
Run Code Online (Sandbox Code Playgroud)

然后在壳牌:

openssl enc -d -aes-256-cbc -in file.enc -out file2.txt -k password
// Give me this err: bad …
Run Code Online (Sandbox Code Playgroud)

encryption openssl node.js cryptojs

1
推荐指数
1
解决办法
3607
查看次数

SWIFT - Realm db加密无法正常工作

我试图加密存储在领域数据库中的数据.我按照Realm的Swift页面上提到的示例代码.我想加密数据而不是数据库文件.以下是我正在使用的代码:

    var error: NSError? = nil
    let configuration = Realm.Configuration(encryptionKey: EncryptionManager().getKey())

    if let realmE = Realm(configuration: configuration, error: &error) {
        // Add an object
        realmE.write {
            realmE.add(objects, update: T.primaryKey() != nil)
        }
    }
Run Code Online (Sandbox Code Playgroud)

对象是我需要在数据库中插入的对象列表.下面是从示例代码中挑选的getKey()函数前面的代码:

func getKey() -> NSData {
    // Identifier for our keychain entry - should be unique for your application
    let keychainIdentifier = "io.Realm.test"
    let keychainIdentifierData = keychainIdentifier.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

    // First check in the keychain for an existing key
    var query: [NSString: AnyObject] …
Run Code Online (Sandbox Code Playgroud)

encryption realm ios swift

1
推荐指数
1
解决办法
1837
查看次数