How to Encrypt and Decrypt Text Using Java (Simple)

How to Encrypt and Decrypt Text Using Java (Simple)

Max O'Didily

4 года назад

29,707 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Hangeryou hen
Hangeryou hen - 24.02.2022 22:06

How can I encrypt and decrypt the same text but always return the same result for the same text?

Ex:

1° cryptography
input = "Test"
encrypt(input) = 123456Abc

decrypt(123456Abc) = "Test"


2° cryptography
input = "Test"
encrypt(input) = 123456Abc (same input [Test] returned the same result)

decrypt(123456Abc) = "Test"

Ответить
Deshna Shah
Deshna Shah - 08.02.2022 09:02

How do we come to know our output is correct?
What is key value?

Ответить
Matthew K
Matthew K - 15.12.2021 02:53

Thanks man great video, only critique is the font size

Ответить
Alice Hughes
Alice Hughes - 05.07.2021 17:45

Does this work still if you input a string that includes spaces or special characters ?
thank you

Ответить
Stl
Stl - 19.06.2021 00:20

What if someone has access to this code? I mean if he knows the the encryption works.

Ответить
Shlok Kamat
Shlok Kamat - 25.05.2021 22:25

How do I store the key, to Decrypt later?

Ответить
Paula Anaya
Paula Anaya - 04.05.2021 20:52

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;

public class encrip {

private static final String UNICODE_FORMAT = "UTF-8";


public static void main(String[] args) {
String text = "Holiwis";


try {
SecretKey key = generateKey("AES"); // generamos llave
Cipher chipher;
chipher = Cipher.getInstance("AES");

byte[] encryptedData = encryptString(text, key, chipher);
String encryptedString = new String(encryptedData); // Dato encriptado

System.out.println(encryptedString);

String decrypted = decryptString(encryptedData, key, chipher);
System.out.println(decrypted);


}catch (Exception d){

}


}


public static SecretKey generateKey (String encryptionType){

try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(encryptionType);
SecretKey myKey = keyGenerator.generateKey();
return myKey;

}catch (Exception r){
return null;
}

}

public static byte[] encryptString (String dataToEncrypt, SecretKey myKey , Cipher cipher){




try{

byte[] text = dataToEncrypt.getBytes(UNICODE_FORMAT);
cipher.init(Cipher.ENCRYPT_MODE, myKey);
byte[] textEncrypted = cipher.doFinal(text);

return textEncrypted;

}catch (Exception m){
return null;
}

}

public static String decryptString (byte[] dataToDecrypt, SecretKey myKey , Cipher cipher) {


try {
cipher.init(Cipher.DECRYPT_MODE, myKey);
byte[] textDecrypted = cipher.doFinal(dataToDecrypt);
String result = new String(textDecrypted);

return result;

} catch (Exception n) {

return null;

}



}


}

Ответить
Team Pluto
Team Pluto - 12.12.2020 17:18

Tip: Please make your font bigger or zoom your video little more. Its very useful for everyone.
Great video thanks.

Ответить
mohamed dafalla
mohamed dafalla - 02.11.2020 22:50

Hello master , im sorry to write here , I am really proud of you - You have a lot to offer people - This is a wonderful thing. I hope you continue your creativity - I have a question, how is it extracted (server seed) from (server seed hash SHA-256) -
for example this server seed (4f68926f4a05851ccf1f85f4312592ecfbd86be8135f610ec8011eb68862add9) will give me hash-256 (2168b9e8f9499d5ac6f8bdec4da9598c8fbb927b5f90a2e5972f1e01cebdff52) on site xorbin , but if i dont have (server seed) - i have only server seed hash-256 how i can Decrypt to server seed , and that using CryptoJS v3.1.2 ( Base64 ) random ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=")
thanks master

Ответить
kw
kw - 29.10.2020 11:56

can is use this code for Camellia encryption?

Ответить
Abhay Patil
Abhay Patil - 27.10.2020 17:52

Please provide source code please.

Ответить
Abhay Patil
Abhay Patil - 14.10.2020 19:22

bro, please provide code for the same...!!! its urgent :(

Ответить
Vinci Noyb
Vinci Noyb - 10.10.2020 16:26

Thanks for the video. Trying to get this to work but .. how do you get encrypted data into a byte[] if you didn't encrypt it in the same session?
e.g. I want to write to a file something unreadable (encrypted). Next run, i want this to be readable (decrypted). I have to feed the decryptString method with a byte[] containing the encrypted data.
I don't see how I could get the encrypted data into byte[]. I feel like I am missing something....😬

Ответить
Danijal Azerovic
Danijal Azerovic - 07.08.2020 23:01

Hey, how to do the same thing but with files? I want to encrypt some text, write it to the file and then read the file and decrypt the text. How to use the same secret key and not generate the new one each time. Thanks

Ответить
Kiran Ojha
Kiran Ojha - 23.05.2020 15:43

Hey man how do i make this 256 bits?

Ответить
Dávid Malich
Dávid Malich - 22.05.2020 01:51

You're awesome! but.. why don't you use Dark Mode?

Ответить
Akromplayz
Akromplayz - 30.04.2020 21:09

Hi,
How would I make a program that accepts a plaintext, letter array and encrypted arrayList and uses these parameters to encrypt the word and return it.?

Ответить
avacado slayer
avacado slayer - 14.04.2020 01:52

you are a godsend during this quarantine. like the joe wick for programming

Ответить
Amir .G
Amir .G - 08.04.2020 17:29

Interesting!❤👏

Ответить