在java中加密和解密一个String [英] Encrypt and decrypt a String in java

查看:155
本文介绍了在java中加密和解密一个String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚加密。我想学习如何加密和解密文件中的文本...当我提到相关的文章在网。我怀疑在同一文本上多次加密时,单个文本的加密文本是否相同?任何人都可以清除我的疑问?

解决方案

  public String encrypt(String str){
try {
//使用utf-8将字符串编码成字节
byte [] utf8 = str.getBytes(UTF8);

//加密
byte [] enc = ecipher.doFinal(utf8);

//编码字节到base64以获取字符串
return new sun.misc.BASE64Encoder()。encode(enc);
} catch(javax.crypto.BadPaddingException e){
} catch(IllegalBlockSizeException e){
} catch(UnsupportedEncodingException e){
} catch(java.io.IOException e ){
}
返回null;
}

public String decrypt(String str){
try {
//解码base64获取字节
byte [] dec = new sun。 。misc.BASE64Decoder()decodeBuffer(STR);

//解密
byte [] utf8 = dcipher.doFinal(dec);

//使用utf-8解码
return new String(utf8,UTF8);
} catch(javax.crypto.BadPaddingException e){
} catch(IllegalBlockSizeException e){
} catch(UnsupportedEncodingException e){
} catch(java.io.IOException e ){
}
返回null;
}
}

这里有一个使用类的例子:

  try {
//生成一个临时密钥。实际上,您将保存此密钥。
//另请参见使用密码加密DES。
SecretKey key = KeyGenerator.getInstance(DES)。generateKey();

//创建加密/解密类
DesEncrypter encrypter = new DesEncrypter(key);

//加密
加密字符串= encrypter.encrypt(不要告诉任何人!);

//解密
字符串解密= encrypter.decrypt(加密);
} catch(Exception e){
}


I am new to cryptography. I wish to learn how to encrypt and decrypt the text in a file... when I refer the related articles in net. I had a doubt that whether the encrypted text will be same for single text when encryption is done multiple times on the same text? Can anyone please clear my doubt?

解决方案

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to base64 to get a string
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }

    public String decrypt(String str) {
        try {
            // Decode base64 to get bytes
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);

            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } catch (java.io.IOException e) {
        }
        return null;
    }
}

Here's an example that uses the class:

try {
    // Generate a temporary key. In practice, you would save this key.
    // See also Encrypting with DES Using a Pass Phrase.
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // Create encrypter/decrypter class
    DesEncrypter encrypter = new DesEncrypter(key);

    // Encrypt
    String encrypted = encrypter.encrypt("Don't tell anybody!");

    // Decrypt
    String decrypted = encrypter.decrypt(encrypted);
} catch (Exception e) {
}

这篇关于在java中加密和解密一个String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆